// Screen.java -- This screen class manages the details of 'double-buffering'
//                for smooth animation. (AWT version)

import java.awt.*;
import java.awt.Image;
import java.awt.Component;
import java.util.Vector;

public class Screen {
	protected Graphics g = null;
	protected Image imageOffscreen = null;
	protected Graphics gOffscreen = null;
	public int x = 0;
	public int y = 0;
	public int width = 0;
	public int height = 0;
	public Color backColor;
	public Color transparentColor;
   
	// This constructor allows us to initialize using a component
	// and a rectangle within that component.  This is the default
	// way to specify an animated area of an applet/application.
	public Screen(Component c, Rectangle r, Color thisBackColor, Color thisTransparentColor) {
		// Get visible screen.
		g = c.getGraphics();
		x = r.x;
		y = r.y;
		width = r.width;
		height = r.height;

		// Get off-screen buffer.
		imageOffscreen = c.createImage(width, height);
		gOffscreen = imageOffscreen.getGraphics();

		// Save colors.
		backColor = thisBackColor;
		transparentColor = thisTransparentColor;
	}
  
	public Graphics getGraphics() {
		// Allow caller to access offscreen buffer for graphics.
		return gOffscreen;
	}
  
	public void erase() {
		// Erase all content in back buffer using background color.
		if (!isValidGraphics())
			return;
		gOffscreen.setColor(backColor);   
		gOffscreen.fillRect(0, 0, width - 1, height - 1);   
	}
     
	public void flip() {
		// Flips back buffer to front buffer -- smooth animation with this 'double buffering'.
		g.drawImage(imageOffscreen, x, y, null);
	}
   
	public boolean isValidGraphics() {
		return (g != null && gOffscreen != null);
	}

}
