How to make a splash in Java
Developing splash screens for Java applications is harder than it needs to be, until now. In this article, we will show you two ways of adding splash screens to your Java application. The old and the hard way; the Mustang and the easy way.
The old way
Splash screens are just images; typically these are images displayed in a borderless window. Up until JavaSE 5 (a.k.a. Tiger), this is done with JWindow. Here is how you would do it:
- Create a class that extends JWindow.
- Load an image and display this in the window. The easiest way to display an image is to set it to a JLabel and add the JLabel to the window
- There are multiple ways to load an image. The easiest and most straightforward way is to use Toolkit class. However, Toolkit images are immutable viz., you cannot change them. So if you do not want to spice up your splash screen with fancy things like trasparency, shearing, etc., then load it with either ImageIO or as VolatileImage. We will not debate the pros and cons of each of these methods here.
public class MySplashScreen extends JWindow { |
The Mustang way
Mustang, or JavaSE 6, offers a simpler declarative way of displaying spash screens. Here is what you need to do.
java -splash:./splash.png MyJavaApplication |
The -splash option is new to Mustang. When you run your Java application (MyJavaApplication in the above example) with -splash option, the spash.png will be displayed when your application runs. In fact, your application may not even be aware that it has been 'splashed'.
If your application is packaged in a JAR file, you can specify the splash image as an attribute of the manifest file. The path of the splash image is relative to the root of the JAR file. Using the previous example, here is how you would write in the MANIFEST.MF file. Note that the splash.png image is saved in the images directory inside the JAR file.
Manifest-Version: 1.0 |
In case you want to decorate the splash screen with information, Mustang provides a class call SplashScreen which your application can get a handle to the splash image. You can now adorn the splash screen image with additional information or by spicying up the graphics.
- Use getSplashScreen() to return an instance of the splash screen. This will return null if there are no splash screens or the splash screen has already been closed. getSplashScreen() is a static method.
- Create a graphic context to the splash screen with createGrahics() so we can adorn the splash screen. createGraphics() returns a Graphics2D object which is an overlay on the splash screen image; use this Graphics2D object to draw on top of the splash screen.
- Use update() to update the contents of the Graphics2D to the splash screen
- Call close() to programatically close the splash screen.
Here is an example of how to programatically control and adorn splash screens in Mustang:
public static void main(String[] args) throws Exception { |
All examples and code snippets in this article can be found here. Unzip the file and read the README.txt file for instructions.
Lee Chuk-Munn has been programming in the Java language since 1996, when he first joined Sun Microsystems in Hong Kong. He currently works as a senior developer consultant and technology evangelist for Technology Outreach at Sun in Singapore. Chuk's focus is in Java APIs, Java EE, Java SE, and Java ME. Chuk graduated in 1987 from the Royal Melbourne Institute of Technology in Melbourne, Australia, where his favorite subject was compiler theory.