Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Component on Image

 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, here is an easy one, but appearntly not for me. I have a JFrame with a background Image which is placed on the JFrame by overriding the paint(Graphics g) method because I wanted it as a Backgroud Image, not as a JLabel. So now I add a Button to the ContentPane of my JFrame and it does not show up until I click on the JButton. Here is my code...
 
Rene Liebmann
Ranch Hand
Posts: 196
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gregg,
if you want to have a background image on your frame, then you have to use a panel for it, so your frame has a BorderLayout and only your ImagePanel is added.
There is already a great toolkit available, which is able to have background images. It is free and you can download it here.
There you have to have a look to the class kiwi.ui.KPanel.
Hope this helps
Rene
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the Kiwi Link. It looks like a neat package. However, your first statement is not accurate. You can add a background image to a JFrame without using a JPanel. I did that. Whether or not I can make it work the way I need it to is the problem. I have temporarily used a JLayeredPane to achieve what I need until someone may be able to provide an answer to my original problem (if there is one ).
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can paint directly to the JFrame... but it causes problems because JFrame uses some special kind of painting to paint all it's contents. The best thing to do (in my opinion) would be to replace the content pane of the JFrame with a custom JPanel that paints your image...

 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nate! Works like a charm. But what is it about JFrame that causes this not to function correctly with my method. Just curious.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JFrame is one of the few "top-level" components in Swing... the paint() method calls other methods that control the painting of the content pane and all the components within it. To get that behavior, you have to call super.paint(g) within the overridden paint method. Unfortunately, some of the super.paint() code looks like it clears the screen by painting over it (probably some of the built-in double buffering done by Swing...), so if you paint the image *before* you call super.paint(g) it gets painted over, and so it looks like it never shows up. If you paint the image *after* you call super.paint(g) it paints on top of everything.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic