DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

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

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
  1. DZone
  2. Coding
  3. Java
  4. Displaying a Splash Image With Java 6

Displaying a Splash Image With Java 6

Daniel Pietraru user avatar by
Daniel Pietraru
·
May. 01, 08 · News
Like (0)
Save
Tweet
Share
16.40K Views

Join the DZone community and get the full member experience.

Join For Free

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

Java (programming language)

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

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: