Article Source: http://soft.yesky.com/softchannel/72342371961929793.SHTML Draw animation is very simple, just follow the following three steps; (1) Wipe off the entire small program drawing area; (2) Heavy painting animation background; (3) draw graphics in a new location. However, when the movement and color of the continuous frame are not continuous, the flash is generated (excluded). Question The screen refreshes in the small program area, there are two ways to prevent the stop. The first method is to do only the local brush, that is, only the changed part is erased. For example, if you want to draw a "star-flying star" moving, each time you brush the new operation, first wipe the first star star, and then draw one in the new position. However, if the motion ratio is more complex, the moving portion has more overlapping, and the local brush is very popular and the speed of the row. In this case, it can be solved with another method (dual cache). Its nuclear thinking is to finish all frames of one frame outside the screen, then display the frames of the most afterward in the sub-program screen. The process is shown in Figure 3.
Examples are as follows:
import java.awt *;. public class Ball extends MultiThreadApplet {Image ball = null; Image applet = null; Graphics appletG, ballG; Public void run () {If (ball = = null) {Applet = createlmage (bounds (). Width, bounds (). height); ball = crete image (70, 70); appletg = applet.getgraphics (); ballg = ball.getgraphics (); ballg.setcolor (color.gray); ballg.draworal (0, 0, 70, 70); {for (int x = 0; x <400; x ) {double angle = "((Double) x) / 20;" int y = "(math.abs (Math. SIN (ANGLE) * 80); "Appletg.clearRect (0, 0, Bounds () .width.bounds () (); DrawBackground (appletg.drawImage); Appletg.drawImage (Ball, X.80-Y, this) THIS.GETGRAPHICS () .drawImage (Applet, 0, 0, this); try {thread.sleep (25);} catch (exception ignored) {}}}} G) {for (int i = "0; I" <1; i ) {g.drawline (0.i * 10, 400, i * 10);}}} The small program first uses CreateImage () to obtain the exterior graphics cache that is exactly the same as the small program size. Give the variable applet, then get the cached drawing object Appletg. The rear of the backfraction of the frame is the needle to Appletg. This includes clear frames, draws back views, and draws the drawing in the new position. Finally, use the DrawImage () method to copy the cache to the small program. Run this small program, you will send it to the current painting is not often smooth, and there is no flash in flash. (2) The method of using a double buffer technology is another way to flicker between frames is to use dual buffers, which are used in many animation applets. Its main principle is to create a background image, draw a frame of drawing into the image, then call DrawImage () to draw the entire image on the screen; the advantage is that most of the draw is off the screen, will pass the screen image once The screen is much more effective than drawing directly on the screen, greatly improving the performance of the figure. Double buffer can make the animation smooth, but there is a shortcomings to assign a background image, if the image is quite large, this will require a big memory; when you use double buffer technology, you should overload Update (). Let's take an example of a clock to explain how to process animations.
//AnimatorDemo.javaimport java.util *;. Import java.awt *;. Import java.applet *;. Import java.text *;. Public class AnimatorDemo extends Applet implements Runnable {Thread timer; // for displaying a clock Thread Int Lastxs, Lastys, Lastxm, Lastym, Lastxh, Lastyh; SimpleDateFormat Formatter; // Format Time Display String LastDate; // Save the current time string font clockfacefont; // Set the font of the digital Date CurrentDate in the display clock; / / Display Current Time Color HandColor; // The color color number of the clocked, minute and dials is used to display the color of the second hand and the number of the color public void init () {Int x, y; lastxs = Lastys = lastxm = Lastym = lastxh = LastyH = 0; Formatter = new SimpleDateFormat ("YYYY EEE MMM DD HH: MM: SS"); currentdate = new date (); lastdate = formatter.format (currentdate); clockfacefont = new font ("serif" , Font.Plain, 14); Handcolor = color.blue; NumberColor = color.darkgray; Try {setBackground (New Color (Integer ("BGColor", 16)));} catch } Try {HandColor = New Color (Integer.Parseint (GetParameter ("FGColor1"), 16);} catch (exception e) {} try { Numbercolor = New Color (Integer.Parseint (GetParameter ("fgcolor2"), 16);} catch (exception e) {} resize (300, 300); // Setting the clock window size} // calculate a quarter of the arc Public void PlotPoints (int X0, int y0, int x, int y, graphics g) {g.drawline (x0 x, y0 y, x0 x, y0 y); g.drawline (x0 y, y0 X, X0 Y, Y0 X); g.drawline (X0 Y, Y0-x, X0 Y, Y0-x); g.drawline (X0 X, Y0-Y, X0 X, Y0 -y); g.drawline (X0-x, Y0-Y, X0-X, Y0-Y); g.drawline (X0-Y, Y0-x, X0-Y, Y0-x); g.drawline X0-Y, Y0 X, X0-Y, Y0 X); g.drawline (X0-x, Y0 Y, X0-X, Y0 Y);} // Draw circle with a Bresenham algorithm, where ( X0, Y0) is the center of the circle,
R is a circular radius public void circle (int X0, int y0, int R, graphics g) {Int x, y; float D; x = 0; y = r; d = 5/4-r; plotpoints (x0, y0 , x, y, g); while (y> x) {IF (d <0) {d = D 2 * x 3; x ;} else {d = D 2 * (xy) 5; x , Y -; h = 10, xcenter, ycenter; String today; currentDate = new Date (); SimpleDateFormat formatter = new SimpleDateFormat ( "s", Locale.getDefault ()); try {s = Integer.parseInt (formatter.format (currentDate)) } Catch (number}) {s = 0;} formatter.applypattern ("m"); try {m = integer.parseint (formatter.format (currentdate));} catch (numberformatexception n) {m = 10;} Formatter.ApplyPattern ("h"); try {h = integer.parseint (formatter.format (currentdate));} catch (number}) {h = 10;} formatter.applypattern ("Eee MMM DD HH: mm: ss YYYY "); today = formatter.format (currentdate); // Set the center point of the dial of the clock (80, 55) Xcenter = 80; Ycenter = 55; // a = s * pi / 2 - pi / 2 (to switch 0, 0 from 3:00 to 12:00) // x = r (cos a) xceenter, y = R (SIN A) Ycenter XS = (int) (Math.cos (S * 3.14F / 30 - 3.14F / 2) * 45 xcenter; ys = (int) (Math.SIN (S * 3.14F / 30 - 3.14F / 2) * 45 Ycenter); XM = (int) (Math.cos (M * 3.14F / 30 - 3.14F / 2) * 40 Xcenter); ym = (int) (Math.sin (M * 3.14F / 30 - 3.14F / 2) * 40 Ycenter; xh = (int) (Math.cos ((h * 30 m / 2) * 3.14F / 180 - 3.14F / 2) * 30 xcenter); yh = (int) (math.sin ((h * 30 m / 2) * 3.14F / 180 - 3.14F / 2) * 30
Ycenter); / / Draw the cutting disk outer disc at which the center is 50 g.SetFont (clockfacefont); G.SetColor (Handcolor); Circle (Xcenter, Ycenter, 50, g); // Digital g.SetColor (NumberColor) in the clock disk; g.drawstring ("9", xcenter-45, ycenter 3); g.drawstring ("3", Xcenter 40, Ycenter 3); g.drawstring ("12", Xcenter-5, Ycenter-37); g.drawstring ("6", xcenter-3, ycenter 45); // If necessary, wipe then and redraw G.SetColor (GetBackground ()); IF (xs! = lastxs || ys! = lastys) {g.drawline (xcenter, ycenter, lastxs, lastys); g.drawstring (Lastdate, 5, 125);} if (xm! = lastxm || ym! = Lastym) {g.drawline (Xcenter, Ycenter-1, Lastxm, Lastym); g.drawline (Xcenter-1, Ycenter, Lastxm, Lastym);} if (xh! = lastxh || yh! = lastyh) {g. Drawline (Xcenter, Ycenter-1, Lastxh, Lastyh); G.Drawline (Xcenter-1, Ycenter, Lastxh, Lastyh);} g.SetColor (NumberColor); g.drawstring (", 5, 125); g. DrawString (Today, 5, 125); G. Drawline (Xcenter, Ycenter, XS, Y); G.SetColor; g.drawline (Xcenter, Ycenter-1, XM, YM); g.drawline (xcent ER-1, Ycenter, XM, YM; g.drawline (Xcenter, Ycenter-1, XH, YH); g.drawline (Xcenter-1, Ycenter, XH, YH); Lastxs = XS; Lastys = ys; Lastxm = xm; lastym = ym; lastxh = xh; lastyh = yh; lastdate = Today; currentdate = null;} // applet Startup method PUBLIC VOID Start () {Timer = New Thread (this); Timer.Start (); } // applet stop method public void stop () {Timer = NULL;} // thread RUN () {thread me = thread.currentthread (); while (time (time (time (time) {Try {thread .currentthread (). SLEEP (1000);} catch (interruptedException e) {} repaint ();}} // Note:
The Update () method is renovated here, just call the Paint () method to eliminate the flash phenomenon public void update (graphics g) {Paint (g);}} The following is the contents of AnimatorDemo.html required to run the applet.