我正在为我的应用程序启动启动画面。它只是由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个应用程序(游戏和地图编辑器)...我只想在地图编辑器中显示启动画面,所以我无法修改清单文件。
问候
我不得不添加'drawComponents(getGraphics());',但现在它工作!非常感谢:) – m4tx