aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Component on Image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Component on Image" Watch "Component on Image" New topic
Author

Component on Image

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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...


GenRocket - Experts at Building Test Data
Rene Liebmann
Ranch Hand

Joined: May 02, 2002
Posts: 196
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

Joined: Jul 11, 2001
Posts: 15299
    
    6

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

Joined: Oct 18, 2000
Posts: 4121

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...



-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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

Joined: Oct 18, 2000
Posts: 4121

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
 
subject: Component on Image