1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| 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 EcranDepart
{
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 EcranDepart(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("Chargement du programme. Attendez..."); // tell the user what initialization task is being done
splashProgress(pctDone); // give them an idea how much we have completed
try
{
Thread.sleep(1500); // 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(563., height*0.85, width * .20, 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, 10);
splashGraphics.setFont(font);
// initialize the status info
splashText("Chargement");
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.RED);
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.RED);
splashGraphics.fillRect(x, y+1, doneWidth, hgt-1);
// make sure it's displayed
mySplash.update();
}
}
} |
Partager