我有一个ArrayList的形状(矩形和椭圆形),我想绘制这些形状。 如何在for循环中用颜色填充它们?我的ArrayList由矩形和椭圆两种组成。如果我使用fillRect(color),则将的所有形状绘制为矩形,如果我使用fillOval(color),则将所有形状绘制为椭圆形。我该如何适当填充椭圆和矩形?下面的代码只是大纲。Java:如何用颜色填充ArrayList的形状?
private ArrayList<Shape> shapes = new ArrayList<Shape>();
private Shape currentShape; // the shape being drawn (either Rectangle or Oval)
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(Shape s : shapes) {
Graphics2D g2d = (Graphics2D) g.create();
s.paint(g2d);
}
}
事实上,你的'形状'被保存在一个ArrayList中是完全不相关的 - 你的问题实际上是“如何用颜色填充一个形状”。 –
'Graphics2D g2d =(Graphics2D)g.create();'应该**在循环外**。 –