30
Sep
In this section, you ll learn the cause of
Figure 2-6 Double-buffering in action Let s translate this into Java. First, you must declare an offscreen buffer, using the following syntax: Graphics offscreen; // Declaration of offscreen buffer Image image; Think of an Image as a complete picture that can be blasted to the screen. (You ll learn more about images in Chapter 3, Animating Sprites.) The actual allocation of the offscreen buffer should take place in the init() method of the applet: public void init() { … other initializations … image = createImage(width,height); // allocation of offscreen offscreen = image.getGraphics(); // buffer } The Image method getGraphics() returns the graphics context associated with the image. Now modify paint() so that it draws to the offscreen buffer, instead of to the original graphics context. You must clear the offscreen buffer now to prevent getting trails. When the offscreen buffer is ready, dump it to the screen by using drawImage(). For example, here s how Broadway s paint() would be changed to implement double-buffering. Note that the Graphics method drawImage() paints image to the screen at coordinates (0,0) of the applet s graphics context. public void paint(Graphics g) { offscreen.setColor(Color.black); offscreen.fillRect(0,0,300,300); // clear buffer offscreen.setColor(Color.yellow); offscreen.fillRect(0,0,90,90); offscreen.fillRect(250,0,40,190); … offscreen.setColor(Color.magenta); offscreen.fillRect(200,55,60,135); g.drawImage(image,0,0,this); // draw offscreen buffer // to screen }
Note: If you are looking for cheap and quality provider to host and run your java application check Astra java hosting services
