java - 由于没有重新粉刷,灰色启动画面

标签 java image swing splash-screen graphics2d

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

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"));

顺便说一句,- 我正在创建自己的启动画面类,因为我在 1 个 jar 中有 2 个应用程序(游戏和 map 编辑器)...我只想在 map 编辑器中显示启动画面,所以我不能修改 list 文件。

问候

最佳答案

改用JLabel。直接在框架上绘画是个坏主意。

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

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();
                }
            }
        });
    }
}

关于java - 由于没有重新粉刷,灰色启动画面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10521192/

相关文章:

java - 无法使用 Apache POI 删除行,因为单元格包含电子邮件地址

java - ReSTLet 2.3 如何设置临时目录

java - 如何将对象设置在画面中央?

java - 检查文本退格问题

JavaFX数据库应用指南

java - 尝试实现一些伪代码、算法

ios - 访问核心数据镜像中的外部数据引用属性

java - 查找并消除轮廓opencv

android - 单击 RecyclerView 中的名称字段时显示刻度线图像?

java - 无法设置面板的背景颜色