File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Background image for a Swing GUI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Background image for a Swing GUI" Watch "Background image for a Swing GUI" New topic
Author

Background image for a Swing GUI

Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
If I wanted to have a background image for my whole Swing application, what are my options? Is there some sort of a standard approach for adapting to the window getting resized?

From my initial googling around, I see that there's a thing called glass pane that could be a solution but it seems like there's a lot of work involved in passing mouse/key events from the glass pane to the components behind the glass pane. Is that the case or am I just misinterpreting the examples I've found online?

Thanks.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

What do you mean "my whole Swing application"? Do you just need a background image on a JPanel? If that is the case then just override the paintComponent method of your JPanel and draw the image there. Otherwise, I'll need a bit more info about what you need.


GenRocket - A Test Data Generation Platform
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Thanks, Gregg. Yes, I'm talking about just having a JPanel with a background image. How would I go about rendering stuff on top of the background image? Is it so simple as to calling super.paintComponent()?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Yes, the first line in your paintComponent method will always be a call to super.paintComponents.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Thanks again. This seems to work:

Why does the call to super.paintComponents(Graphics) need to be the first line? Shouldn't that effectively draw all child elements and then draw the background image on top of them? It doesn't, but that's what my intuition would say.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Yea, I don't know the answer to that Lasse. It's just what I was always told and too often I guess when something works, you tend to not question it.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Nope, it's because the paint() method is the real control behind all of this... it calls paintComponent() and then paintChildren(). Calling super.paintComponent() just works out a lot of basic Swing painting stuff for painting this component, things like setting opacity, default color, etc.
[ April 11, 2005: Message edited by: Nathan Pruett ]

-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

I went back and read your initial post and realized you also had a question about resizing. As far as scaling the image on resize, you could have an internal ComponentListener class on the panel to listen for when it is resized, and scale and re-position the image accordingly.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Nathan Pruett:
Nope, it's because the paint() method is the real control behind all of this... it calls paintComponent() and then paintChildren(). Calling super.paintComponent() just works out a lot of basic Swing painting stuff for painting this component, things like setting opacity, default color, etc.

That makes sense. Thanks!
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Yes, thanks Nathan. I kind of miss you hanging out in this forum answering everyone's questions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Background image for a Swing GUI
 
Similar Threads
Why Container?
Can't clear glassPane
GlassPane displays flickering
Responsibility for buffering?
How to place components beneath tabs in right oriented JTabbedPane