Java performance optimization skills highlights (5)

xiaoxiao2021-03-06  41

The content introduced in this article is suitable for the application of graphical user interface (Applet and normal application), to use AWT or SWING. 1. Use JAR compression class file

The Java file (JAR file) is a compressed file according to JavaBean standard, which is the primary and recommended ways to publish JavaBean components. JAR file helps reduce file volume and shorten the download time. For example, it helps Applet increase the start speed. A JAR file can contain one or more related beans and support files such as graphics, sound, HTML, and other resources. To specify a JAR file in an HTML / JSP file, just join the Archive = "name.jar" declaration in the Applet tag.

2. Tips Applet loading process

Have you seen the website that uses Applet, notice that a placeholder appears in a place where you should run Applet? What happens when the applet is downloaded? The biggest possibility is that the user will go out. In this case, the information displayed on an applet is undoubtedly helps to encourage users to wait. Let's take a look at a specific implementation method. First create a small applet, the applet is responsible for downloading official applets in the background:

IMPORT JAVA.Applet.applet;

Import java.applet.appletstub;

Import java.awt.label;

Import java.awt.graphics;

Import java.awt.gridlayout;

Public Class Preloader Extends Applet Implements Runnable, AppletStub {

String largeappletname;

Label label;

Public void init () {

/ / Require installation of official applet

LargeAppletName = getParameter ("applet"); // "Please wait" prompt information

Label = new label ("Please wait ..." LargeAppletName);

Add (label);

}

Public void run () {

Try

{

/ / Get the class to be loaded with the applet

Class LargeAppletClass = Class.Forname (LargeAppleTname);

// Create an instance of the Applet to be loaded

Applet LargeApplet = (applet) LargeAppletClass.newInstance ();

/ / Set the APPLET STUB program

Largeapplet.setstub (this);

// Cancel "Please wait" information

REMOVE (Label);

// Set the layout

SetLayout (New GridLayout (1, 0));

Add (LargeApplet);

/ / Show formal applet

Largeapplet.init ();

Largeapplet.start ();

}

Catch (Exception EX)

{

/ / Display error message

Label.SetText ("You cannot load the specified applet");

}

// Refresh screen

Validate ();

}

Public void appletresize (int Width, int Height)

{

// Pass the AppleTResize call from the Stub program to Applet

Resize (Width, Height);

}

}

The compiled code is less than 2K, and the download speed is very fast. There are several places in the code worth noting. First, the preloader implements the AppletStub interface. Generally, Applet judges its own codeBase from the caller. In this case, we must call setstub () telling the applet where to extract this information. Another thing that is worth noting is that the AppletStub interface contains many ways with the Applet class, except for the AppleTResize () method. Here we pass the call to the AppleTResize () method to the Resize () method. 3. Put it in advance before drawing the graph

ImageOBserver interface can be used to receive prompt information for graphics. The ImageOBserver interface has only one method imageUpdate (), which can draw graphics on the screen with a repaint () operation. An example is provided below.

Public Boolean ImageUpdate (Image IMG, INT FLAGS, INT X, INT Y, INT W, INT H) {

IF ((Flags & Allbits)! = 0 {

Repaint ();

}

Else IF (Flags & (Error | Abort))! = 0) {

Error = True;

// The file is not found, consider displaying a placeholder

Repaint ();

}

Return (Flags & (Allbits | Error | Abort) == 0;

}

When graphics information is available, the ImageUpdate () method is called. If further update is needed, the method returns true; if the required information has been obtained, the method returns false.

4. Cover Update Method

The default action of the update () method is to clear the screen and then call the Paint () method. If you use the default Update () method, frequent use of graphics can display flicker. To avoid the screen clearance operation before Paint () call, you only need to override the update () method according to the following:

Public void Update (graphics g) {

Paint (g);

}

The more ideal solution is to override Update (), only the area that changes on the screen, as shown below:

Public void Update (graphics g) {

g.ClipRect (x, y, w, h);

Paint (g);

}

(Author: Java Research)

转载请注明原文地址:https://www.9cbs.com/read-61388.html

New Post(0)