This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Showing image in JPanel - it disappears  RSS feed

 
Ben Hodgson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to show an image in a JPanel.
I've obtained a Graphics object okay, using myPanel.getGraphics(). I then cast it to a Graphics2D object (g2), and call the following:
g2.drawImage( anImage, 0, 0, 400, 400, myPanel );
The image draws on the panel, BUT:
1. There is a hole where a message box was sitting (it's not painting properly) - how do I fix this?
2. When I move the window containing my JPanel, the image disappears and I'm left with an empty JPanel.
I'd appreciate any help in fixing this.
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ben,
It sounds like you aren't placing your drawing code into an overridden paint routine. The paint method gets called in all the cases you described in your post.
Regards,
Manfred.
 
Ben Hodgson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Manfred, very much appreciated - I subclassed JPanel, created my panel (within the JFrame) from the subclass and drew the image in the overridden paint() method of my subclass. Both problems were solved by doing this, I'm so happy!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!