Double buffering

D

David

Hi ,
Does anybody have an example on how to use double buffering to switch a
picture in a java program?

thanks,

david
 
A

ak

David said:
Hi ,
Does anybody have an example on how to use double buffering to switch a
picture in a java program?

thanks,

david

/*
* @(#)DoubleBufferPanel.java 1.2 97/01/14 Jeff Dinkins
*
* Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
*
*/

package actual;

import java.awt.*;
import java.applet.*;

public class DoubleBufferPanel extends Panel {

Image offscreen;

/**
* null out the offscreen buffer as part of invalidation
*/
public void invalidate() {
super.invalidate();
offscreen = null;
}

/**
* override update to *not* erase the background before painting

*/
public void update(Graphics g) {
paint(g);
}

/**
* paint children into an offscreen buffer, then blast entire image
* at once.
*/
public void paint(Graphics g) {
if(offscreen == null) {
offscreen = createImage(getSize().width, getSize().height);
}

Graphics og = offscreen.getGraphics();
og.setClip(0,0,getSize().width, getSize().height);
super.paint(og);
g.drawImage(offscreen, 0, 0, null);

og.dispose();
}

}
 
H

Harald Hein

David said:
Does anybody have an example on how to use double buffering to
switch a picture in a java program?

If you use Swing, you just draw the image inside a paintComponent()
method. Swing does double buffering by default.

If you use AWT, paint to e.g. a BufferedImage, and copy that buffered
image to the screen in a paint() method.
 
A

Alex Hunsley

Harald said:
:




If you use Swing, you just draw the image inside a paintComponent()
method. Swing does double buffering by default.

This is fine for lightweight components, but if you're drawing something
complicated (time consuming), you shouldn't do this.
See my comments starting at http://makeashorterlink.com/?Z2D1246A6 in
comp.lang.java.help.
If you use AWT, paint to e.g. a BufferedImage, and copy that buffered
image to the screen in a paint() method.

.... and don't do it in the GUI thread, if it's time consuming to draw. :)

(Didn't see this thread originally as the same thing was multiposted to
comp.lang.java.help.)


David, if you're going to post to multiple groups (which isn't always
advised), never multipost, it's wasteful. Crosspost instead.

see http://www.blakjak.demon.co.uk/mul_crss.htm

alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top