I have to draw two circles many times, each time with a different pair of colors. So I'd like to pass the colors as arguments to the paintComponent method. But if I do so, the method of the super class JPanel won't be overriden. What should I do? Here's my code:
public class Test extends JPanel{
Ellipse2D oval;
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
oval = new Ellipse2D.Double(137, 0, 40, 40);
g2.setPaint(Color.black); //color I want to pass as argument
g2.fill(oval);
oval = new Ellipse2D.Double(420, 0, 40, 40);
g2.setPaint(Color.red); //color I want to pass as argument
g2.fill(oval);
}
}
I want to pass the colors when calling the constructor:
public class MyFrame extends JFrame {
Test t1, t2;
public MyFrame(){
//setSize, setTitle...
t1 = new Test(); // would pass the colors in here
t2 = new Test(); // would pass the colors in here
add(t1);
add(t2);
setVisible(true);
}
}
Pass the colors in the constructor and save them in a class member variable. Then over write paint component to use those colors to draw your circle
so in the Test class add this code
private java.awt.Color insideColor;
private java.awt. Color outsideColor;
public class Test (java.awt.Color inside, java.awt.Color outside){
this.insideColor = inside;
this.outsideColor = outside;
}