Two tricks that eliminate flash in Java animation!

zhaozj2021-02-08  268

Author: javaduke original in: www.dukejava.com

1. Eliminate flash by overwriting public void update (graphics g)!

In the animation implementation, the repaint () function is often used to redraw screen, realize the animation loading, actually in java, repainT () is through two steps to implement refresh function, first call public void update () Refresh screen, followed by calling Paint (Graphcis G) to redraw screen, which is easy to flicker, especially some programs that need to redraw background, if the next image can completely cover the image, you can The rewriting the UPDATE function will be removed down:

Public void Update (graphics g) {Paint (g)}

Also call the repaint () redraw screen. Or directly rewrite the use of repaint, and use

Graphics g = getGraphics ();

Paint (g); to achieve the heavy picture screen.

2. Eliminate the twinkling through dual cache?

In order to prevent the flashing of the screen heavy blow, create an exterior drawing area, heavy painting, directly draw the finished product to the small program screen. Below is a dual cache technology code used when the webmaster creates a dynamic advertising board:

OFFSCREENIMAGE = CREATEIMAGE (W, H); // Creating a screen exterior drawing area

OFFSCREEN = OFFSCREENIMAGE.GETGRAPHICs (); // Get drawing environments

Offscreen.SetFont (New Font ("Serif", Font.Plain, 14);

Offscreen.drawstring ("You are Welcome!", 4, getBounds (). HEIGHT / 2);

}

Else {

OFFSCREEN.SETCOLOR (BGCLR);

OFFSCREEN.FILLRECT (0, 0, W, H);

OFFSCREEN.SETCOLOR (FGCLR);

For (int i = j; i

OFFSCREEN.DRAWSTRING (STRS [I], 0, Y = WordVspace);

}

Y = Y-16 * WordVspace;

}

g.drawImage (Offscreenimage, 0, 0, this); // Draw the exterior finished product into the apparatus

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

New Post(0)