30
Sep
To use double-buffering in your own applets, paste
To use double-buffering in your own applets, paste in the declarations of the image and offscreen buffer, and modify init() and paint() as has just been discussed. You ll also need to override update() so it doesn t clear the screen: public void update(Graphics g) { paint(g) } Voil flicker-free animation! Next, let s discuss a simple way of improving the performance of the animation. Using Clipping to Improve Performance If you look carefully at the Broadway applet again, you ll notice that the animation takes place in the center of the screen, and the surrounding portions remain unchanged. As a result, dumping the entire offscreen buffer to the screen seems to be a waste. Ideally, only the part of the offscreen buffer that changed should be copied. Java makes it easy to specify the portion of the graphics buffer that needs to be modified by subsequent graphics operations. Defining a clipping rectangle for a graphics context limits future changes to the interior and edges of that rectangle. In the case of Broadway, the clipping rectangle might be the maximum region that the moving rectangle traverses, as shown in Figure 2-7. Figure 2-7 Clipping rectangle for Broadway To define a clipping region, use the Graphics method clipRect(): // sets clipRect to rectangle at (x,y) with the // specified width and height g.clipRect(int x, int y, int width,int height); Now Broadway s update() can be modified in the following way: public void update(Graphics g) { g.clipRect(70,90,130,110); paint(g); } You might be wondering why the call to g.clipRect() occurs in update(), instead of in the paint()
Note: If you are looking for good and quality webspace to host and run your java application check Actions java hosting services
