Double Buffering Java Help

Double Buffering
Not only are images useful for storing pictures, as we’ve just shown, but you can. also use them as offscreen drawing surfaces. This allows you to render any image including ‘text and graphics, to an. offscreen buffer that you can display at a later time. The advantage to doing this is that the image is seen only when it is complete. Drawing a complicated image could take several milliseconds or more, which can be seen by the user as flashing or flickering. This flashing is distracting and causes the user to perceive your-rendering as slower than It-actually is. Use of an offscreen image to reduce flicker is called double image  because Screen is considered a’buffer f6r pixels, and the offscreen image is the second buffer, where you can prepare pixels for display Earlier in this chapter, you saw how to create a blank Image object.Now you will see how to draw on .that image. rather than the screen. As .you recall from earlier  you need a Graphics object in order to use-any of Iava’s rendering methods, Conveniently, the’ Graphics object that you can use to draw-on an Image is available via the getGraphics method.Here is a code fragment that creates anew image  obtains graphics context, and fills the.entire image with red pixels: Canvas new Canvas ()

Image test = c.createImage(200, 100)

Graphics gc = test.getGraphico()
gc. setColor (Color, 0, 200, 100)

Once you have constructed and filled an offscreen image, it will still not be visible. To .actually display the image, call drawImage( )  Here is an example that draws a time-consuming image, to demonstrate the difference that double buffering can make in perceived drawing time This simple apple thas n complicated paint f( ) method. It fills the background with blue and then draws n red moire pattern on top of that. It paints some black text on top of that and then paints a yellow circle centered at the coordinates mx,my. The mouseMoved( ) and mouse dragged ( ) methods are overridden to track the mouse position. These methods are identical, except for the setting of the flicker Boolean variable. mouseMoved( ) sets flicker to true, and mouseDragged( ) sets it to false. This has the effect of calling repaint( ) with flicker set to true when the mouse is moved (but no button is pressed) and set to false when the mouse is dragged with any button pressed. When paint! ) gets called with flicker set to true, we see each drawing operation as it is executed on the screen. In the .case where a mouse button is pressed and paint( ) is called with flicker set to false, we see quite a different picture ..The paint( ) method swaps the Graphics reference g with the graphics context ~hat refers to the offscreen canvas, buffer, which we created in initi. Then all of the drawing operations are invisible. At the end. of paint(), we simply call drawImage() to show ‘the results of these drawing ‘methods all at once Notice that it is okay to pass in a null as the fourth parameter to drawImage( ). This is the parameter used to pass an ImageObserver object-that receives notification of image events. since this is an image that is not being produced from a network stream, we have no need for notification. The left snapshot  is what the applet looks like with the mouse buttons not pressed. As you en” see, the image was in the middle of repainting when this snapshot was taken. The right snapshot shows how when a mouse button is pressed, the image, is always complete and clean due to double buffering.

  • Feel free to send us an inquiry, we will reply back in hours.


Posted on September 18, 2014 in The Complete Reference Images

Share the Story

About the Author

Back to Top