GIF / PNG图像的透明部分在JLabel java中以黑色显示 [英] transparent parts of GIF/PNG image shown in black inside a JLabel java

查看:140
本文介绍了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...

大卫

这篇关于GIF / PNG图像的透明部分在JLabel java中以黑色显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