Reputation: 471
I'd like to know how you are supposed to override a paint method for each panel in the same class and how to call them separately?
I only know about the repaint() call when you are in a class that extends JPanel (so in only one panel), not when you just make panels.
Thanks in advance.
Upvotes: 0
Views: 2220
Reputation: 7910
I think the normal thing is to extend JPanel for each unique panel you wish to create. In other words, each panel you create is its own class. Then you can overwrite the paint method for each individually.
Upvotes: 0
Reputation: 2920
Typically you create a class that extends JPanel
to override the paintComponent
method:
public class Test extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// code here
}
public void doStuff() { repaint(); }
}
You might consider creating a nested class like so:
public class Test {
public class MyPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// code here
}
}
JPanel panel = new MyPanel();
panel.repaint();
}
Or you can do this without creating a class that extends JPanel
:
JPanel panel1 = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// code here
}
};
panel1.repaint();
Upvotes: 1