import java.awt.*;			// import all classes of package awt
import java.awt.event.*;	// used for the frame-closing-button

/*
* Start of class Colortest
* Simple demonstration of 2-dimensional arrays
* the important part of this demonstration 
* (method initArray(int rows, int columns) )is placed at the
* end of this file between rows 
* vvvvvvvvvv
*    and
* ^^^^^^^^^^
*
* Don't care about any other methods in here, they are built in only for
* graphical reasons and to confuse you.
* Rolf Lakaemper, 1/2003
*/
public class Colortest
extends Frame
{
	// Fields ---------------------------------------------------------------------
	private static int numberOfRows    = 16;
	private static int numberOfColumns = 16;
	private Color[][] colorField;		// the 2 dimensional array of Color-Objects

	
	// Methods --------------------------------------------------------------------
	/*
	* MAIN --- the entry point.
	*/
	public static void main(String[] args)
	{
		Colortest ct=new Colortest();	// create Window
		
		ct.initColorArray(numberOfRows,			// 
						  numberOfColumns);		// Init array !
		
		ct.repaint();					// and show.
										// repaint is inherited from Frame, it's 
										// main task is to call the 'paint'-method
										// which we override with our own paint-method
										// below
	}
	
		
	/*
	* The (only) Constructor
	*/
	private Colortest()					//note that it's private !
	{
		super ("CIS068: Colortest");	// name the frame
		
		addWindowListener(									// don't care about
			new WindowAdapter(){							// this, it simply
				public void windowClosing(WindowEvent event)// activates the frame-
					{System.exit(0);}						// closing button
			}												//
		);													//
		
		// set size of window dependent of array-size
		setSize(numberOfRows*16 + 16,numberOfColumns * 16 + 40);
		setVisible(true);				// the window appears ("there shall be windows") !	
	}
	
	
	/*
	* paint: overrides method paint of superclass Frame
	* called by repaint() or automatically when window
	* changed in any way (size etc.)
	* visualization of colorField
	*/
	public void paint(Graphics g)
	{
		// get array-size
		int rows = colorField.length;			// number of rows
		int columns = colorField[0].length;		// number of columns
		
		for (int c=0;c < columns; c++){
			for (int r=0;r < rows; r++){
				g.setColor(colorField[r][c]);	// color for next rectangle
				int positionX = c * 16 + 8;		// position
				int positionY = r * 16 + 32;	// 8 and 32 are offsets
				g.fillRect(positionX, positionY, 16, 16);	// size = 16 x 16
			}
		}
	}
	
	//-------------------------------------------------------------------------------
	//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
	/*
	* initColorArray
	* initialize 2 dimensional array 'colorField'
	*/
	private void initColorArray(int rows, int columns)
	{
		// A 2 dimensional array is an array of arrays 
		// hence it could be defined row-wise with different
		// column - lengths ! However, the most usual case is a
		// rectangular shape, i.e. every column has the same dimension.
		// These arrays could  be declared in a less complicated way
		// by new arrayname[sizeRow][sizeCol].
		// But to make clear, that we really deal with nested arrays we define
		// them beyond in a more universal way.
		
		colorField = new Color[rows][];
		for (int i = 0; i < rows; i++){
			colorField[i] = new Color[columns];	// every column with same size here
		}
		
		// now define different colors
		for (int r=0; r < rows; r++){
			for (int c = 0; c < columns; c++){
				int red = (int)(255.0 * r / rows);		// compute portions of red 
				int green = (int)(255.0 * c / columns);	// and green by rule of three
				int blue = 0;							// no blue today
				colorField[r][c] = new Color(red, green, blue);
			}
		}
	}
	//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	//--------------------------------------------------------------------------------	
}
