![]() |
Double-buffering
PrerequisitesTo perform smooth screen updates for drawing and animation, a technique called double-buffering is often used. Double-buffering provides a means of performing redraws in a background buffer (thus hiding it from the viewer), which is then in one smooth motion, copied to the visible drawing buffer. This technique is used extensively in the animations for the Thread Magercises. In this Magercise, we are going to use double-buffering for another, important reason--the background buffer will record the set of elements we have drawn (recall that Java erases the applet drawing region each time it repaints unless you modify the definition of method update). We will combine the previous Rubberbanding Magercise so that rectangles are drawn on the visible screen during rubberbanding, but copied to the background "save" buffer. How do we rubberband on top of an already-present image without erasing part of it as we drag across? Exclusive OR mode. XOR mode allows you to draw once (perturbing the background very slightly) and draw again, erasing without affecting the background.
Work LocationPerform all work for this magercise in VisualAge project MageLang Magercises, package magercises.doublebuffer. If this project does not appear in your Workspace add it from the repository (if it exists there) or create a new project using this name. Tasks
Perform the following tasks:
Define update so that it simply calls paint. Normally it clears the graphics region before calling paint. Because we will copy over the entire graphics region in our paint, it is a waste of time and a source of flicker to have update erase the screen. Define paint so that it copies the background buffer on the canvas. Draw a border around the canvas 0,0,width-1,height-1. Define mouse down to set the upper-left corner of the rectangle (downX, downY). Just like in the rubberbanding Magercise. Reset width and height (w, h). Define mouseDrag so that it erases the old image on the visible canvas and draws an updated image at new location. Use XOR mode so the background image is not disturbed. Use method clip to restrict the dragging to the region of the canvas. Draw the rubberbanding rectangle in red, but draw final rectangle in black. Define mouseUp to draw a rectangle on the background buffer and call repaint to schedule a canvas repaint.
|
Copyright © 1996-1997 MageLang Institute. All Rights Reserved. |