I have found this class that draws circles with different colors. The color of each circle is determined according to a specific order of colors which iterates as it comes to the end (having used all colors by one time). I want to modify this on a way that grants me the potential to determine individually the color (on g.setColor) for each circle. In other words, I want to be able to deploy the color as a parameter and to invoke the method from another method in another class.
public class DrawingPane extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Rectangle rect;
for (int i = 0; i < circles.size(); i++) {
rect = circles.elementAt(i);
g.setColor(colors[(i % color_n)]);
g.fillOval(rect.x, rect.y, rect.width, rect.height);
}
}
}
If you find my question stupid I would like to let you know that what worries me is the fact that the method is inherited from JPanel and I am not sure how to override it effectively.
Your
DrawingPaneseems to have a list ofRectanglenamedcircles(sic). I don’t know ifRectangleis one of your classes or the standardjava.awt.Rectangle.If it’s one of your class, then simply add a
colorattribute to this class, and get this attribute from it during your iteration.If it’s the standard
java.awt.Rectangle, then introduce aCircleclass, containing a Rectangle and a color, and use a list ofCirclerather than a list ofRectangle.