Question regarding a double buffer example (at javaworld).

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?)

They probably put the code there so it would be in one place, since they were checking for size changes anyway.

I think it is possibel to change the size of an applety dynamically, but it is not common. I don’t know what the normal method for doing it would be. Perhaps some jscript and manipulating the size in the DOM?

Thanks for your reply. I thought that I would use setResizable(false); in the init() method, but it’s not available. Still I can set my own applet size from that method using (in my case) setSize(400, 400);. A bit strange, I guess it won’t be a problem.

[quote=“swpalmer,post:2,topic:25490”]
I managed to crash both Netscape and IE when I tried that stunt back in the day. These days I’d just do it in AJAX. :slight_smile: