Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Background image for a Swing GUI

 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the first line in your paintComponent method will always be a call to super.paintComponents.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, thanks Nathan. I kind of miss you hanging out in this forum answering everyone's questions.
 
Arch enemy? I mean, I don't like you, but I don't think you qualify as "arch enemy". Here, try this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic