Daniel has posted 4 posts at DZone. View Full User Profile

Displaying a Splash Image With Java 6

05.01.2008
| 12285 views |
  • submit to reddit

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.

---

This article was originally posted at http://littletutorials.com/2008/03/09/displaying-a-splash-image-with-java-6

References
Published at DZone with permission of its author, Daniel Pietraru. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Genady Beryozkin replied on Fri, 2008/05/02 - 1:10pm

I haven't worked with Swing/AWT for a while, but from what I remember you could create a splash screen by showing an image. So, is it just a performance enhancement or a new functionality?

Daniel Pietraru replied on Fri, 2008/05/02 - 1:52pm

Hi Genady,

 This is new functionality that allows you to display a splash image by just specifying the file on the command line. A new API is also provided to allow access to the image being displayed. The API gives you the tools to "enhance" the image by drawing on it. You can also replace the image at runtime. So in the end it is a standad way of working with splash screens that should replace all the individual solutions developed over the years.

Genady Beryozkin replied on Fri, 2008/05/02 - 3:01pm

By "performance" I did mean that the splash screen would be displayed faster(=earlier) with the command line option.

Drawing on top of it is definetely a nice thing.  

Daniel Pietraru replied on Fri, 2008/05/02 - 3:06pm

Yes you are right, it is displayed earlier, as soon as the application starts. The image gets killed as soon as a window is displayed. So it just nicely covers the application initialization time/lag.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.