X
Tech

How to make a splash in Java

There is an easier way to add splash screens to your Java applications. Lee Chuk-Munn shows you how.
Written by Lee Chuk-Munn, Contributor
Most applications have splash screens. What are splash screens? It is the nice little image that is displayed when you launch an application. The splash screen typically displays the name of the application and possibally lists the parts of the application that are being loaded. The idea of a splash screen is to keep the user occupied while the application loads.

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:

  1. Create a class that extends JWindow.
  2. 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
  3. 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 {

    public MySplashScreen(String imgFile) {

        JPanel panel;
        //Load the image
        Image image = Toolkit.getDefaultToolkit().getImage(imgFile);
        //Set the image to a JLabel
        JLabel lab = new JLabel(new ImageIcon(image));

        //Add the Label to the center of the Window
        //Default layout for Windows are BorderLayout
        add(lab, BorderLayout.CENTER);

        pack();

        //Get the size of the screen and the size of the images
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension labelSize = lab.getPreferredSize();
        //Do some calculation so that we can position the splash screen at the
        //center of the screen
        setLocation((screenSize.width/2) - (labelSize.width/2)
                ,(screenSize.height/2) - (labelSize.height/2));       
    }

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
    Main-Class: MyJavaApplication
    SplashScreen-Image: images/splash.png
    ...

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.

  1. 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.
  2. 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.
  3. Use update() to update the contents of the Graphics2D to the splash screen
  4. 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 {

        SplashScreen splash = SplashScreen.getSplashScreen();

        //Check if we have specified any splash screen
        if (splash == null) {
            System.out.println("No splash screen");
            System.exit(0);
        }

        if (splash.isVisible()) {
            //Get a graphics overlay for the splash screen
            Graphics2D graphics = splash.createGraphics();
            //Do some drawing on the graphics object
            ...
            //Now update to the splash screen
            splash.update();
        }

        ...

        //Close the splash screen once we are done (optional)
        splash.close();
    }

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.

Editorial standards