You can use Java Foundation Classes/Swing (JFC/Swing) or AWT 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 JVM*, AWT, usually Swing, and perhaps some application-dependent libraries. The resulting delay of several seconds has made use of a Java technology-based splash screen less than desirable. Until now.
Java Platform, Standard Edition (Java SE, formerly known as J2SE) version 6, provides a solution that allows the application to show the splash screen much earlier, even before the virtual machine starts. Now, a Java application launcher is able to decode an image and display it in a simple nondecorated window (see Figure 1).
Note: To run the code in this article, you must download and install Java SE 6.
Use of an option in the manifest file can allow a JAR-packaged application to display a splash screen. Other types of applications can use a command-line option. You can use a desktop shortcut or a script to provide the command-line option to the Java application launcher. The splash screen can display any GIF
, PNG
, or JPEG
image, with transparency, translucency, and animation.
There are two ways to show the native splash screen:
- If your application is run from the command line or from a shortcut, use the
-splash:
Java application launcher option to show a splash screen:
java -splash:filename.gif SplashTest
- 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 archive and specify the path in the option. For example, use this code in themanifest.mf
file:
Manifest-Version: 1.0
Main-Class: SplashTest
SplashScreen-Image: filename.gif
The command-line interface has precedence over the manifest setting.
The feature is extremely easy to use. In most cases, all you need to do is provide the image and use a launcher option. The splash screen will close automatically when the first AWT or Swing window is displayed.
In certain cases, you might want to provide some additional dynamic information in the splash screen. The SplashScreen
class may be used to close the splash screen, change the splash-screen image, get the image position or size, and paint in the splash screen. The class cannot be used to create the splash screen. You should use the command-line or manifest-file option for that.
In addition, this class cannot be instantiated. Only a single instance of this class can exist, and it may 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, you want to keep the splash-screen image on the screen and display something over the image, such as a progress indicator. The splash-screen window has an overlay surface with an alpha channel, and you can access this surface with a traditional Graphics
or Graphics2D
interface.
The following code sample demonstrates how you first obtain the SplashScreen
object, then obtain a graphic handle with the getGraphics()
method. Next, obtain the size of the splash screen, and clear the image if you already have drawn anything there. Set an AlphaComposite.Clear
composite mode, and paint a rectangle over the whole splash screen. Restore the painting mode and paint whatever you want. Lastly, call the update()
method to display what you have drawn.
SplashScreen splash = SplashScreen.getSplashScreen(); |
Also, you might want to replace the splash screen with an AWT or Swing window:
SplashScreen splash = SplashScreen.getSplashScreen(); |
Now show your new hand-coded splash-screen window at exactly the same location as specified in splashBounds
. The original splash screen closes automatically.
You may change the image in the splash screen with the setImageURL
method. If the user wants to close the splash screen before the first AWT or Swing window is displayed (or in the rare case that AWT or Swing is not used for the application GUI), this may be done with the SplashScreen.close
method.
The following application is an example of how the new splash screen works.
import java.awt.*; |
Note that getGraphics
creates a graphics context (as a Graphics
object) for the splash-screen overlay image, which allows you to draw over the splash screen. Rather than drawing on the main image, you draw instead on the image that is displayed over the main image, using alpha blending. Also note that drawing on the overlay image does not necessarily update the contents of the splash-screen window. You should call update()
on the SplashScreen
when you want the splash screen to be updated immediately.