2006年12月26日星期二

java6的启动界面

Splash screens are a standard part of any modern graphical user interface (GUI) application. Their primary purpose is to let the user know that the application is starting up. An application that displays a polished and professional-looking splash screen can occupy the user's attention and gain the user's confidence that the application is starting. In addition, splash screens may provide marketing information. And they are sometimes required for legal reasons: to present copyright information, third-party logos, and so forth.

You can use Java Foundation Classes/Swing (JFC/Swing) or AWT to create splash screens in Java technology applications. However, because the main purpose of a splash screen is to provide the user with feedback about the application's startup, the delay between the application's startup and the moment when the splash screen pops up should be minimal. Before the splash screen can pop up, the application has to load and initialize the JVM*, AWT, usually Swing, and perhaps some application-dependent libraries. The resulting delay of several seconds has made use of a Java technology-based splash screen less than desirable. Until now.

Java Platform, Standard Edition (Java SE, formerly known as J2SE) version 6, provides a solution that allows the application to show the splash screen much earlier, even before the virtual machine starts. Now, a Java application launcher is able to decode an image and display it in a simple nondecorated window (see Figure 1).

Note: To run the code in this article, you must download and install Java SE 6.

Use of an option in the manifest file can allow a JAR-packaged application to display a splash screen. Other types of applications can use a command-line option. You can use a desktop shortcut or a script to provide the command-line option to the Java application launcher. The splash screen can display any GIF, PNG, or JPEG image, with transparency, translucency, and animation.

There are two ways to show the native splash screen:

  • If your application is run from the command line or from a shortcut, use the -splash: Java application launcher option to show a splash screen:

      java -splash:filename.gif SplashTest

  • If your application is packaged in a JAR file, you can use the SplashScreen-Image option in a manifest file to show a splash screen. Place the image in the JAR archive and specify the path in the option. For example, use this code in the manifest.mf file:

         Manifest-Version: 1.0
    Main-Class: SplashTest
    SplashScreen-Image: filename.gif

    The command-line interface has precedence over the manifest setting.

The feature is extremely easy to use. In most cases, all you need to do is provide the image and use a launcher option. The splash screen will close automatically when the first AWT or Swing window is displayed.

In certain cases, you might want to provide some additional dynamic information in the splash screen. The SplashScreen class may be used to close the splash screen, change the splash-screen image, get the image position or size, and paint in the splash screen. The class cannot be used to create the splash screen. You should use the command-line or manifest-file option for that.

In addition, this class cannot be instantiated. Only a single instance of this class can exist, and it may be obtained using the getSplashScreen() static method. If the application has not created the splash screen at startup through the command-line or manifest-file option, the getSplashScreen method returns null.

Typically, you want to keep the splash-screen image on the screen and display something over the image, such as a progress indicator. The splash-screen window has an overlay surface with an alpha channel, and you can access this surface with a traditional Graphics or Graphics2D interface.

The following code sample demonstrates how you first obtain the SplashScreen object, then obtain a graphic handle with the getGraphics() method. Next, obtain the size of the splash screen, and clear the image if you already have drawn anything there. Set an AlphaComposite.Clear composite mode, and paint a rectangle over the whole splash screen. Restore the painting mode and paint whatever you want. Lastly, call the update() method to display what you have drawn.

SplashScreen splash = SplashScreen.getSplashScreen();
Graphics2D g = (Graphics2D)splash.getGraphics();
Dimension size = splash.getDimension();
g.setComposite(AlphaComposite.Clear);
g.fillRect(0, 0, size.width, size.height);
g.setPaintMode();

Also, you might want to replace the splash screen with an AWT or Swing window:

SplashScreen splash = SplashScreen.getSplashScreen();
// Obtain the splash-screen bounds.
Rectangle splashBounds = splash.getBounds();

Now show your new hand-coded splash-screen window at exactly the same location as specified in splashBounds. The original splash screen closes automatically.

You may change the image in the splash screen with the setImageURL method. If the user wants to close the splash screen before the first AWT or Swing window is displayed (or in the rare case that AWT or Swing is not used for the application GUI), this may be done with the SplashScreen.close method.

The following application is an example of how the new splash screen works.

import java.awt.*;
import java.awt.event.*;

public class SplashTest extends Frame implements ActionListener {
static void renderSplashFrame(Graphics2D g, int frame) {
final String[] comps = {"foo", "bar", "baz"};
g.setComposite(AlphaComposite.Clear);
g.fillRect(130,250,280,40);
g.setPaintMode();
g.setColor(Color.BLACK);
g.drawString("Loading "+comps[(frame/5)%3]+"...", 130, 260);
g.fillRect(130,270,(frame*10)%280,20);
}
public SplashTest() {
super("SplashScreen demo");
setSize(500, 300);
setLayout(new BorderLayout());
Menu m1 = new Menu("File");
MenuItem mi1 = new MenuItem("Exit");
m1.add(mi1);
mi1.addActionListener(this);

MenuBar mb = new MenuBar();
setMenuBar(mb);
mb.add(m1);
final SplashScreen splash = SplashScreen.getSplashScreen();
if (splash == null) {
System.out.println("SplashScreen.getSplashScreen() returned null");
return;
}
Graphics2D g = (Graphics2D)splash.getGraphics();
if (g == null) {
System.out.println("g is null");
return;
}
for(int i=0; i<100; i++) {
renderSplashFrame(g, i);
splash.update();
try {
Thread.sleep(200);
}
catch(InterruptedException e) {
}
}
splash.close();
setVisible(true);
toFront();
}
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
public static void main (String args[]) {
SplashTest test = new SplashTest();
}
}

Note that getGraphics creates a graphics context (as a Graphics object) for the splash-screen overlay image, which allows you to draw over the splash screen. Rather than drawing on the main image, you draw instead on the image that is displayed over the main image, using alpha blending. Also note that drawing on the overlay image does not necessarily update the contents of the splash-screen window. You should call update() on the SplashScreen when you want the splash screen to be updated immediately.

2 条评论:

匿名 说...

When you consider the word your message adore, not just in relation to its an amorous romantic relationship having one more, although for a sense that's engendered once you have miltchmonkey a better romantic relationship with yourself way too : or even just being a sense of higher unity family members or maybe human race -- it gets to be all the more superior that each one any one is looking to get in life is actually love.

trustno1 说...

java编程实例
java example将字符串转换为十六进制