FileDocCategorySizeDatePackage
BufferedPanel.javaAPI DocJMF 2.1.1e5397Mon May 12 12:20:50 BST 2003com.sun.media.ui

BufferedPanel

public class BufferedPanel extends Panel

Fields Summary
protected boolean
buffered
protected boolean
autoFlushing
protected Image
background
protected boolean
windowCreated
protected transient Image
buffer
protected transient Graphics
bufferGraphics
protected transient Region
damage
protected Object
lock
Constructors Summary
public BufferedPanel(LayoutManager layout)

//	super(layout);
        super ();
        this.setLayout ( layout );

	buffered = true;
	autoFlushing = true;
	background = null;
	windowCreated = false;
	buffer = null;
	bufferGraphics = null;
	damage = new Region();
    
public BufferedPanel()

	this(null);
    
Methods Summary
public voidaddNotify()

	super.addNotify();
	windowCreated = true;
	if (buffered) {
	    createBufferImage();
	    repaint();
	}
    
voidcreateBufferImage()

	Dimension size = getSize();
	
	if ((size.width > 0) && (size.height > 0)) {

	    buffer = createImage(size.width, size.height);
	    if (buffer != null) {
		bufferGraphics = buffer.getGraphics();
	    }
	}
    
public voidflushBuffer()

	Dimension size = getSize();
	super.repaint(0, 0, 0, size.width, size.height);

    
public java.awt.ImagegetBackgroundTile()

	return background;
    
public booleanisAutoFlushing()

	return autoFlushing;
    
public booleanisBuffered()

	return buffered;
    
booleanisLightweight(java.awt.Component comp)

	return comp.getPeer() instanceof java.awt.peer.LightweightPeer;
    
public voidpaint(java.awt.Graphics g)

	if ((buffered) && (buffer != null)) {
	    renderBuffer();
	    g.drawImage(buffer, 0, 0, this);
	} else {
	    super.paint(g);
	}
    
protected voidpaintBackground(java.awt.Graphics g)

	Dimension size = getSize();

	if (background == null) {
	    // Just fill with the background color
	    g.setColor(getBackground());
	    g.fillRect(0, 0, size.width, size.height);
	} else {
	    // Tile the background image to cover the window
	    Rectangle tile = new Rectangle(0, 0, 
					   background.getWidth(this),
					   background.getHeight(this));
	    Rectangle clip = g.getClipBounds();
	    
	    while (tile.y < size.height) {
		while (tile.x < size.width) {
		    if ((clip == null) || (clip.intersects(tile))) {
			g.drawImage(background, tile.x, tile.y, this);
		    }
		    tile.x += tile.width;
		}
		tile.x = 0;
		tile.y += tile.height;
	    }
	}
    
private voidreadObject(java.io.ObjectInputStream is)


          
	 

	is.defaultReadObject();
	damage = new Region();
    
protected voidrender(java.awt.Rectangle rect)

	
	Rectangle clip;
	Component children[] = getComponents();
	Component child;
	Graphics g;

	synchronized(buffer) {
	    // Clear the background
	    bufferGraphics.setClip(rect);
	    paintBackground(bufferGraphics);
	    bufferGraphics.setColor(getForeground());

	    // Paint the children if they are visible and intersect
	    // the current rectangle
	    for (int c = children.length - 1; c >= 0; c--) {
		child = children[c];
		if (isLightweight(child) && child.isVisible()) {
		    clip = child.getBounds();
		    if (clip.intersects(rect)) {
			g = bufferGraphics.create(clip.x, clip.y,
						  clip.width, clip.height);
			child.paint(g);
			g.dispose();
		    }
		}
	    }
	    bufferGraphics.setClip(0, 0, getSize().width, getSize().height);
	}
    
protected voidrenderBuffer()

	Region rects;
	Rectangle rect;

	// Anything need repainting?
	if (damage.isEmpty()) {
	    return;
	}
	
	// Make sure we have a buffer to flush
	if (buffer == null) {
	    return;
	}

	// Keep a copy of the damage region and then reset it
	synchronized(damage) {
	    rects = damage;
	    damage = new Region();
	}
	
	// Cycle through each damage rectangle and paint the children
	// back to front into the buffer image
	for (Enumeration e = rects.rectangles(); e.hasMoreElements(); ) {
	    rect = (Rectangle)(e.nextElement());
	    render(rect);
	}
    
public voidrepaint(long time, int x, int y, int width, int height)

	if (buffered) {
	    synchronized(damage) {
		damage.addRectangle(new Rectangle(x, y, width, height));
	    }
	    if (autoFlushing) {
		flushBuffer();
	    }
	} else {
	    super.repaint(time, x, y, width, height);
	}
    
public voidreshape(int x, int y, int width, int height)

	Rectangle old = getBounds();
	
	super.reshape(x, y, width, height);
	if (windowCreated && 
	    ((width != old.width) || (height != old.height))) {
	    if (buffered) {
		// Resize the image
		createBufferImage();
		repaint();
	    }
	}
    
public voidsetAutoFlushing(boolean flushing)

	if (flushing != autoFlushing) {
	    autoFlushing = flushing;
	}
    
public voidsetBackgroundTile(java.awt.Image background)

	this.background = background;
	repaint();
    
public voidsetBuffered(boolean buffered)

	if (buffered != this.buffered) {
	    this.buffered = buffered;
	    if (buffered) {
		repaint();
	    }
	}
    
public voidupdate(java.awt.Graphics g)

	// It is not necessary to have the system clear the background
	// for us...
	if (buffered) {
	    paint(g);
	} else {
	    super.update(g);
	}