GIF / PNG图像的透明部分在JLabel java中以黑色显示

GIF / PNG图像的透明部分在JLabel java中以黑色显示

问题描述:

我有一张图像(gif或png),其中有一些透明的部分,当放入JLabel时会显示为黑色。

I have an image (gif or png) with some transparents parts which appear in black when put inside a JLabel.

    ClassLoader cl = this.getClass().getClassLoader();
    ImageIcon img = new ImageIcon(cl.getResource("resources/myPicture.png"));
    label = new JLabel(img);

如何解决此问题?

我不需要JLabel,也许有更好的方法可以直接在JPanel上正确显示图像(即透明度)?

I do not need the JLabel, maybe there is a better way to display the image correctly (i.e. with the transparency) directly on a JPanel ?

谢谢
David

Thanks David

找到了罪魁祸首!

实际上图片在添加到JLabel之前已经重新调整,为此,我使用了BufferedImage.TYPE_INT_RGB而不是BufferedImage.TYPE_INT_ARGB

Actually the picture is getting rescaled before being added to the JLabel and for that, I used BufferedImage.TYPE_INT_RGB instead of BufferedImage.TYPE_INT_ARGB

我真的不认为重新缩放方法可以改变这个(愚蠢的我!),这就是为什么我没有在我添加到问题的代码中显示它...

I really didn't think that the rescaling method could alter this (silly me!), that's why I didn't show it in the code I added to the question...

大卫