如何简化graphics2d的代码?

时间:2014-04-09 05:46:44

标签: java arrays swing loops graphics2d

必须有一种更简单的方法来制作草叶!请帮忙! 也许是某种循环或数组。你可以告诉每一排弧线的草叶,但把它们全部写出来太费时了。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class MyPanel extends JPanel

{public void paintComponent(Graphics g)
  { Graphics2D g2=(Graphics2D)g;

    //Background
    Rectangle background = new Rectangle(0,0,getWidth(),getHeight());
    Color skyBlue = new Color(135,206,235);
    g2.setColor(skyBlue);
    g2.fill (background);

    //Grass
    Rectangle grass = new Rectangle(0,525,getWidth(),100);
    Color lawnGreen = new Color(124,252,0);
    g2.setColor(lawnGreen);
    g2.fill(grass);

    Arc2D.Double blade1 = new Arc2D.Double(10,500,10,35,105,180,Arc2D.OPEN);
    Arc2D.Double blade2 = new Arc2D.Double(20,500,10,35,105,180,Arc2D.OPEN);
    Arc2D.Double blade3 = new Arc2D.Double(30,500,10,35,105,180,Arc2D.OPEN);
    Arc2D.Double blade4 = new Arc2D.Double(40,500,10,35,105,180,Arc2D.OPEN);
    Arc2D.Double blade5 = new Arc2D.Double(50,500,10,35,105,180,Arc2D.OPEN);
    Arc2D.Double blade6 = new Arc2D.Double(5,510,10,35,105,180,Arc2D.OPEN); 
    Arc2D.Double blade7 = new Arc2D.Double(15,510,10,35,105,180,Arc2D.OPEN);
    Arc2D.Double blade8 = new Arc2D.Double(25,510,10,35,105,180,Arc2D.OPEN);
    Arc2D.Double blade9 = new Arc2D.Double(35,510,10,35,105,180,Arc2D.OPEN);
    Arc2D.Double blade10 = new Arc2D.Double(45,520,10,35,105,180,Arc2D.OPEN);
    Arc2D.Double blade11 = new Arc2D.Double(10,520,10,35,105,180,Arc2D.OPEN);
    Arc2D.Double blade12 = new Arc2D.Double(20,520,10,35,105,180,Arc2D.OPEN); 
    Arc2D.Double blade13 = new Arc2D.Double(30,520,10,35,105,180,Arc2D.OPEN);
    Arc2D.Double blade14 = new Arc2D.Double(40,520,10,35,105,180,Arc2D.OPEN);
    Arc2D.Double blade15 = new Arc2D.Double(50,520,10,35,105,180,Arc2D.OPEN);


    Color yellowGreen = new Color(107,142,35);
    g2.setColor(yellowGreen);
    g2.fill(blade1);
    g2.fill(blade2);
    g2.fill(blade3);
    g2.fill(blade4);
    g2.fill(blade5);
    g2.fill(blade6);
    g2.fill(blade7);
    g2.fill(blade8);
    g2.fill(blade9);
    g2.fill(blade10);
    g2.fill(blade11);
    g2.fill(blade12);
    g2.fill(blade13);
    g2.fill(blade14);
    g2.fill(blade15);

    Ellipse2D.Double circle = new Ellipse2D.Double(60,100,25,25);
    g2.setColor(Color.RED);
    g2.fill(circle);
    Rectangle box = new Rectangle(150,100,20,80);
    g2.setColor(Color.YELLOW);
    g2.fill(box);

    Line2D.Double line = new Line2D.Double(10,10,150,150);
    g2.setColor(Color.BLUE);
       g2.draw(line);
 }
}

1 个答案:

答案 0 :(得分:3)

您应该尝试减少在paint方法(IMHO)中创建的短期对象的数量。

首先创建List Shape,您可以将各个刀片添加到

 private List<Shape> blades;

用单个刀片填充......

public MyPanel() {
    blades = new ArrayList<>(25);
    blades.add(new Arc2D.Double(10, 500, 10, 35, 105, 180, Arc2D.OPEN));
    blades.add(new Arc2D.Double(20, 500, 10, 35, 105, 180, Arc2D.OPEN));
    blades.add(new Arc2D.Double(30, 500, 10, 35, 105, 180, Arc2D.OPEN));
    blades.add(new Arc2D.Double(40, 500, 10, 35, 105, 180, Arc2D.OPEN));
    blades.add(new Arc2D.Double(50, 500, 10, 35, 105, 180, Arc2D.OPEN));
    blades.add(new Arc2D.Double(5, 510, 10, 35, 105, 180, Arc2D.OPEN));
    blades.add(new Arc2D.Double(15, 510, 10, 35, 105, 180, Arc2D.OPEN));
    blades.add(new Arc2D.Double(25, 510, 10, 35, 105, 180, Arc2D.OPEN));
    blades.add(new Arc2D.Double(35, 510, 10, 35, 105, 180, Arc2D.OPEN));
    blades.add(new Arc2D.Double(45, 520, 10, 35, 105, 180, Arc2D.OPEN));
    blades.add(new Arc2D.Double(10, 520, 10, 35, 105, 180, Arc2D.OPEN));
    blades.add(new Arc2D.Double(20, 520, 10, 35, 105, 180, Arc2D.OPEN));
    blades.add(new Arc2D.Double(30, 520, 10, 35, 105, 180, Arc2D.OPEN));
    blades.add(new Arc2D.Double(40, 520, 10, 35, 105, 180, Arc2D.OPEN));
    blades.add(new Arc2D.Double(50, 520, 10, 35, 105, 180, Arc2D.OPEN));
}

然后只需在paintComponent方法中迭代列表......

Color yellowGreen = new Color(107, 142, 35);
g2.setColor(yellowGreen);
for (Shape blade : blades) {
    g2.draw(blade);
}

现在,另一种选择可能是创建一个BufferedImage并绘制&#34;静态&#34;它的元素,只需在调用paintComponent时绘制图像。

此外,您必须在进行任何自定义绘画之前调用super.paintComponent,否则您将以奇怪的绘画工件结束。

您可以通过执行以下操作来进一步减少它...

blades = new ArrayList<>(25);
for (int x = 10; x < 50; x += 10) {
    blades.add(new Arc2D.Double(x, 500, 10, 35, 105, 180, Arc2D.OPEN));
}
for (int x = 5; x < 45; x += 10) {
    blades.add(new Arc2D.Double(x, 510, 10, 35, 105, 180, Arc2D.OPEN));
}
for (int x = 10; x < 50; x += 10) {
    blades.add(new Arc2D.Double(x, 520, 10, 35, 105, 180, Arc2D.OPEN));
}

甚至......

for (int x = 10; x < 200; x += 10) {
    blades.add(new Arc2D.Double(x, 500, 10, 35, 105, 180, Arc2D.OPEN));
    blades.add(new Arc2D.Double(x - 5, 510, 10, 35, 105, 180, Arc2D.OPEN));
    blades.add(new Arc2D.Double(x, 520, 10, 35, 105, 180, Arc2D.OPEN));
}