Customizing the Splash Screen

Một phần của tài liệu Beginning Java SE 6 Platform From Novice to Professional phần 3 pps (Trang 21 - 25)

A splash window is associated with an overlay image that can be drawn on and alpha-blended with the window’s image, letting you customize the splash screen.

Customization requires you to work with the java.awt.SplashScreenclass. Table 3-3 describes this class’s methods.

Table 3-3.SplashScreen Class Methods

Method Description

public void close() Hides and closes the splash window, and releases all resources. An IllegalStateExceptionis thrown if the splash window is already closed.

public Graphics2D createGraphics() Creates and returns a graphics context for drawing on the overlay image. Because drawing on this image doesn’t necessarily update the splash window, you should call update()when you want to immediately update the splash window with the overlay image. An IllegalStateExceptionis thrown if the splash window has been closed.

public Rectangle getBounds() Returns the splash window’s bounds. These bounds are important for replacing the splash window with your own window. An IllegalStateExceptionis thrown if the splash window has been closed.

public URL getImageURL() Returns the displayed splash image. An

IllegalStateExceptionis thrown if the splash window has been closed.

public Dimension getSize() Returns the splash window size. This size is important for replacing the splash window with your own window.

An IllegalStateExceptionis thrown if the splash window has been closed.

Continued

public static SplashScreen Returns the SplashScreenobject that is used to control getSplashScreen() the startup splash window. If there is no splash window

(or if the window has been closed), this method returns null. An UnsupportedOperationExceptionis thrown if the current AWT toolkit implementation does not support splash screens. A java.awt.HeadlessExceptionis thrown if there is no display device.

public boolean isVisible() Returns true if the splash window is visible. Returns false if the window has been hidden via a call to close() or when the first AWT/Swing window is made visible.

public void setImageURL(URL imageURL) Changes the splash image to the image that is loaded from imageURL. GIF, JPEG, and PNG image formats are supported. This method returns after the image has been loaded and the splash window has been updated.

The window is resized to the image’s size and centered on the screen. A NullPointerExceptionis thrown if you pass nullto imageURL; an IOExceptionis thrown if an error occurs while loading the image; and an

IllegalStateExceptionis thrown if the splash window has been closed.

public void update() Updates the splash window with the current contents of the overlay image. An IllegalStateExceptionis thrown if the overlay image does not exist

(createGraphics()was never called) or if the splash window has been closed.

You cannot directly instantiate a SplashScreenobject, because this object is meaning- less if a splash window has not been created (in response to -splashor SplashScreen- Image). Instead, you must call the getSplashScreen()method to retrieve this object.

Because this method returns null if a splash window does not exist, you need to test the return value prior to performing customization:

SplashScreen splashScreen = SplashScreen.getSplashScreen ();

if (splashScreen != null) {

// Perform appropriate customization.

}

I’ve created a skeletal document viewer application (you supply the document viewer code) that demonstrates how to customize a splash screen. Listing 3-4 presents this application’s source code.

Table 3-3.Continued

Method Description

Listing 3-4.DocViewer.java // DocViewer.java import java.awt.*;

public class DocViewer {

public static void main (String [] args) {

SplashScreen splashScreen = SplashScreen.getSplashScreen ();

if (splashScreen != null) {

// Surround the image with a border that occupies 5% of the smaller // of the width and height.

Dimension size = splashScreen.getSize ();

int borderDim;

if (size.width < size.height)

borderDim = (int) (size.width * 0.05);

else

borderDim = (int) (size.height * 0.05);

Graphics g = splashScreen.createGraphics ();

g.setColor (Color.blue);

for (int i = 0; i < borderDim; i++)

g.drawRect (i, i, size.width-1-i*2, size.height-1-i*2);

// Make sure the text fits the splash window before drawing -- the // text is centered in the lower part of the splash window.

FontMetrics fm = g.getFontMetrics ();

int sWidth = fm.stringWidth ("Initializing...");

int sHeight = fm.getHeight ();

if (sWidth < size.width && 2*sHeight < size.height) {

g.setColor (Color.blue);

g.drawString ("Initializing...", (size.width-sWidth)/2, size.height-2*sHeight);

}

// Update the splash window with the overlay image.

splashScreen.update ();

// Pause for 5 seconds to simulate a lengthy initialization task, // and to view the image.

try {

Thread.sleep (5000);

}

catch (InterruptedException e) {

} }

// Continue with the DocViewer application.

} }

Run this application via java -splash:dvlogo.jpg DocViewer. After verifying that a splash window exists and an image is displayed, the DocViewer.javasource code draws a blue frame and an initialization message (also in blue) on the overlay image. The splashScreen.update()method call alpha-blends this overlay image with the underlying dvlogo.jpgimage. Figure 3-1 shows the resulting combined image.

Figure 3-1.Windows XP displays an hourglass mouse cursor when this cursor is moved over the splash window.

Note For more information about customizing splash screens, see the Sun Developer Network article

“New Splash-Screen Functionality in Java SE 6” (http://java.sun.com/developer/

technicalArticles/J2SE/Desktop/javase6/splashscreen/index.html).

Một phần của tài liệu Beginning Java SE 6 Platform From Novice to Professional phần 3 pps (Trang 21 - 25)

Tải bản đầy đủ (PDF)

(51 trang)