灰色闪屏由于没有重新粉刷

时间:2012-05-09 17:35:18

标签: 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个应用程序(游戏和地图编辑器)...我想只在地图编辑器中显示启动画面,所以我无法修改清单文件。

此致

3 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:4)

1)g.drawImage(image, 0, 0, null);

应该是

g2d.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), SplashScreen.this);

code from

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;

public class SplashScreen extends JWindow {
  private JProgressBar bar;
  private JLabel label;

  public SplashScreen(final BufferedImage img) {
    JPanel panel = new JPanel() {
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();

        g2d.drawImage(img, 0, 0, img.getWidth(), img.getHeight(),
            SplashScreen.this);
      }
    };
    panel.setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));

    Container content = getContentPane();
    content.setLayout(new BorderLayout());
    content.add(panel, BorderLayout.NORTH);
    content.add(label = new JLabel(), BorderLayout.CENTER);
    content.add(bar = new JProgressBar(), BorderLayout.SOUTH);
    pack();
    setLocationRelativeTo(null);
  }

  public void setMessage(String msg) {
    label.setText(msg);
    pack();
  }

  public void setProgress(int prog) {
    bar.setValue(prog);
  }

  public void setIndeterminateProgress(boolean value){
    bar.setIndeterminate(value);
  }
}

2)我建议使用JWindowundecorated JDialog而不是JFrame (from tutorial)

答案 2 :(得分:0)

覆盖paint方法,而不是创建自己的Graphics

@Override
public void paint(Graphics g) {
    g.drawImage(image, 0, 0, null);
}

每次拨打repaint时,它都会调用此方法。默认情况下,此方法会使用背景颜色(灰色)填充JFrame