Hi!
I have looked at http://www.javaworld.com/javaworld/jw-03-1996/jw-03-animation-p2.html and its example 7. It is an example demonstrating moving images on top of each other using double buffering as the update method (implemented “by hand”, not using strategy).
Its update method looks like this:
`
public void update(Graphics g) {
Dimension d = size();
// Create the offscreen graphics context
if ((offGraphics == null)
|| (d.width != offDimension.width)
|| (d.height != offDimension.height)) {
offDimension = d;
offImage = createImage(d.width, d.height);
offGraphics = offImage.getGraphics();
}
// Erase the previous image
offGraphics.setColor(getBackground());
offGraphics.fillRect(0, 0, d.width, d.height);
offGraphics.setColor(Color.black);
// Paint the frame into the image
paintFrame(offGraphics);
// Paint the image onto the screen
g.drawImage(offImage, 0, 0, null);
}
`
The complete applet code with update() within its example context.
I am wondering why the part titled “Create the offscreen graphics context” in that method checks if the offGraphics exists, and if it doesn’t creates it. Why didn’t they create it in the init() method instead? They also check if the size has changed, can the size of an applet change at run time? (And if they can, can i prevent this so that I don’t need to check for creation at each update() call?)
