Displaying a Splash Image With Java 6
Join the DZone community and get the full member experience.
Join For FreeIntroduced 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
Published at DZone with permission of Daniel Pietraru. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Azure Virtual Machines
-
An Overview of Cloud Cryptography
-
How to Use an Anti-Corruption Layer Pattern for Improved Microservices Communication
-
Performance Comparison — Thread Pool vs. Virtual Threads (Project Loom) In Spring Boot Applications
Comments