Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Images on top of each other

 
Andreas Bystrom
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Andreas Bystrom
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic