Friday, June 20, 2008

Splash screens using Java

Almost all modern applications have a splash screen. Typically splash screens are used for the following purposes:

* Advertising a product
* Indicating to the user that the application is launching during long startup times
* Providing information that is only needed once per visit

Fortunately, Java™ SE 6 provides a solution that allows the application to display the splash screen much earlier, even before the virtual machine starts. A Java application launcher is able to decode an image and display it in a simple non-decorated window.

Here is sample java code available to display splash Screen.

Sample Java Code

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.SplashScreen;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SplashScreen extends Frame{
static void renderFrame(Graphics2D g, int frame) {
final String[] comps = { "foo", "bar", "baz" };
g.setComposite(AlphaComposite.Clear);
g.fillRect(130, 250, 280, 40);
g.setPaintMode();
g.setColor(Color.BLACK);
g.drawString("Loading " + comps[(frame / 5) % 3] + "...", 130, 260);
g.fillRect(130, 270, (frame * 10) % 280, 20);
}

public SplashScreen() {
super("SplashScreen demo");
setSize(500, 300);
setLayout(new BorderLayout());
Menu menu = new Menu("File");
MenuItem menuItem = new MenuItem("Exit");
menu.add(menuItem);
menuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}

});

MenuBar mb = new MenuBar();
setMenuBar(mb);
mb.add(menu);
final SplashScreen splash = SplashScreen.getSplashScreen();
if (splash == null) {
return;
}
Graphics2D g = (Graphics2D) splash.createGraphics();
if (g == null) {
return;
}
for (int i = 0; i <>
renderFrame(g, i);
splash.update();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
splash.close();
setVisible(true);
toFront();
}

public static void main(String args[]) {
SplashScreen screen = new SplashScreen();
}
}

References

http://java.sun.com/docs/books/tutorial/uiswing/misc/splashscreen.html

0 Java Junction comments: