简体   繁体   English

如何在JFrame上正确显示PNG

[英]How to display a PNG properly on JFrame

How would I go about displaying a png with transparent parst on JFrame. 如何在JFrame上显示带透明parst的png。
I currently have this code and all is good, but it displays the transparent parts grey! 我目前有这个代码,一切都很好,但它显示透明部分灰色!

private static void createAndShowSplashScreen() throws Exception {
    Image image = ImageIO.read(TCPServer.class
            .getResource("images/splash.png"));
    BufferedImage img = (BufferedImage) image;

    frame = new JFrame("Splash");
    frame.setUndecorated(true);
    frame.add(new JLabel(new ImageIcon(image)) {
        {
            setOpaque(false);
        }
    });
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));
    frame.setBounds((int) (java.awt.Toolkit.getDefaultToolkit()
            .getScreenSize().getWidth() / 2 - img.getWidth() / 2),
            (int) (java.awt.Toolkit.getDefaultToolkit().getScreenSize()
                    .getHeight() / 2 - img.getHeight() / 2),
            img.getWidth(), img.getHeight());
    frame.setOpacity(0f);
    frame.setMinimumSize(frame.getPreferredSize());
    frame.setVisible(true);
}

THX for the help! THX的帮助! Sincerely Roberto! 真诚的罗伯托!

EDIT: IMG 编辑: IMG

PROBLEM SOLVED: 问题解决了:

Image image = ImageIO.read(TCPServer.class
            .getResource("images/splash.png"));
    BufferedImage img = (BufferedImage) image;

    frame = new JFrame("Splash");
    frame.setUndecorated(true);
    frame.add(new JLabel(new ImageIcon(image)) {
        {
            setOpaque(false);
        }
    });
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));
    frame.setBounds((int) (java.awt.Toolkit.getDefaultToolkit()
            .getScreenSize().getWidth() / 2 - img.getWidth() / 2),
            (int) (java.awt.Toolkit.getDefaultToolkit().getScreenSize()
                    .getHeight() / 2 - img.getHeight() / 2),
            img.getWidth(), img.getHeight());
    RoundRectangle2D r = new RoundRectangle2D.Double(0, 0, img.getWidth(), img.getHeight(), 25, 25);
    frame.setShape(r);
    frame.setOpacity(0f);
    frame.setMinimumSize(frame.getPreferredSize());
    frame.setVisible(true);

I do not know if this suits your particular case but how about changing the shape of the JFrame to match the image ? 我不知道这是否适合您的特定情况但是如何更改JFrame的形状以匹配图像? The setShape(Shape shape) lets you use any shape from the geom package. setShape(Shape shape)允许您使用geom包中的任何形状。 I see that this is a splash screen and will be undecorated, probably, so setting the shape should do the trick. 我看到这是一个闪屏并且可能是未修饰的,所以设置形状应该可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM