Question

I am trying to display several PNG Images on a JLayeredPane with overlapping.

Here is the current code for generating the JLabel containing the image and adding it to the JLayeredPane :

BufferedImage im = ImageIO.read(new File(fname));
JLabel uLabel = createLabelForImage(im);
mapLayeredPane.add(uLabel, new Integer(zIndex++), 1);

And the createLabelForImage() method :

protected JLabel createLabelForImage(BufferedImage im) {
    JLabel label = new JLabel(new ImageIcon(im));
    label.setVerticalAlignment(JLabel.TOP);
    label.setHorizontalAlignment(JLabel.CENTER);
    label.setOpaque(true);
    label.setBounds(0, 0, im.getWidth(), im.getHeight());

    return label;
}

What I get is the objects are correctly overlapping but the transparent background of the PNG images is replaced with a kind of white-gray color.

Is it a problem with ImageIcon ? With JLayeredPane ? Maybe I have to set something like label.setBakground(sort_of_transparency_code) or something like that ?

Thanks in advance :)

Was it helpful?

Solution

Your label should not be opaque if you want to see through, see JComponent.setOpaque

 If true the component paints every pixel within its bounds. 
 Otherwise, the component may not paint some or all of its
 pixels, allowing the underlying pixels to show through.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top