/*
 *	
 *  Filename: Grid.java
 *  Grid class to be used by
 *  Author: Achim E Karger
 *  Course: CSCI 111 Java I
 *  Assignment: Lab 5
 *  Date:   March 10, 2008
 *  compiler JCreator LE
 *  Client web application: lab5.html
 *  
 *
 */
import java.awt.Graphics;
import java.awt.Color;

/**
 * The Grid class displays a grid w/ filled squares on the computer screen.
 * The user can set the position on the screen, number of squares in either direction and the grid spacing
 * all 13 pre-defined colors avaliable in the Color class are displayed in the grid
 * 
 * @author  Achim E Karger
 * @version 1.0 03/10/08
 * @see     java.lang.System
 */
public class Grid{
		 //  x,y grid upper left corner coordinates (pixel)
     	 //  grid number of rows and cols, size of grid square(pixel)
    private int x,y,nrow,ncol,pitch;    
	private Color[] myColors = {Color.black, Color.blue, Color.cyan, Color.gray, Color.darkGray, Color.lightGray,
			Color.green, Color.magenta, Color.orange, Color.pink, Color.red, Color.white, Color.yellow};
	
	// constructor
	public Grid(int x,int y,int nrow,int ncol,int pitch){
	  this.x=x;
	  this.y=y;
	  this.nrow=nrow;
	  this.ncol=ncol;
	  this.pitch=pitch;
	} // end constructor

/**
*  draw() draws a grid x,y grid upper left corner coordinates (pixel),
*   grid number of rows and cols, size of grid square(pixel)  
*  
*/
	public void draw(Graphics g){
	int i;
	for(i=0;i<=nrow;i++)
		g.drawLine(x,y+i*pitch,x+ncol*pitch,y+i*pitch);
	for(i=0;i<=ncol;i++)
		g.drawLine(x+i*pitch,y,x+i*pitch,y+nrow*pitch);
	} // end method draw()

/**
 *  fill() fills the first squares of the grid , starting at the upper left corner 
 *  with all of the  13 predefined colors avaliable in the Color class 
 *  
 */
	public void fill(Graphics g){
	int i,j;
	int count  = 0;	
	for(i=0;i<nrow;i++)                               // 'slow' index      
			for(j=0;j<ncol;j++) {					  // 'fast' index
	            if (count<myColors.length){           // fill grid squares w/ predefined colors ... 
	            	g.setColor(myColors[count]);
					g.fillRect(x+j*pitch+1,y+i*pitch+1,pitch-2,pitch-2);
					count++;
				}
				else {							    	// until we run out of colors
					g.setColor(Color.white);
					g.fillRect(x+j*pitch+2,y+i*pitch+2,pitch-3,pitch-3); // use 2 pixel offset from the grid lines
				}
			}
	
	} // end method fill()

} // end class Grid
		
		
		

