Reputation: 861
I have a WorldManager class that extends a JPanel and has:
public void paint(Graphics g) {}
What I would like to do is have separate classes, like a world class with its own paint method and be able to just call that classes paint method like so:
public void paint(Graphics g) { world1.paint(); hero.paint(); }
Upvotes: 1
Views: 1126
Reputation: 74750
In principle, there is nothing wrong with your approach.
As trashgod noted, you should overwrite the paintComponent method instead of the paint method.
The reason for this is noted in the article linked by trashgod: this way, the paintBorder()
and paintChildren()
method can do their painting of the border and the child components, and you are free to think only about the real content.
Here is an example:
class WorldManager extends JPanel
{
private World world1;
private Person hero;
public void paintComponent(Graphics g) {
super.paintComponent(); // paints the background, if opaque
world.paint(g);
hero.paint(g);
}
}
So, what was your question, actually?
Upvotes: 3