/**
* @(#)ColumnTransition.java
* @version 1.51 04/06/97
* @author Robert Temple (robertt@starwave.com)
*/

import java.awt.image.MemoryImageSource;
import java.awt.*;

/**
* The ColumnTransition class changes one image into another by drawing
* increasingly larger columns of the new image onto the old image.  The
* column sizes increases to the left, and the same pixels are always drawn
* on the left side of each column.  This makes the image appear to be sliding
* in from behind the old image.
*/
public class ColumnTransition extends BillTransition {
// Static Members
	/**
	* The total number of cells this transition will show on the screen before
	* the new image is shown in its entirety
	*/
	final static int CELLS = 7;

	/** The pixel amount a column show grow every cell */
	final static int WIDTH_INCREMENT = 3;

	/**
	* The maximum pixel size a column can grow to be.  This determines how many
	* columns there will be, the width divided by this number.
	*/
	// this number must be evenly divisible by the WIDTH_INCREMENT
	final static int MAX_COLUMN_WIDTH = 24;

// Instance Members

	/**
	* The width of the last column, because the width of the last column will
	* usually not be the same size as the MAX_COLUMN_WIDTH, unless the width
	* of the image is evenly divisible by the MAX_COLUMNS_WIDTH
	*/
	int rightmost_columns_max_width;

	/**
	* starting from the left hand side of the image, the pixel that the last
	* column will start at.
	*/
	int rightmost_columns_x_start;
	/**
	* the current size of the columns in pixels, the number of pixels to
	* draw from the new images onto the old one in this column
	*/
	int column_width = WIDTH_INCREMENT;

	/**
	* Used to initialize the transition right after it is created.
	* creates cells
	* @param owner the component to be used to create images from cells
	*/
	public void init(Component owner, int[] current, int[] next) {
		init(owner, current, next, CELLS, 200);

		rightmost_columns_max_width = cell_w % MAX_COLUMN_WIDTH;
		rightmost_columns_x_start = cell_w - rightmost_columns_max_width;

		// copy the whole of the old image into the work pixel array.
		System.arraycopy((Object)current_pixels, 0, (Object)work_pixels, 
					0, pixels_per_cell);

		// create all the image cells
		for(int c = 0; c < CELLS; ++c) {

			// give other threads a shot at the CPU
			try { Thread.sleep(100); } catch (InterruptedException e) {}

			// draw the next cell into the work pixels
			NextFrame();

			// give other threads a shot at the CPU
			try { Thread.sleep(100); } catch (InterruptedException e) {}

			// create the new cell image from the work pixels
			createCellFromWorkPixels(c);

			// make the column width wider for the next cell
			column_width += WIDTH_INCREMENT;
		}

		// we don't need the work pixels anymore
		work_pixels = null;
	}

	/** 
	* Create the next cell in the work pixel array 
	*/
	void NextFrame() {

		int old_column_width = MAX_COLUMN_WIDTH - column_width;

		// iterate through each row of the image
		for(int p = pixels_per_cell - cell_w; p >= 0; p -= cell_w) {

			// iterate through each column of the image, except the last
			for (int x = 0; x < rightmost_columns_x_start; x += MAX_COLUMN_WIDTH) {

				// copy one row of a column of the new pixels into the work
				// pixels
				System.arraycopy((Object)next_pixels, x + p, (Object)work_pixels,
							old_column_width + x + p, column_width);
			}

			// now do the last column if we need to
			if(old_column_width <= rightmost_columns_max_width) {

				System.arraycopy((Object)next_pixels,
							rightmost_columns_x_start + p, (Object)work_pixels,
							rightmost_columns_x_start + old_column_width + p - 1,
							rightmost_columns_max_width - old_column_width + 1);
			}
		}
	}
}