Java animation programming basis Part III

zhaozj2021-02-08  466

Double buffer technology: Another method of flickering between frames is to use dual buffers, which are used in many animation applets. The main principle is to create a background image, draw a frame into the image, then call DrawImage () draws the entire image once on the screen. The advantage is that most of the drawings are away from the screen. Painting the off-screen image to the screen is much more effective than drawing it directly on the screen. Double buffer can make the animation smooth, but there is a shortcoming, to assign a background image, if the image is quite large, this will require a big memory. Update () should be overloaded when you use double buffer technology. Dimension offDimension; Image offImage; Graphics offGraphics; public void update (Graphics g) {Dimension d = size ();! If ((offGraphics == null) || (d.width = offDimension.width) || (d.height ! = offDimension.height)) {offDimension = d; offImage = createImage (d.width, d.height); offGraphics = offImage.getGraphics ();} offGraphics.setColor (getBackground ()); offGraphics.fillRect (0, 0 , d.Width, D.Height; offgraphics.setColor (color.black); PaintFrame (offgraphics); g.drawImage (Offimage, 0, 0, null);} public void Paint (Graphics G) {IF (Offimage! = NULL) {g.drawImage (OFFIMAGE, 0, 0, NULL);}} public void PaintFrame (GRAPHICS G) {Dimension D = Size (); int h = d.Height / 2; for (int x = 0; x

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

New Post(0)