加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java Swing中的错误PNG渲染(低颜色深度)

发布时间:2020-12-15 04:59:48 所属栏目:Java 来源:网络整理
导读:我正在使用swing在 java中设计Pac Man.我使用以下语句在屏幕上绘制PNG图像. wall = new ImageIcon(GamePanel.class.getResource("wall.png")).getImage();g2d.drawImage(wall,x,y,this); 我遇到的问题是它似乎呈现了实际文件的非常低的颜色深度再现.它似乎确
我正在使用swing在 java中设计Pac Man.我使用以下语句在屏幕上绘制PNG图像.

wall = new ImageIcon(GamePanel.class.getResource("wall.png")).getImage();
g2d.drawImage(wall,x,y,this);

我遇到的问题是它似乎呈现了实际文件的非常低的颜色深度再现.它似乎确实保持透明度(灰色背景是Panel bg颜色),但它失去了颜色深度.

实际图像如下所示:运行时,它看起来像这样:

有没有人有办法解决吗?谢谢!

解决方法

第二张图片中似乎有些错误.在黑色BG上看到它看起来非常不同 – 没有’光环’.

import java.awt.*;
import java.net.URL;
import javax.swing.*;

public class TestYellowDotImage {

    public static JLabel getColoredLabel(Icon icon,Color color) {
        JLabel label = new JLabel(icon);
        label.setBackground(color);
        label.setOpaque(true);

        return label;
    }

    public static void main(String[] args) throws Exception {
        URL url = new URL("http://i.stack.imgur.com/1EZVZ.png");
        final Icon icon = new ImageIcon(url);
        Runnable r = new Runnable() {

            @Override
            public void run() {
                JPanel gui = new JPanel(new GridLayout(0,4));

                gui.add(new JLabel(icon));
                gui.add(getColoredLabel(icon,Color.BLACK));
                gui.add(getColoredLabel(icon,Color.WHITE));
                gui.add(getColoredLabel(icon,Color.RED));

                JOptionPane.showMessageDialog(null,gui);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读