重复代码行

时间:2016-02-04 13:16:55

标签: java

对于我的学校项目,我必须在Java中制作9个简单的独眼巨人面孔。目前我已经完成,但我对重复的代码行不满意。有没有办法将此更改为更少的代码行?

public void paint(Graphics tekening){
    int i, xbegin, ybegin;
    for (i = 0; i <= 4; i++){
        //Text
        Font font = new Font("Impact", Font.BOLD, 20);
        tekening.setColor(Color.BLACK);
        tekening.setFont(font);
        tekening.drawString("VIERKANTEN CYCLOOP", 250, 30);

        //Backgroundcolor
        tekening.setColor(Color.YELLOW);
        tekening.fillRect(100, 50, 500, 500);

        //Faces
        tekening.setColor(Color.ORANGE);

        tekening.drawRect(150, 100, 75, 75);
        tekening.drawRect(300, 100, 75, 75);
        tekening.drawRect(450, 100, 75, 75);

        tekening.drawRect(150, 250, 75, 75);
        tekening.drawRect(300, 250, 75, 75);
        tekening.drawRect(450, 250, 75, 75);

        tekening.drawRect(150, 400, 75, 75);
        tekening.drawRect(300, 400, 75, 75);
        tekening.drawRect(450, 400, 75, 75);

        //Eyes
        tekening.setColor(Color.GREEN);

        tekening.fillOval(175, 120, 20, 10);
        tekening.fillOval(325, 120, 20, 10);
        tekening.fillOval(475, 120, 20, 10);

        tekening.fillOval(175, 270, 20, 10);
        tekening.fillOval(325, 270, 20, 10);
        tekening.fillOval(475, 270, 20, 10);

        tekening.fillOval(175, 420, 20, 10);
        tekening.fillOval(325, 420, 20, 10);
        tekening.fillOval(475, 420, 20, 10);

        //Pupils
        tekening.setColor(Color.BLACK);

        tekening.fillOval(181, 121, 6, 6);
        tekening.fillOval(331, 121, 6, 6);
        tekening.fillOval(481, 121, 6, 6);

        tekening.fillOval(181, 271, 6, 6);
        tekening.fillOval(331, 271, 6, 6);
        tekening.fillOval(481, 271, 6, 6);

        tekening.fillOval(181, 421, 6, 6);
        tekening.fillOval(331, 421, 6, 6);
        tekening.fillOval(481, 421, 6, 6);

        //Mouths
        tekening.setColor(Color.RED);

        tekening.fillArc(171, 145, 25, 3, 180, 180);
        tekening.fillArc(321, 145, 25, 3, 180, 180);
        tekening.fillArc(471, 145, 25, 3, 180, 180);

        tekening.fillArc(171, 295, 25, 7, 180, 180);
        tekening.fillArc(321, 295, 25, 7, 180, 180);
        tekening.fillArc(471, 295, 25, 7, 180, 180);

        tekening.fillArc(171, 445, 25, 11, 180, 180);
        tekening.fillArc(321, 445, 25, 11, 180, 180);
        tekening.fillArc(471, 445, 25, 11, 180, 180);
        }
    }
}

4 个答案:

答案 0 :(得分:4)

我建议使用基于对象的方法,例如:

public class Face {
     private int x;

     /** A little light on parameters but you get the idea. **/
     public Face(int x) {
         this.x = x;
     }

     public void draw(Graphics g) {
         // Use graphics to draw a face.
     }
}

然后你得到一个父绘制函数,如:

Face face = new Face(10);
face.draw(tekening);

Face face2 = new Face(20);
face2.draw(tekening);

删除重复的代码。清理逻辑并意味着您可以直接处理每个组件,而无需编辑某些程序样式脚本。

答案 1 :(得分:2)

使用for,并从for索引计算每次调用的参数。

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

答案 2 :(得分:1)

您可以创建一个对象来存储四个点,创建四个点对象的列表,并对列表中的每个循环使用a。 类似的东西:

public class Face {
  private final int a, b, c, d;

  public Face(int a, int b, ....) {
     this.a = a;
     ...
  }

  public int getA() {
    return a;
  }

}

然后可以使用以下内容:

List<Face> faces = // list of faces, multiple ways to create this.

for (Face face in faces) {
  tekening.drawRect(face.getA(), face.getB(),....
}

答案 3 :(得分:1)

这里有一个如何简化其中一个部分的例子,我希望它很有用。

//Faces
tekening.setColor(Color.ORANGE);
for(int var = 100; var <= 400; var += 150){

     for(int var2 = 150; var2 <= 450; var2 += 150){

          tekening.drawRect(var2, var, 75, 75);
     }
}

只需为嘴添加更多代码

即可获得相同的结果
//Mouths
tekening.setColor(Color.RED);

int second_parameter = 145;
int increment = 150;

for(int var1 = 3; var1 <= 11; var1+= 4 ){

    for(int var2 = 171; var2<=471; var2+=increment){

        tekening.fillArc(var2, second_parameter, 25, var1, 180, 180);

    }
    second_parameter+=increment;
}     
相关问题