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
Java Foundation Classes, both Swing and Abstract Windowing Toolkit (AWT), enable a developer to create splash screens in Java technology applications. However, because the main purpose of a splash screen is to provide the user with feedback about the application's startup, the delay between the application's startup and the moment when the splash screen pops up should be minimal. Before the splash screen can pop up, the application has to load and initialize the Java™ Virtual Machine (JVM), AWT, Swing, and sometimes application-dependent libraries as well. The resulting delay of several seconds has made the use of a Java™ technology-based splash screen less than desirable.
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
, orjpeg
image, with transparency, translucency, and animation. The figure below represents an example of the Java application splash screen developed as an animatedgif
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 thegetSplashScreen()
static method. If the application has not created the splash screen at startup through the command-line or manifest-file option, thegetSplashScreen
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 thecreateGraphics()
method:... final SplashScreen splash = SplashScreen.getSplashScreen(); if (splash == null) { System.out.println("SplashScreen.getSplashScreen() returned null"); return; } Graphics2D g = splash.createGraphics(); if (g == null) { System.out.println("g is null"); return; } ...Find the demo's complete code in the
SplashDemo.java
file.
Note: The SplashDemo application uses fixed coordinates to display overlay information. These coordinates are image-dependent and calculated individually for each splash screen.The native splash screen can be displayed in the following ways:
- Command-line argument
- Java™ Archive (JAR) file with the specified manifest option
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:
- Compile the
file.
SplashDemo.java
- Save the
image in the
splash.gif
images
directory.- Run the application from the command line with the following arguments:
java -splash:images/splash.gif SplashDemo- Wait until the splash screen has been completely displayed.
- The application window appears. To close the window choose File|Exit from the pop-up menu or click the X.
- 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- You will see the following output string:
SplashScreen.getSplashScreen() returned null
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:Manifest-Version: 1.0 Main-Class: <class name> SplashScreen-Image: <image name>
Try this:
- Compile the
file.
SplashDemo.java
- Save the
image in the
splash.gif
images
directory.- Prepare the
splashmanifest.mf
file as follows:Manifest-Version: 1.0 Main-Class: SplashDemo SplashScreen-Image: images/splash.gif- Create a JAR file using the following command:
jar cmf splashmanifest.mf splashDemo.jar SplashDemo*.class images/splash.gifFor more information about JAR files, see Using JAR Files in the Packaging Programs in JAR Files page.- Run the application:
java -jar splashDemo.jar- Wait until the splash screen has been completly displayed.
- The application window appears. To close the window choose File|Exit from the pop-up menu or click the X.
The Splash Screen API
The
SplashScreen
class cannot be used to create the splash screen. Only a single instance created within this class can exist.
Method Purpose getSplashScreen() Returns the SplashScreen
object used for Java startup splash screen control.createGraphics() Creates a graphics context (as a Graphics2D
object) for the splash screen overlay image, which allows you to draw over the splash screen.getBounds() Returns the bounds of the splash screen window as a Rectangle
.close() Closes the splash screen and releases all associated resources. Example That Uses the SplashScreen API
The following table lists the example that uses splash screen.
Example Where Described Notes SplashDemo
This section Shows a splash screen before opening the application window.