5. Delayed redrawing operation
For the application of the graphical user interface, the main reason why performance is often the efficiency of the heavy picture screen. This is usually obvious that the user changes the window size or scrolls one window. Changing the window size or the operation such as scrolling screen results in a large number of flash events to generate, and even exceeds the execution speed of the relevant code. The best way to deal with this problem is to ignore all "late" events.
It is recommended to introduce a few milliseconds here, that is, if we immediately receive another heavy draw event, stop processing the current event to handle the finals that the last received heavy draw event; otherwise, we continue to carry out the current heavy blow.
If the event is to start a time consuming, it is a good way to process a work thread; otherwise, some parts may be "freeze" because only one event can be handled each time. A simple example of an event handling is provided below, but it can be used to control the working thread after expansion.
Public Static Void Runonce (String ID, Final Long MilliseConds) {
SYNCHRONIZED (E_QUEUE) {
// E_QUEUE: Collection of all events
IF (! E_QUE.CONTAINSKEY (ID)) {
e_queue.put (token, new lastone ());
}
}
Final lastone lastone = (lastone) E_QUEUE.GET (TOKEN);
Final long time = system.currenttimemillis (); // Get the current time
Lastone.Time = TIME;
(new thread () {public void run () {
IF (MilliseConds> 0) {
Try {thread.sleep (MilliseConds);} // Term thread
Catch (Exception EX) {}
}
Synchronized (Lastone.Running) {// Waiting for the last event end
IF (Lastone.Time! = Time) // only handle the last event
Return;
}
}}). START ();
}
Private static hashtable e_queue = new hashtable (); private static class lastone {
Public long time = 0;
Public Object Running = New Object ();
}
6. Use a double buffer
A buffer drawing outside the screen, and immediately display the entire graph immediately after completion. Because there are two buffers, the program can switch back and forth. In this way, we can use a low priority thread to draw, so that the program can perform other tasks with idle CPU times. The pseudo code fragment below demonstrates this technology.
Graphics mygraphics;
Image myoffscreenimage = createImage (size (). Width, size (). Height);
Graphics offscreengraphics = myoffscreenimage.Graphics ();
OFFSCREENGRAPHICS.DRAWIMAGE (IMG, 50, 50, THIS);
MyGraphics.drawImage (MyoffScreenimage, 0, 0, this);
7. Using BufferedImage
Java JDK 1.2 uses a soft display device that makes text look similar on different platforms. To implement this feature, Java must directly process pixels that make up text. Since this technique is to be done in memory, early JDK is in poor performance in using this technology. The Java standard proposed to solve this problem has achieved a new graphic type, which is bufferedimage. The BufferedImage subclass describes the graphic with an accessible graphics data buffer. A bufferedImage contains a ColorModel and a set of raster graphics data. This class generally uses the RGB (red, green, blue) color model, but it can also handle grayscale graphics. Its constructor is simple, as follows: Public BufferedImage (int Width, Int Height, int imagetype)
ImageType allows us to specify what type of graphic to buffer, such as 5-bit RGB, 8-bit RGB, grayscale, etc.
8. Using VolatileImage
Many hardware platforms and their operating systems provide basic hardware acceleration support. For example, hardware acceleration typically provides rectangular filling, and hardware acceleration is more efficient than using CPUs to complete the same task. Since the hardware accelerates a part of the work, multiple workflows are allowed to perform concurrent, thereby alleviating the pressure on the CPU and the system bus, so that the application can run faster. Use VolatileImage to create hardware accelerated graphics and the content of management graphics. Since it directly uses the ability to use low-level platforms, the degree of improvement in performance depends primarily on the graphical adapter used by the system. The content of VolatileImage may be lost at any time, that is, it is "Volatile". Therefore, it is best to check if its content is lost before using the graph. VolatileImage has two ways to check if the content is lost:
Public Abstract Int Validate (GraphicsConfiguration GC); Public Abstract Boolean Contentslost ();
You should call the validate () method each time you copy content or write to VolatileImage, you should call the VOLATILATIMAGE object. The contentslost () method tells us that since the last Validate () call, the content of the graph is lost. Although VolatileImage is an abstract class, don't send a child class from it. VolatileImage should be created by Component.createVolatileImage () or GraphicsConfiguration.createCompaVolatileImage () method.
9. Using Window Blitting
When scrolling operation, all visible content generally want to scroll, resulting in a lot of unnecessary heavy paintings. Many operating systems of graphic subsystems, including Win32 GDI, MacOS, and X / Windows, support Window Blitting Technology. Window Blitting technology moves the graphics directly to a new location in the screen buffer, only the new area. To use Window Blitting technology in Swing applications, the setting method is as follows:
SetscrollMode (int mode);
In most applications, this technique can be used to improve scrolling speed. Only in one case, Window Blitting will result in a reduction in performance, that is, the application is scrolling in the background. If the user is scrolling an application, it is always at the front desk, there is no need to worry about any negative impact. (T117)