Reputation: 136
I am trying to draw several things inside a JPanel
. I am drawing shapes (works), and I also want to fill the JPanel with a picture.
But paintComponent()
only takes one argument. And this gets complicated when I have some extra code for drawing the shapes.
My paintComponent() method is currently like this:
public void paintComponent(Graphics g) {
g2 = (Graphics2D) g;
for (int i = 0; i < shapes.size(); i++) {
Shape s = (Shape) shapes.get(i);
if (s != null)
g2.draw(s);
}
}
I have searched around a lot and cannot find a way to do this.
Does anyone know how to do this, or maybe some workaround?
Upvotes: 1
Views: 1047
Reputation: 347204
Just like your approach to drawing shapes, you need to maintain a reference to the images you want to draw within the class and reference them in much the same way.
The following are all examples of drawing images within paintComponent
, on a verity of topics
Nb - I may be misreading this, but you should never be calling paintComponent
yourself. It is called on your behalf by the repaint engine within Swing
Upvotes: 1