[英]How to write a splash screen?
我正在使用Java開發游戲。
目前,我有幾節課。 重要的是:
最后,我想讓MainMenu在JFrame上繪制一個初始屏幕,然后在5秒鍾后將LevelBuilder繪制到原始JFrame內,而不創建一個新的JFrame。
抱歉,如果這是一個基本問題,我剛剛開始學習Java。
好吧,可以通過清單將閃屏簡單地添加到您的jar中。
問題是默認情況下,它僅在需要加載Swing應用程序的情況下才會顯示。 因此,第二(第三,第四等)執行會快速顯示初始屏幕,因為已經加載了JVM和GUI使用的類等。
在我的游戲中,為了創建飛濺更長時間,我有以下兩種方法:
/**
* This will render the splash for longer than just loading components
*
* @return true if there is a splash screen file supplied (set via java or
* manifest) or false if not
* @throws IllegalStateException
*/
private boolean showSplash() throws IllegalStateException {
final SplashScreen splash = SplashScreen.getSplashScreen();
if (splash == null) {
return false;
}
Graphics2D g = splash.createGraphics();
if (g == null) {
return false;
}
for (int i = 0; i < 100; i++) {//loop 100 times and sleep 50 thus 100x50=5000milis=5seconds
renderSplashFrame(g);
splash.update();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
}
splash.close();
return true;
}
private void renderSplashFrame(Graphics2D g2d) {
//draw anyhting else here
}
這將被稱為:
JFrame frame=...;
...
//show splash
if (!showSplash()) {
JOptionPane.showMessageDialog(null, "SplashScreen could not be shown!", "Splash Error: 0x003", JOptionPane.ERROR_MESSAGE);
}
// set JFrame visible
frame.setVisible(true);
frame.toFront();
請注意,因為showSplash()
表示如果沒有提供初始屏幕(即您尚未在清單中添加一個showSplash()
,它將返回false。
如果您還沒有的話,我也建議您閱讀如何創建啟動畫面 。
另請參見其他類似的答案/問題: 使用進度條(如Eclipse)制作啟動屏幕
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.