2014年1月16日 星期四

[ Java 常見問題 ] Swing: How to Create a Splash Screen

來源自 這裡 
Preface: 
Almost all modern applications have a splash screen. Typically splash screens are used for the following purposes: 
Advertising a product
* Indicating to the user that the application is launching during long startup times
Providing information that is only needed once per visit

Fortunately, Java™ SE 6 provides a solution that allows the application to display the splash screen much earlier, even before the virtual machine starts. A Java application launcher is able to decode an image and display it in a simple non-decorated window. The splash screen can display any gif, png, or jpeg image, with transparency, translucency, and animation. The figure below represents an example of the Java application splash screen developed as an animated gif file. 
 

The SplashScreen class is used to close the splash screen, change the splash-screen image, obtain the image position or size, and paint in the splash screen. An application cannot create an instance of this class. Only a single instance created within this class can exist, and this instance can be obtained using the getSplashScreen()static method. If the application has not created the splash screen at startup through the command-line or manifest-file option, the getSplashScreen method returns null. 

Typically, a developer wants to keep the splash-screen image on the screen and display something over the image. The splash-screen window has an overlay surface with an alpha channel, and this surface can be accessed with a traditional Graphics2D interface. 

The following code snippet shows how to obtain a SplashScreen object, then how to create a graphics context with the createGraphics() method: 
  1. ...  
  2.         final SplashScreen splash = SplashScreen.getSplashScreen();  
  3.         if (splash == null) {  
  4.             System.out.println("SplashScreen.getSplashScreen() returned null");  
  5.             return;  
  6.         }  
  7.         Graphics2D g = splash.createGraphics();  
  8.         if (g == null) {  
  9.             System.out.println("g is null");  
  10.             return;  
  11.         }  
  12. ...  
Find the demo's complete code in the SplashDemo.java file. 

How to Use the Command-Line Argument to Display a Splash Screen 
To display a splash screen from the command line use the -splash: command-line argument. This argument is a Java application launcher option that displays a splash screen: java -splash:<file name> <class name> 
Try this: 
1. Compile the SplashDemo.java file.
2. Save the splash.gif image in the images directory.
3. Run the application from the command line with the following arguments: java -splash:images/splash.gif SplashDemo
4. Wait until the splash screen has been completely displayed.
5. The application window appears. To close the window choose File|Exit from the pop-up menu or click the X.
6. Change the splash screen name to a non-existent image, for example, nnn.gif. Run the application as follows: java -splash:images/nnn.gif SplashDemo
7. You will see the following output string: SplashScreen.getSplashScreen() returned null

How to Use a JAR File to Display Splash Screen 
If your application is packaged in a JAR file, you can use the SplashScreen-Image option in a manifest file to show a splash screen. Place the image in the JAR file and specify the path in the option as follows: 
  1. Manifest-Version: 1.0  
  2. Main-Class: <class name>  
  3. SplashScreen-Image: <image name>  
Try this: 
1. Compile the SplashDemo.java file
2. Save the splash.gif image in the images directory. 
3. 請建立一個目錄 ./bin 並將剛剛產生的 *.class 與 ./images/splash.gif 置於該目錄下. 
4. 使用右邊命令建立 jar 檔: jar cvfm test.jar MANIFEST.MF -C bin/ . <---產生 test.jar 
5. 使用壓縮軟體打開 test.jar 應該有檔案結構如下: 
 
6. 編輯 META-INF/MANIFEST.MF 為下面內容: 
  1. Manifest-Version: 1.0  
  2. Main-Class: SplashDemo  
  3. SplashScreen-Image: images/splash.gif  
7. Run the application: java -jar test.jar

Supplement: 
Java Gossip: 製作 Executable JAR

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...