Reputation: 192
Set application Splash Screen from Project properties -> Run Option
SplashScreen which draw by StartApp Class
public class StartApp {
public static void main(String[] args) {
new Thread(new Runnable() {
public void run() {
splashInit(); // initialize splash overlay drawing parameters
appInit(); // simulate what an application would do before starting
if (mySplash != null) // check if we really had a spash screen
{
mySplash.close(); // we're done with it
}
}
}).start();
// begin with the interactive portion of the program
Context.initialize();
}
static SplashScreen mySplash; // instantiated by JVM we use it to get graphics
static Graphics2D splashGraphics; // graphics context for overlay of the splash image
static Rectangle2D.Double splashTextArea; // area where we draw the text
static Rectangle2D.Double splashProgressArea; // area where we draw the progress bar
static Font font; // used to draw our text
/**
* just a stub to simulate a long initialization task that updates
* the text and progress parts of the status in the Splash
*/
private static void appInit() {
Random random = new Random();
int progress = 0;
splashProgress(0);
while (progress < 100) {
//Sleep for up to one second.
splashText("Please wait... Application is starting " + progress + "%");
try {
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException ex) {
break;
}
//Make random progress.
progress += random.nextInt(20);
splashProgress(Math.min(progress, 100));
}
}
/**
* Prepare the global variables for the other splash functions
*/
private static void splashInit() {
// the splash screen object is created by the JVM, if it is displaying a splash image
mySplash = SplashScreen.getSplashScreen();
// if there are any problems displaying the splash image
// the call to getSplashScreen will returned null
if (mySplash != null) {
// get the size of the image now being displayed
Dimension ssDim = mySplash.getSize();
int height = ssDim.height;
int width = ssDim.width;
// stake out some area for our status information
splashTextArea = new Rectangle2D.Double(20, height * 0.91, width * .50, 25.);
splashProgressArea = new Rectangle2D.Double(1.0, height * .87, width - 2, 3);
// create the Graphics environment for drawing status info
splashGraphics = mySplash.createGraphics();
font = new Font("Dialog", Font.PLAIN, 14);
splashGraphics.setFont(font);
// initialize the status info
splashText("Starting");
splashProgress(0);
}
}
/**
* Display text in status area of Splash. Note: no validation it will fit.
* @param str - text to be displayed
*/
public static void splashText(String str) {
if (mySplash != null && mySplash.isVisible()) { // important to check here so no other methods need to know if there
// really is a Splash being displayed
// erase the last status text
splashGraphics.setPaint(new Color(248, 249, 250));
splashGraphics.fill(splashTextArea);
// draw the text
splashGraphics.setPaint(Color.BLACK);
splashGraphics.drawString(str, (int) (splashTextArea.getX() + 10), (int) (splashTextArea.getY() + 15));
// make sure it's displayed
mySplash.update();
}
}
/**
* Display a (very) basic progress bar
* @param pct how much of the progress bar to display 0-100
*/
public static void splashProgress(int pct) {
if (mySplash != null && mySplash.isVisible()) {
// Note: 3 colors are used here to demonstrate steps
// erase the old one
splashGraphics.setPaint(new Color(230, 230, 230));
splashGraphics.fill(splashProgressArea);
// draw an outline
// Calculate the width corresponding to the correct percentage
int x = (int) splashProgressArea.getMinX();
int y = (int) splashProgressArea.getMinY();
int wid = (int) splashProgressArea.getWidth();
int hgt = (int) splashProgressArea.getHeight();
int doneWidth = Math.round(pct * wid / 100.f);
doneWidth = Math.max(0, Math.min(doneWidth, wid - 1)); // limit 0-width
// fill the done part one pixel smaller than the outline
splashGraphics.setPaint(new Color(21, 106, 151));
splashGraphics.fillRect(x, y - 2, doneWidth, hgt + 1);
// make sure it's displayed
mySplash.update();
}
}
}
Above code is to draw Splash Screen. Please tell why it not work in jar file
while works in Netbeans IDE
.
Upvotes: 2
Views: 1270
Reputation: 347314
The commandline parameter -splash:
represents a image file on the disk, you can not reference embedded resources (which you image has become).
You should NEVER use any path reference which contains src
it simply won't exist once the program is built/packaged/exported.
I'd recommend using the manifest approach instead, as described in How to Create a Splash Screen, in the section titled "How to Use a JAR File to Display Splash Screen"
Netbeans actually has the ability to do this for you automatically, including packaging the image into the Jar and updating the manifest file for your
Upvotes: 3