I code the following basic Java Swing example which is a JPanel with blue background:
public class Chart1 extends JFrame {
private MainPanel1 main;
public Chart1() throws InterruptedException {
setSize(600,500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// CREATE AND ADD MAIN PANEL
main = new MainPanel1();
this.getContentPane().add(main, BorderLayout.CENTER);
// DRAW WINDOW
this.setVisible(true);
}
}
public class MainPanel1 extends JPanel {
public MainPanel1() {
setBackground(Color.BLUE);
}
}
I get the following result:
So far, so good.
Now I add a paint()
method. The source code is as follows:
public class MainPanel1 extends JPanel {
public MainPanel1() {
setBackground(Color.BLUE);
}
public void paint(Graphics g) {
}
}
Then even without doing anything in paint()
I get a grey background, why? how can I fix this?