透明 BufferedImage 在 JLabel 上绘制时显示为黑色背景

发布于 2024-10-06 07:12:32 字数 1265 浏览 5 评论 0原文

我有一个从 png 文件创建的 BufferedImage。创建它时,我将类型设置为 TYPE_INT_ARGB,这应该给我一个透明图像。当我在 JPanel 中使用 PaintComponent 来绘制图像时,我得到的图像具有黑色背景。我真的需要让它透明,这样任何帮助都会有用。为了清楚起见,这里是代码:

public class ImagePanel extends JPanel {      

    private static final long serialVersionUID = 1L;
    private BufferedImage image; 

    public ImagePanel() {
        this.image = null;
    }


    public void createImage(String fileName) {
        this.image = ImageUtilities.getBufferedImage(fileName, this);
        this.repaint();

     }

    public void paint(Graphics g) {
        g.drawImage(this.image, 0, 0, this);
    }
}

这是我加载图像的方式:

public class ImageUtilities {

/** Create Image from a file, then turn that into a BufferedImage.
*/

   public static BufferedImage getBufferedImage(String imageFile, Component c) {
       Image image = c.getToolkit().getImage(imageFile);
       waitForImage(image, c);
       BufferedImage bufferedImage = new BufferedImage(image.getWidth(c), image.getHeight(c),
                    BufferedImage.TYPE_INT_ARGB);
       Graphics2D g2d = bufferedImage.createGraphics();
       g2d.drawImage(image, 0, 0, c);
       return(bufferedImage);
   }

最后要添加的一件事是,此 ImagePanel 位于另一个面板内,如果这有任何意义的话。

I've got a BufferedImage which is created from a png file. When creating it I set the type to be TYPE_INT_ARGB which should give me a transparent image. When I use paintComponent inside a JPanel to paint the image, I get the image with a black background. I really need to get it transparent so any help will be useful. Here is the code for clarity:

public class ImagePanel extends JPanel {      

    private static final long serialVersionUID = 1L;
    private BufferedImage image; 

    public ImagePanel() {
        this.image = null;
    }


    public void createImage(String fileName) {
        this.image = ImageUtilities.getBufferedImage(fileName, this);
        this.repaint();

     }

    public void paint(Graphics g) {
        g.drawImage(this.image, 0, 0, this);
    }
}

Here is how I load the image:

public class ImageUtilities {

/** Create Image from a file, then turn that into a BufferedImage.
*/

   public static BufferedImage getBufferedImage(String imageFile, Component c) {
       Image image = c.getToolkit().getImage(imageFile);
       waitForImage(image, c);
       BufferedImage bufferedImage = new BufferedImage(image.getWidth(c), image.getHeight(c),
                    BufferedImage.TYPE_INT_ARGB);
       Graphics2D g2d = bufferedImage.createGraphics();
       g2d.drawImage(image, 0, 0, c);
       return(bufferedImage);
   }

And one last thing to add is that this ImagePanel is inside another Panel, if that has any significance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

新一帅帅 2024-10-13 07:12:32

不确定这是否能解决您的问题,但是:

Not sure if this will solve your problem, but:

油焖大侠 2024-10-13 07:12:32

您是否被限制使用旧版本的 Java?尝试使用 ImageIO.read(fileName) 加载图像文件。

Are you restricted to using an older version of Java? Try using ImageIO.read(fileName) to load the image file.

俏︾媚 2024-10-13 07:12:32

试试这个(即 setComposite()):

g2d.setComposite(AlphaComposite.SrcOver);
g2d.setPaint(backgroundColor);
g2d.fillRect(0, 0, w, h);

Try this (i.e. setComposite()):

g2d.setComposite(AlphaComposite.SrcOver);
g2d.setPaint(backgroundColor);
g2d.fillRect(0, 0, w, h);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文