0
我需要一些幫助,我一直在網上尋找代碼,用於閃屏,我發現一些代碼是完美的,但有一個問題,我想飛濺在完成加載後打開我的程序屏幕,我該如何去做這件事?這裏是我的啓動畫面代碼:加載後啓動畫面打開程序
/*
* This demonstration program is released without warranty or restrictions.
* You may modify and use it as you wish.
* Just don't complain to me if you have trouble.
*/
package splashdemo;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.SplashScreen;
import java.awt.geom.Rectangle2D;
/**
* Example for Splash Screen tutorial
* @author Joseph Areeda
*/
public class Main
{
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
public static void main(String[] args)
{
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
// begin with the interactive portion of the program
}
/**
* 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()
{
for (int i = 1; i <= 10; i++)
{ // pretend we have 10 things to do
int pctDone = i * 10; // this is about the only time I could calculate rather than guess progress
splashText("Configuring Program"); // tell the user what initialization task is being done
splashProgress(pctDone); // give them an idea how much we have completed
try
{
Thread.sleep(1000); // wait a second
}
catch (InterruptedException ex)
{
break;
}
}
}
/**
* 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(15., height*0.88, width * .45, 32.);
splashProgressArea = new Rectangle2D.Double(width * .55, height*.92, width*.4, 12);
// 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(Color.LIGHT_GRAY);
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(Color.LIGHT_GRAY);
splashGraphics.fill(splashProgressArea);
// draw an outline
splashGraphics.setPaint(Color.BLUE);
splashGraphics.draw(splashProgressArea);
// 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(Color.GREEN);
splashGraphics.fillRect(x, y+1, doneWidth, hgt-1);
// make sure it's displayed
mySplash.update();
}
}
}
沒有比['SplashScreen'(http://docs.oracle。 com/javase/7/docs/api/java/awt/SplashScreen.html)。 –
順便說一句 - 不要阻塞EDT(事件調度線程) - 當這種情況發生時GUI將「凍結」。而不是調用'Thread.sleep(n)'實現一個Swing'Timer'來重複執行任務,或者一個'SwingWorker'執行長時間運行的任務。有關更多詳細信息,請參見[Swing中的併發](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/)。 –