QUOTE(bastones @ 11 Jan, 2009 - 12:07 PM)

When I am extending the JPanel I have to super.paintComponent(g) - I know that it is required because a panel like JPanel is opaque but what is it really used for? I don't understand that the code itself paints the panel with the background colour - how does it lose it in the first place? Is there a method in JComponent that the JPanel inherits from JComponent class that makes the JPanel opaque but loses it? If this is true does it lose it as we're using the paintComponent class or as we're extending the JPanel?
Any help is appreciated...
Cheers
You do not always have to call super.paint(g)
you only have to do it when you draw something.
Let's have an example.
CODE
class MyPanel extends JPanel {
MyPanel() {
add(new JLabel("Hello");
add(new JButton("OK");
... addd other JComponent
}
}
In that case you do not have to overload paint. Panel has a method call paint() that "knows" how to draw JLabel and JButton
Now if you draw something like Oval and Rectangle then you have to overload paint
CODE
class MyPanel extends JPanel {
MyPanel() {
add(new JLabel("Hello");
add(new JButton("OK");
... addd other JComponent
}
// I am overloading piant to do my own stuff
public void paint(Graphics g) {
// now that I have overload paint... do I know how to draw the JLabel and JButton ?
// obviously not. So let the super method do the job
super.paint(g);
// now that the JLabel and JButton are paint les me do my own drawing/animation
g.drawOval(......
}
}
So you have to overload and call super if you have "standard" components (that you don't know how to paint) and other
Now if your code is 100% your own drawing... you do not need to call super, just do your drawing
CODE
class MyPanel extends JPanel {
MyPanel() {
}
// I am overloading paint to do my own stuff there are no "standard" GUI elements to draw
public void paint(Graphics g) {
// now that the JLabel and JButton are paint les me do my own drawing/animation
g.drawOval(......
}
}
Hope this help