灰色闪屏由于没有重新粉刷 [英] Grey splash screen due to no repainting

查看:68
本文介绍了灰色闪屏由于没有重新粉刷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用程序制作启动画面。它只是由Graphics2D绘制的静态BufferedImage,在JFrame中没有装饰。我的问题是:窗口有时没有正确绘制,这意味着它并不总是包含我的图像,它有时只是灰色。我尝试在第二个线程中创建了启动画面,但它没有帮助。我可以每行调用 splashScreen.repaint(),但这是无稽之谈......这是我的代码:

I'm making a splash screen for my application. It's just static BufferedImage drawn by Graphics2D, inside JFrame with no decorations. My problem is: the window sometimes isn't drawn properly, it means it doesn't always contain my image, it's sometimes just grey. I tried already creating splash screen in second thread, but it didn't help. I could call splashScreen.repaint() every line, but it's nonsense... Here's my code:

package SeriousSteve.MapEditor;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

/**
 * Simple splash screen, contains only the image.
 * 
 * @author m4tx3
 */
public class SplashScreen extends JFrame {

    private BufferedImage image;

    /**
     * Constructor. Creates a window with splash screen, loads an image at the
     * URL specified in imageURL, and finally displays this image.
     * 
     * @param imageURL  URL to the image that you want to put on the splash
     *                  screen.
     */
    public SplashScreen() {
        super("Splash screen");
    }

    public void createSplashScreen(final URL imageURL) {
        try {
            image = ImageIO.read(imageURL);
        } catch (IOException ex) {
            Logger.getLogger(SplashScreen.class.getName()).
                    log(Level.SEVERE, null, ex);
        }
        setUndecorated(true);
        setSize(image.getWidth(), image.getHeight());
        setLocationRelativeTo(null);
        setVisible(true);

        createGraphics();

        repaint();
    }

    /**
     * Creates a graphics context and draws a splash screen.
     */
    private void createGraphics() {
        Graphics2D g = (Graphics2D) getGraphics();
        g.drawImage(image, 0, 0, null);
    }

    /**
     * Closes the splash screen and frees the memory taken by the image.
     * 
     * Call this function when the loading is complete.
     */
    public void close() {
        setVisible(false);
        image = null;
        dispose();
    }
}

并且:

    SplashScreen splashScreen = new SplashScreen();
    splashScreen.createSplashScreen(getClass().getResource(
            "Img/splash.png"));

Btw。, - 我正在创建自己的启动画面类,因为我有2个应用程序(游戏和地图编辑器)在1个jar中...我想只在地图编辑器中显示启动画面,所以我无法修改清单文件。

Btw., - I'm creating my own splash screen class, because I've got 2 apps (the game and map editor for it) in 1 jar... I want to show splash screen only in map editor, so I can't modify manifest file.

问候

推荐答案

改为使用 JLabel 。直接在画框上画画是一个坏主意。

Use a JLabel instead. Painting on the frame directly is a bad idea.

看到这个,它每次都有效:

See this, it works everytime:

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

/**
 * Simple splash screen, contains only the image.
 * 
 * @author m4tx3
 */
public class SplashScreen extends JFrame {

    /**
     * Constructor. Creates a window with splash screen, loads an image at the URL specified in imageURL, and finally displays this image.
     * 
     * @param imageURL
     *            URL to the image that you want to put on the splash screen.
     */
    public SplashScreen() {
        super("Splash screen");
    }

    public void createSplashScreen(final URL imageURL) {

        JLabel splashLabel = new JLabel(new ImageIcon(imageURL));
        add(splashLabel);
            setSize(splashLabel.getPreferredSize());
        setUndecorated(true);
        setLocationRelativeTo(null);
        setVisible(true);

        repaint();
    }

    /**
     * Closes the splash screen and frees the memory taken by the image.
     * 
     * Call this function when the loading is complete.
     */
    public void close() {
        setVisible(false);
        dispose();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    new SplashScreen().createSplashScreen(new URL(
                            "http://art.gnome.org/download/themes/splash_screens/1334/Splash-GnomeDarkSplashScreen.png"));
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }
}

这篇关于灰色闪屏由于没有重新粉刷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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