/*
 * MirrorCanvas - a double buffered canvas
 *
 * by Adam Doppelt
 * http://www.cs.brown.edu/people/amd/
 */
import java.awt.*;

public class MirrorCanvas extends Canvas {
    Image image_;
    Graphics offscreen_, onscreen_;
    int width_, height_;
    
    public MirrorCanvas(int width, int height) {
	resize(width, height);
	width_ = width;
	height_ = height;
	offscreen_ = null;
	onscreen_ = null;
    }

    public void Flip() {
	if (onscreen_ != null)
	    onscreen_.drawImage(image_, 0, 0, null);
	else
	    repaint();
    }

    public void update (Graphics g) {
	paint(g);
    }
    
    public synchronized void paint (Graphics g) {
        if (offscreen_ == null) {
            resize(width_, height_);
            image_ = createImage(width_, height_);
            offscreen_ = image_.getGraphics();
	    onscreen_ = getGraphics();
        }

        g.drawImage(image_, 0, 0, null);
    }
}
