/**
* @(#)BillTransition.java
* @version 1.51 04/06/97
* @author Robert Temple (robertt@starwave.com)
*/

import java.awt.image.MemoryImageSource;
import java.awt.*;
import java.util.Hashtable;

/**
* The BillTransition class is used as a base class for other transition
* classes.  These other classes are what create transition cells between
* two individual billboard images.
*
* NOTE: This class is abstract.  Create subclasses from it.
*/
public abstract class BillTransition {
// Static Members

	/** 
	* Holds static information which can be used on a per applet basis for
	* applets of unique sizes.
	*/
	static Hashtable object_table = new Hashtable(20);

// Instance Members

	/** 
	* The actual images that are created by this transition.  The owner
	* uses these cells to transition from one billboard to the next
	*/
	public Image[] cells;

	/** The delay the owner should use to change from one cell to the next. */
	public int delay;

	/** 
	* Used to provide the Component which is needed to create new images from
	* pixel arrays
	*/
	Component owner;

	/** The number of cells to be created by this transition */
	int number_of_cells;

	/** The width of each cell */
	int cell_w;

	/** The height of each cell */
	int cell_h;

	/** The total number of pixels per cell */
	int pixels_per_cell;

	/** The pixels for the current billboard which is visible on the applet. */
	int[] current_pixels;

	/** The pixels for the next billboard to be displayed on the applet. */
	int[] next_pixels;

	/** A work canvas used to create cells onto */
	int[] work_pixels;

	/** 
	* Used to initialize the transition right after it is created.
	* @param owner the component to be used to create images from cells
	*/
	public abstract void init(Component owner, int[] current_pixels, int[] next_pixels);

	/** 
	* Called by derived classes to initialize the transition and set
	* number_of_cells and delay
	* @param owner				sets the variable: owner
	* @param number_of_cells	sets the variable: number_of_cells
	* @param delay				sets the variable: delay		
	*/
	final protected void init(Component owner, int[] current_pixels, int[] next_pixels, int number_of_cells, int delay) {
		this.delay = delay;
		this.number_of_cells = number_of_cells;
		this.next_pixels = next_pixels;
		this.current_pixels = current_pixels;
		this.owner = owner;

		cells = new Image[number_of_cells];
		cell_w = owner.size().width;
		cell_h = owner.size().height;
		pixels_per_cell = cell_w * cell_h;		
		work_pixels = new int[pixels_per_cell];
	}

	/** 
	* Called by derived classes to initialize the transition and set
	* number_of_cells.  Sets delay to 120 milliseconds.
	* @param owner				sets the variable: owner
	* @param number_of_cells	sets the variable: number_of_cells	
	*/
	final protected void init(Component owner, int[] current_pixels, int[] next_pixels, int number_of_cells) {
		init(owner, current_pixels, next_pixels, number_of_cells, 120);
	}

	/** 
	* Used to create an actual Image from a pixel array
	* @param cell the index into the cell array to store the new image
	*/
	final void createCellFromWorkPixels(int cell) {
		cells[cell] = owner.createImage(new MemoryImageSource(cell_w, cell_h,
					work_pixels, 0, cell_w));
		owner.prepareImage(cells[cell], null);
	}

	/**
	* clean up any resources associated with the cell images
	*/
	final void flushCells() {
		for(int c = 0; c < cells.length; ++c) {
			if(cells[c] != null) {
				cells[c].flush();
			}
		}
	}
}


