java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow javax.swing arrow Splash Screens and Mustang
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4090
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Splash Screens and Mustang E-mail
User Rating: / 6
PoorBest 

This Tech Tip reprinted with permission by java.sun.com

According to Wikipedia, a splash screen is a computer term for an image that appears while a program or operating system is loading. It provides the user with a visual indicator that the program is initializing. Prior to Java SE 6, (code name Mustang) you could only offer the behavior of a splash screen by creating a window at the start of your main method and placing an image in it. Although this worked, it required the Java runtime to be fully initialized before the window appeared. This initialization included AWT and typically Swing, so that it delayed the initial graphical display. With Mustang, a new command-line option makes this functionality much easier. It also displays the image more quickly to the user, that is, even before the Java runtime has started. Final inclusion of the feature is subject to JCP approval.

Command Line Option

If you run a program from the command line, you can generate a splash screen through the -splash command line switch. This functionality is most useful when you're using a script, batch file, or desktop shortcut to run the program. The command line switch is followed by an image name:

   java -splash:Hello.png HelloWorld

Yes, that is a colon between -splash and the image name. This immediately displays the image before the runtime environment is fully initialized. The displayed image is centered on the screen. Splash screen images can be have GIF, PNG, or JPEG formats. As is the case for the regular Image class, splash screen images support animation, transparency, and translucency (translucency support is limited to Microsoft Windows 2000 or XP). The splash screen disappears when the first window is created by the application.

JAR File Manifests

Typically most users won't want to put -splash on their command line entry. So perhaps a more effective way of displaying a splash screen is to create a manifest file for an application, and then combine the application with the manifest and image in a JAR file. When a user launches the application from the JAR file, the splash screen appears. In this case, the user doesn't have to specify a command line option.

The manifest file option is named SplashScreen-Image. The option is followed by the image filename. The full path of the filename needs to be specified if the file is not at the top level of the JAR file.

Here's a simple example that demonstrates these new splash screen features. First, create the following program:

   import javax.swing.*;
   import java.awt.*;
   
   public class HelloSplash {
     public static void main(String args[]) {
       Runnable runner = new Runnable() {
         public void run() {
           try {
               Thread.sleep(1500);
           catch (InterruptedException e) {
           }
           JFrame frame = new JFrame("Splash Me");
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           JLabel label = new JLabel(
                "Hello, Splash", JLabel.CENTER);
           frame.add(label, BorderLayout.CENTER);
           frame.setSize(30095);
           frame.setVisible(true);
        }
       };
       EventQueue.invokeLater(runner);
     }
    

Next, compile the program:

    javac HelloSplash.java

Then try out the command-line -splash. For simplicity, use a splash screen image that's in the same directory as the program (this is not an absolute requirement):

   java -splash:MyImage.png HelloSplash

You'll see MyImage centered on the screen immediately, followed by the application screen once the Java runtime environment initializes.


Image Image

Now let's try the JAR file approach. First create the manifest.mf file for the manifest. The contents of the file should look like this:

Manifest-Version: 1.0 Main-Class: HelloSplash SplashScreen-Image: MyImage.png

Then package the JAR file:

jar -mcvf manifest.mf Splash.jar HelloSplash*.class MyImage.png

Then run the JAR without specifying the -splash command line option:

java -jar Splash.jar

As before, you should see the splash screen followed by the application screen.

If your JAR file has a splash screen image specified in its manifest, and a user specifies a splash image from the command line, the command line image is given precedence and shown instead.

Beyond the Basics

Although the command-line -splash and manifest SplashScreen-Image options are sufficient for most needs, there is more to splash screens in Mustang. The java.awt package offers a SplashScreen class for more advanced functionality beyond simply showing a splash screen image.

Provided an image was created by either the -splash command line option or the SplashScreen-Image option in the manifest, the getSplashScreen() method of the SplashScreen class returns the generated screen. If no image was created, getSplashScreen() returns null.

Using other SplashScreen methods, you can discover various things related to a splash screen:

  • getBounds() returns the bounds of the splash screen rectangle.
  • getImageURL() returns the URL of the splash screen image.
  • getSize() returns the size of the splash screen window.
  • isVisible() indicates whether the splash screen is visible

You can change the image shown after the splash screen is loaded, but before the application starts. You have two ways to do this. The setImageURL() method allows you to provide a URL for a new image to display. The second approach, which is likely more typical, is to call the getGraphics() method to get the graphics context (java.awt.Graphics) of the window. You then update the image with any of the normal graphical and Java 2D APIs. That's because this is an instance of Graphics2D, not simply java.awt.Graphics. After you draw to the graphics context, you call the update() method of SplashScreen to draw the updated image.

Here's an example of the latter behavior, which cycles through a bunch of colors on the splash screen. Imagine this displaying a progress bar or some other state data indicating the progress of application initialization.

   import javax.swing.*;
   import java.awt.*;
   import java.awt.geom.*;
   import java.util.*;

   public class ExtendedSplash {
     public static void main(String args[]) {
       Runnable runner = new Runnable() {
         public void run() {
           Random random = new Random();
           SplashScreen splash = SplashScreen.getSplashScreen();
           Graphics2D g = (Graphics2D)splash.getGraphics();
           Dimension dim = splash.getSize();
           Color colors[] {Color.RED, Color.ORANGE, 
             Color.YELLOW, Color.GREEN, Color.BLUE, 
             Color.MAGENTA};
           for (int i=0; i<100; i++) {
             g.setColor(colors[i % colors.length]);
             g.fillRect(5050, dim.width-100, dim.height-100);
             splash.update();
             try {
               Thread.sleep(250);
             catch (InterruptedException ignored) {
             }
           }
           JFrame frame = new JFrame("Splash Me2");
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           JLabel label = 
             new JLabel("Hello, Splash", JLabel.CENTER);
           frame.add(label, BorderLayout.CENTER);
           frame.setSize(30095);
           frame.setVisible(true);
         }
       };
       EventQueue.invokeLater(runner);
     }
   }

Image

Notice how the drawing is done over the splash screen image.

After the color cycling is complete, the example shows the frame. This is typical of a startup process: after the initialization completes, show the frame, which hides the splash screen.

The final SplashScreen option to mention uses the close() method. You can call this method if you want to explicitly close the window and release the associated resources. It isn't necessary to explicitly call this method because it is called automatically when the first window is made visible.

For additional information on using splash screens, see the technical article New Splash-Screen Functionality in Mustang. Also see the javadoc for the SplashScreen class.

Copyright (c) 2004-2005 Sun Microsystems, Inc.
All Rights Reserved.


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.