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你就不会经历陈述,这样会更有效率,而且你不会两次询问相同的条件if

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

只需使用布尔方法。

前任:

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

现在对 isLeft()、isTop() 等每个方法都这样做。现在你可以做这样的事情了。

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

else 语句将使您的代码稍微更有效率。

于 2013-09-15T15:28:26.703 回答
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 回答