Here’s my code:
public class JavaApplication7 extends JPanel
{
public static void main(String[] args) {
JPanel pan = new JPanel();
JFrame frm = new JFrame();
frm.add(new JavaApplication7());
frm.setSize(500, 500);
frm.setBackground(Color.yellow);
frm.setForeground(Color.red);
frm.setVisible(true);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pan.setSize(768,512);
pan.setVisible(true);
}
public void paintComponent (Graphics g){
g.drawOval(50, 50, 50, 50);
g.setColor(Color.CYAN);
}
}
I want the Oval to change color but when I run my program it remains black instead of cyan that I want.
You first draw the oval and then change the color. That’s why you don’t see the effect.
Just change the order of statements: