Java drawing images outside of JPanel class
I have a WorldManager class that extends a JPanel and has:
public void pai开发者_开发问答nt(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(); }
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()
andpaintChildren()
method can do their painting of the border and the child components, and you are free to think only about the real content.- Your other paint methods should also take a Graphics parameter (or Graphics2D, if you need this and want to cast only once), and then be invoked.
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?
精彩评论