1

プログラミングを始めたばかりです。私は自分のプログラミングクラスのためにこのクラスを書きました。ドライバーからパラメーターを取り込みます (先生が教えてくれました)。次に、ユーザーが選択した jPanel の象限に比例した形状を配置します。それは正常に動作しますが、コードを書くためのより効率的でクリーンな方法があるかどうか疑問に思っています. 先生は以前のプロジェクトについて何のフィードバックもしていないので、ここで質問します. 助けてくれてありがとう。

import java.awt.*;

 public class LearnGraphics {


public static void drawRectangle(Graphics g, int size, String cord_x, String cord_y) {
        int longside = size / 4;
        int shortside = size / 8;
        int x = 0;
        int y = 0;
        if(cord_x.equals("top")&&cord_y.equals("right")) {  
            x = size-(size/3);
            y = size/8;
        }
        if(cord_x.equals("top")&&cord_y.equals("left")) {  
            x = size/12;
            y = size/8;
        }
        if(cord_x.equals("bottom")&&cord_y.equals("right")) {  
            x = size-(size/3);
            y = size-(size/4);
        }
        if(cord_x.equals("bottom")&&cord_y.equals("left")) {  
            x =  size/12;
            y = size-(size/4);
        }
        g.drawRect(x, y, longside, shortside);

    }

public static void drawLine(Graphics g, int size, String cord_x, String cord_y) {

    int x = 0;
    int y = 0;
    int x1 = 0;
    int y1 = 0;
    if(cord_x.equals("top")&&cord_y.equals("right")) {  
        x = size/12;
        y = size/6;
        x1 = size/3;
        y1 = size/6;
    }
    if(cord_x.equals("top")&&cord_y.equals("left")) {  
        x = (size/2)+(size/6);
        y = size/6;
        x1 = size-(size/12);
        y1 = size/6;
    }
    if(cord_x.equals("bottom")&&cord_y.equals("right")) {  
        x = size/12;
        y = size-(size/6);
        x1 = size/3;
        y1 = size-(size/6);
    }
    if(cord_x.equals("bottom")&&cord_y.equals("left")) {  
        x = (size/2)+(size/6);
        y = size-(size/6);
        x1 = size-(size/12);
        y1 = size-(size/6);
    }
    g.drawLine(x, y, x1, y1 )  
}
public static void drawOval(Graphics g, int size, String cord_x, String cord_y) {
    int longside = size / 4;
    int shortside = size / 8;
    int x = 0;
    int y = 0;
    if(cord_x.equals("top")&&cord_y.equals("right")) {  
        x = size-(size/3);
        y = size/8;
    }
    if(cord_x.equals("top")&&cord_y.equals("left")) {  
        x = size/12;
        y = size/8;
    }
    if(cord_x.equals("bottom")&&cord_y.equals("right")) {  
        x = size-(size/3);
        y = size-(size/4);
    }
    if(cord_x.equals("bottom")&&cord_y.equals("left")) {  
        x =  size/12;
        y = size-(size/4);
    }
    g.drawOval(x, y, longside, shortside);    
   }
  }
4

3 に答える 3

1

ステートメントを実行if-elseすると、ステートメントの1つを入力するとステートメントを通過しなくなりif、より効率的になります。さらに、同じ条件を2回尋ねることはありません

public static void drawRectangle(Graphics g, int size, String cord_x, String cord_y) {
    int longside = size / 4;
    int shortside = size / 8;
    int x = 0;
    int y = 0;
    if(cord_x.equals("top")) {  
        if(cord_y.equals("right"))
        {
           x = size-(size/3);
           y = size/8;
        }
        else
        {
           if(cord_y.equals("left"))
           {
               x = size/12;
               y = size/8;
           }
        }
    }
    else
    {

        if(cord_x.equals("bottom")) {  
           if(cord_y.equals("right"))
           {
              x = size-(size/3);
              y = size-(size/4);
           }
           else
           {
              if(cord_y.equals("left"))
              {
                 x =  size/12;
                y = size-(size/4);
              }
           }
        }
    }
    g.drawRect(x, y, longside, shortside);

}

