Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /mnt/w0503/d19/s26/b02d525a/www/littletutorials.com/wordpress/wp-content/plugins/all-in-one-seo-pack/all_in_one_seo_pack.php:632) in /mnt/w0503/d19/s26/b02d525a/www/littletutorials.com/wordpress/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /mnt/w0503/d19/s26/b02d525a/www/littletutorials.com/wordpress/wp-content/plugins/all-in-one-seo-pack/all_in_one_seo_pack.php:632) in /mnt/w0503/d19/s26/b02d525a/www/littletutorials.com/wordpress/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41

Displaying a splash image with Java 6


Introduced in Java 6 is the option of displaying a splash screen when an application starts. The splash image file can be specified on the command line with the new splash option -splash:splash.jpg or in the manifest of a jar file with the SplashScreen-Image option.
The program can access at runtime the splash screen image through the SplashScreen class. This class cannot be created directly and its sole instance can be obtained calling SplashScreen.getSplashScreen(). The splash screen image is closed automatically when the first AWT/Swing window is displayed or it can be closed using the API.

The programmer can draw on an overlay image displayed on top of the splash image. Direct access to the displayed splash image is not provided but the splash image can be changed at runtime.

The next example shows how to use the splash screen features. It draws text and a progress bar on top of the splash image specified as a JVM command line parameter. It also changes the splash image from time to time. The splash images are expected to be in the current directory.

package com.littletutorials.splash;

import java.awt.*;
import java.io.*;
import java.net.*;

public class SplashTest
{
    private static final String[] SPLASHES = {"splash.jpg", "splash2.jpg"};
    private static final int X = 20, W = 250;
    private static final int TEXT_H = 10, BAR_H = 20;
    private static final int NUM_BUBBLES = 10;

    private int textY, barY;
    private int barPos = 0;

    private final SplashScreen splash;
    private Graphics2D graph;

    public SplashTest()
    {
        splash = SplashScreen.getSplashScreen();
        if (splash == null)
        {
            System.out.println(
                "Error: no splash image specified on the command line");
            return;
        }

        // compute base positions for text and progress bar
        Dimension splashSize = splash.getSize();
        textY = splashSize.height - 50;
        barY = splashSize.height - 30;

        graph = splash.createGraphics();
        drawSplashUrl(splash.getImageURL());
    }

    public void closeSplash()
    {
        if (splash != null)
        {
            splash.close();
        }
    }

    public void drawSplashProgress(String msg)
    {
        // clear what we don't need from previous state
        graph.setComposite(AlphaComposite.Clear);
        graph.fillRect(X, textY, W, TEXT_H);
        if (barPos == 0)
        {
            graph.fillRect(X, barY, W, BAR_H);
        }

        // draw new state
        graph.setPaintMode();

        // draw message
        graph.setColor(Color.BLACK);
        graph.drawString(msg, X, textY + TEXT_H);

        // draw progress bar
        graph.setColor(Color.BLUE);
        graph.fillOval(X + barPos * (BAR_H + 1), barY, BAR_H, BAR_H);

        // show changes
        splash.update();
        barPos = (barPos + 1) % NUM_BUBBLES;
    }

    public void changeSplash(int i)
    {
        try
        {
            splash.setImageURL(new File(SPLASHES[(i / 10) % 2]).toURI().toURL());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private void drawSplashUrl(URL url)
    {
        graph.setPaintMode();
        graph.setColor(Color.BLACK);
        graph.drawString("Splash image: " + url.toString(), X, 30);
        splash.update();
    }

    public static void main(String args[]) throws Exception
    {
        SplashTest test = new SplashTest();
        for (int i = 0; i < 100; i++)
        {
            test.drawSplashProgress("Progress step number " + i);
            Thread.sleep(250);

            // change the splash image from time to time
            if (i > 0 && i % 10 == 0)
            {
                test.changeSplash(i);
            }
        }

        test.closeSplash();
    }
}

To run this example place 2 images (splash.jpg and splash2.jpg) in the directory referred by the “user.dir” system property. and run the application with the JVM command line option -splash:splash.jpg.

2 Responses to “Displaying a splash image with Java 6”

  1. Hi - very nice article.
    We’d really like to see this on JavaLobby. If you’re interested in reposting please contact me and we can organize it.

    Regards
    James

  2. Thanks James, I will contact you.

  • Calendar

    March 2008
    M T W T F S S
        Apr »
     12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31  
  • Tracking

  • License

    • Creative Commons License