wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Images on top of each other Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Images on top of each other" Watch "Images on top of each other" New topic
Author

Images on top of each other

Andreas Bystrom
Ranch Hand

Joined: Mar 05, 2003
Posts: 32
Hi all!
I have a question of how I place several images on top of each other. I have an application and I place a background image in it. Then I want to have images "on top" of the background imgage. All images is in the format .png, which can be transparent.
My problem is that when I add an transparent image, I dont see the background image through it. Instead I get the default background color (something grey/blue).
Can somebody help me with:
1) How do I put the background picture in my JFrame (I'm not sure I'm doing this correctly)
2) How do I display .png images in the JFrame so that I see the background through it?
Regards,
Andreas
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

The same component should display all the images, and if you are using any buffered images to hold the PNGs that go "on top" you should make sure that they support the same color model and transparency as the PNGs. How are you doing multilayered images? Are you painting all the images from one component's paint() or paintComponent() method?


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Andreas Bystrom
Ranch Hand

Joined: Mar 05, 2003
Posts: 32
I do nothing special actually. Do you have an suggestion how to add the image image3.png in the code below?

When I try to add an image there, the transparent part of the imgae shows as grey.
Regards,
Andreas
Andreas Bystrom
Ranch Hand

Joined: Mar 05, 2003
Posts: 32
I got it to work (I got the transparent image to show with transparent), I added this code:

My problem now is how I can place this picture wherever I want, without having to use a LayouitManager (unless there is a layout manager that enable you to do this)
Any ideas?
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

You could get the image by using the ImageIcon.getImage() method.

The component that you posted code for only allows you to add one image to the panel... a background image. To support multiple images, you'd have to add more methods to set the "on top" images, and modify the paint method to paint the background and then all the "on top" images. Take a look ath the following code. I tested it out with a PNG with transparency, and it worked:
 
Don't get me started about those stupid light bulbs.
 
subject: Images on top of each other
 
Similar Threads
SWT Composite setBackgroundImage() doesn't support transparency?
variable amount of images, layering images and transparency
Question regarding older JDK and Transparent images
Image as Transparent to be placed in the JFrame
Did older Java have problems with Transparent Images?