それ以外は、最新のコンピューターは非常に高速であり、このコードは長くないため、現時点では何も心配する必要はありません。したがって、高速になります。

于 2013-09-15T15:29:05.620 に答える
0

私は列挙型の大ファンです。列挙型を使用してそれを行う方法を次に示します。これには、(別のクラスの) コードと、それらを並べて比較するドライバー プログラムも含まれます。HTH。

package test;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class LearnGraphics {
    public static void drawRectangle(Graphics g, int size, String cord_x, String cord_y) {
        Quadrant q = Quadrant.valueOf(cord_x, cord_y);
        float x = q.horiz.rectX*size, y = q.vert.rectY*size;
        g.drawRect(Math.round(x), Math.round(y), size / 4, size / 8);
    }

    public static void drawLine(Graphics g, int size, String cord_x, String cord_y) {
        Quadrant q = Quadrant.valueOf(cord_x,cord_y);
        float x0 = q.horiz.lineX0*size, y  = q.vert.lineY*size, x1 = q.horiz.lineX1*size;
        g.drawLine(Math.round(x0), Math.round(y), Math.round(x1), Math.round(y));
    }

    public static void drawOval(Graphics g, int size, String cord_x, String cord_y) {
        Quadrant q = Quadrant.valueOf(cord_x,cord_y);
        float x = q.horiz.ovalX*size, y = q.vert.ovalY*size;
        g.drawOval(Math.round(x), Math.round(y), size / 4, size / 8);
    }


    public static void invertColor(Graphics g){
        Color c = g.getColor();
        g.setColor(new Color(255-c.getRed(), 255-c.getGreen(), 255-c.getBlue()));
    }

    public static void main(String[] arg){
        JFrame jf = new JFrame("Learn Enums (NEW WAY)");
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final int size=400;

        @SuppressWarnings("serial")
        JPanel jp = new JPanel(){
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.BLUE);
                drawRectangle(g, size, "left", "top");
                g.setColor(Color.ORANGE);
                drawOval(g, size, "left", "bottom");
                g.setColor(Color.GREEN);
                drawLine(g, size, "right", "bottom");
            }
        };

        jf.setContentPane(jp);
        jf.setBounds(120, 120, size, size);
        jf.setVisible(true);

        JFrame owjf = new JFrame("Learn Graphics (OLD WAY)");
        owjf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        @SuppressWarnings("serial")
        JPanel owjp = new JPanel(){
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.BLUE);
                OldWays.drawRectangle(g, size, "left", "top");
                g.setColor(Color.ORANGE);
                OldWays.drawOval(g, size, "left", "bottom");
                g.setColor(Color.GREEN);
                OldWays.drawLine(g, size, "right", "bottom");
            };
        };
        owjf.setContentPane(owjp);
        owjf.setBounds(120+50+size, 120, size, size);
        owjf.setVisible(true);

    }

}
enum Quadrant{
    top_left(Vert.top,Horiz.left), 
    top_right(Vert.top,Horiz.right), 
    bottom_left(Vert.bottom,Horiz.left), 
    bottom_right(Vert.bottom,Horiz.right);

