Almost all modern applications have a splash screen. Typically splash screens are used for the following purposes:
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
, 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:
... 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.
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:
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>
SplashDemo.java
file.splash.gif
image in the images
directory.java -splash:images/splash.gif SplashDemo
nnn.gif
. Run the application as follows:
java -splash:images/nnn.gif SplashDemo
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>
SplashDemo.java
file.splash.gif
image in the images
directory.splashmanifest.mf
file as follows:
Manifest-Version: 1.0 Main-Class: SplashDemo SplashScreen-Image: images/splash.gif
jar cmf splashmanifest.mf splashDemo.jar SplashDemo*.class images/splash.gif
java -jar splashDemo.jar
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. |
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. |