    private Quadrant(Vert v, Horiz h){
        vert=v;
        horiz=h;
    }
    final Vert vert;
    final Horiz horiz;
    public static Quadrant valueOf(String horiz, String vert){
        return valueOf(vert+"_"+horiz);
    }
    enum Vert{
        top(   1/8f, 1/6f, 1/8f),
        bottom(3/4f, 5/6f, 3/4f);
        private Vert(float rectY, float lineY, float ovalY){
            this.rectY=rectY;
            this.lineY=lineY;
            this.ovalY=ovalY;
        }
        final float rectY, lineY, ovalY;
    }
    enum Horiz{
        left( 1/12f,  2/3f, 11/12f, 1/12f),
        right( 1/3f, 1/12f,   1/3f,  2/3f);
        private Horiz(float rectX, float lineX0, float lineX1, float ovalX){
            this.rectX=rectX;
            this.lineX0=lineX0;
            this.lineX1=lineX1;
            this.ovalX=ovalX;
        }
        final float rectX, lineX0, lineX1, ovalX;
    }
}
class OldWays{
    /** INCLUDED SO YOU CAN TEST THE VERSIONS AGAINST EACH OTHER **/
    public static void drawRectangle(Graphics g, int size, String cord_y,
            String cord_x) {
        int longside = size / 4;
        int shortside = size / 8;
        int x = 0;
        int y = 0;
        if (cord_x.equals("top") && cord_y.equals("right")) {
            x = size - (size / 3);
            y = size / 8;
        }
        if (cord_x.equals("top") && cord_y.equals("left")) {
            x = size / 12;
            y = size / 8;
        }
        if (cord_x.equals("bottom") && cord_y.equals("right")) {
            x = size - (size / 3);
            y = size - (size / 4);
        }
        if (cord_x.equals("bottom") && cord_y.equals("left")) {
            x = size / 12;
            y = size - (size / 4);
        }

        System.out.printf("ow.drawRectangle(%d, %d, %d, %d);\n", x, y, longside, shortside);
        g.drawRect(x, y, longside, shortside);

    }

    public static void drawLine(Graphics g, int size, String cord_y,
            String cord_x) {

        int x = 0;
        int y = 0;
        int x1 = 0;
        int y1 = 0;
        if (cord_x.equals("top") && cord_y.equals("right")) {
            x = size / 12;
            y = size / 6;
            x1 = size / 3;
            y1 = size / 6;
        }
        if (cord_x.equals("top") && cord_y.equals("left")) {
            x = (size / 2) + (size / 6);
            y = size / 6;
            x1 = size - (size / 12);
            y1 = size / 6;
        }
        if (cord_x.equals("bottom") && cord_y.equals("right")) {
            x = size / 12;
            y = size - (size / 6);
            x1 = size / 3;
            y1 = size - (size / 6);
        }
        if (cord_x.equals("bottom") && cord_y.equals("left")) {
            x = (size / 2) + (size / 6);
            y = size - (size / 6);
            x1 = size - (size / 12);
            y1 = size - (size / 6);
        }
        System.out.printf("ow.drawLine(%d, %d, %d, %d);\n", x, y, x1, y1);
        g.drawLine(x, y, x1, y1);
    }

    public static void drawOval(Graphics g, int size, String cord_y,
            String cord_x) {
        int longside = size / 4;
        int shortside = size / 8;
        int x = 0;
        int y = 0;
        if (cord_x.equals("top") && cord_y.equals("right")) {
            x = size - (size / 3);
            y = size / 8;
        }
        if (cord_x.equals("top") && cord_y.equals("left")) {
            x = size / 12;
            y = size / 8;
        }
        if (cord_x.equals("bottom") && cord_y.equals("right")) {
            x = size - (size / 3);
            y = size - (size / 4);
        }
        if (cord_x.equals("bottom") && cord_y.equals("left")) {
            x = size / 12;
            y = size - (size / 4);
        }
        System.out.printf("ow.drawOval(%d, %d, %d, %d);\n", x, y, longside, shortside);
        g.drawOval(x, y, longside, shortside);
    }
}

注: 単なる値以上の列挙型を作成する場合 (この場合は値が関連付けられています)、列挙型コード自体が少し乱雑に見えることがありますが、クライアント コードがどれだけきれいになったかを見てください!

于 2013-09-15T17:43:22.087 に答える
0

ブールメソッドを使用するだけです。

元:

  public static boolean isRight(){
          return cord_x.equals("right");
     }

isLeft()、isTop() などのすべてのメソッドに対してこれを行います。これで、このようなことができます。

else if(isRight() && isBottom()) 

else ステートメントを使用すると、コードが少し効率的になります。

于 2013-09-15T15:28:26.703 に答える