// F.Michael O'Brien
// fmobrien@home.com, michael_obrien@mitel.com
// LifeAnimApplet.java
// June 04 1999 - Oct 31 1999 
// adapted based on ...
/**
 * History:
 * 20071229 - refactored glider gun code to handle NW direction
 */

import java.applet.Applet;
import java.awt.*;
import java.util.StringTokenizer;
import java.lang.Exception;
import java.awt.event.*;


public class LifeAnimApplet extends Applet implements MouseListener, MouseMotionListener, ActionListener {
   private int 	xPos, yPos = 0;
   private String s = "";
   private static boolean	bvRandomize;
   private static int	counter=0,				
				ivGridCounter=0,
				izGridHeight=200,
				izGridWidth=200,
				ivGeneration=98,
				ivFirstTime=0,
				ivGridGreen2=0,
				izMaxWidth=400,
				izMaxHeight=400,
				izRandomizeReset=100,
				izRandomize=0,
				izCellSize=2,
               izSleepTime=1;    // milliseconds to sleep
	//private Integer izMaxWidth;
	//private Integer izMaxHeight;
	//private Integer ivoHolder;

	private Color color1, color2;
	// reference grid boolean, setby mouseEvent(in, out)
	private int ivGridOn=0;

	private long ivGridTotal1=0,			   
			   	ivGridTotal2=0;  

	private	int ivNext[][];
	private	int ivPrev[][];
	private	int izT[]={0,0,1,2,0,0,0,0,0,0};

   	// The next two objects are for double-buffering
   	private Graphics gContext; // off-screen graphics context 
   	private Image buffer;      // buffer in which to draw image

	// popup menu
	private String svPopupNames[] = {"Erase","Reset","Randomize"};
	private MenuItem thePopupMenuItems[];
	private	PopupMenu aPopupMenu;

   	// load the images when the applet begins executing
   	public void init()
   	{
		int ivCount=0;
		int x,y;
		int izPresets=0;
		String szParameter;
		int izPresetx,izPresety;

		// set colors
		color1 = Color.blue;
		color2 = Color.white;

		// applet listen for mouse events, ok
		addMouseListener(this);
		addMouseMotionListener(this);
		
	    try {
		if (getParameter("transSequence09") != null) {
			// extract out the grid transition sequence 0 - 9
		    StringTokenizer Points = new StringTokenizer (getParameter ("transSequence09"),"\t\n\r ,;.");	

			ivCount=0;
	    	while (Points.hasMoreTokens()) {
				izT[ivCount++]= new Integer(Points.nextToken()).intValue();
 			}
		}
	    } catch (Exception e) {}
			
			//if(Integer.parseInt(getParameter("transSequence09"))==1)
			//	izT[0]=1;
			//else
			//	if(Integer.parseInt(getParameter("transSequence0"))==2)
			//		izT[0]=2;
			//	else
			//		izT[0]=0;
		izMaxHeight=Integer.parseInt(getParameter("maxHeight"));
		izMaxWidth=Integer.parseInt(getParameter("maxWidth"));
		izRandomizeReset=Integer.parseInt(getParameter("randomizeReset"));
		izRandomize=Integer.parseInt(getParameter("randomize"));
		izCellSize=Integer.parseInt(getParameter("cellSize"));
		System.out.println("maxHeight " + (new Integer(izMaxHeight)).toString());
		System.out.println("maxWidth " + (new Integer(izMaxWidth)).toString());
		System.out.println("randomizeReset " + (new Integer(izRandomizeReset)).toString());
		System.out.println("randomize " + (new Integer(izRandomize)).toString());

		ivGeneration=izRandomizeReset-12;
		
		izGridHeight=(int)Math.floor(izMaxHeight/izCellSize);
		izGridWidth=(int)Math.floor(izMaxWidth/izCellSize);
		ivNext=new int[izGridWidth][izGridHeight];
		ivPrev=new int[izGridWidth][izGridHeight];
		//izT=new int[10];

		// create popup menu
		aPopupMenu = new PopupMenu("Popup");
		thePopupMenuItems = new MenuItem[svPopupNames.length];

		for(int i=0;i<svPopupNames.length;i++)
		{
			thePopupMenuItems[i] = new MenuItem(svPopupNames[i]);
			aPopupMenu.add(thePopupMenuItems[i]);
			thePopupMenuItems[i].addActionListener(this);
		}

		// add popup menu to this canvas
		add(aPopupMenu);
		enableEvents(AWTEvent.MOUSE_EVENT_MASK);

		// create graphics subsystem
      	buffer = createImage(izMaxWidth,izMaxHeight); // create image buffer
      	gContext = buffer.getGraphics(); // get graphics context

      	// set background of buffer to black
      	gContext.setColor(Color.black);	  
      	gContext.fillRect(0,0,izMaxWidth,izMaxHeight+30);

		// place a bargraph template to warn user
		//gContext.setColor(Color.red);
		//for(int i=0;i<101;i+=4)
		//	gContext.fillRect(10+(int)Math.floor(i/4)*2,10,1,4);

		// place some text to warn user
		gContext.setColor(Color.cyan);
		gContext.drawString("Initializing Grid....",10,40);
		gContext.fillRect(0,izMaxHeight+1,izMaxWidth,2);
		gContext.drawString("FMO 1999",10,izMaxHeight+16);

		// initialize grid
		// reset previous and next grid to zero
		for(int i=0;i<izGridWidth;i++)
			for(int j=0;j<izGridHeight;j++)
				ivNext[i][j]=ivPrev[i][j]=0;

		// setup the transition sequence

		// preset some values
		
	   	//loadGosperGliderGunSE(50,50);
		izPresets=Integer.parseInt(getParameter("presets"));
		for(int i=1;i<izPresets+1;i++)
		{
			szParameter = getParameter("preset" + Integer.toString(i));
			izPresetx = Integer.parseInt(getParameter("preset" + Integer.toString(i)+"x"));
			izPresety = Integer.parseInt(getParameter("preset" + Integer.toString(i)+"y"));
			System.out.println("izPresetx = " + izPresetx);
			System.out.println("izPresety = " + izPresety);									
		if(szParameter.equals("gliderGunSE"))
			loadGosperGliderGunSE(izPresetx,izPresety);
		if(szParameter.equals("gliderGunNE"))
		   	loadGosperGliderGunNE(izPresetx,izPresety);
		if(szParameter.equals("gliderGunSW"))
			loadGosperGliderGunSW(izPresetx,izPresety);
		if(szParameter.equals("gliderGunNW"))
			loadGosperGliderGunNW(izPresetx,izPresety);

		}
//		for(int i=20;i<80;i++)
//			for(int j=20;j<80;j++)
//			{
//				if((int)Math.floor(Math.random()*100+1)>50)
//		 			ivPrev[i][j]=1;
//			}
	}



	private void setValues(String event, int x, int y)
	{
		s=event; xPos=x; yPos=y; //repaint();
	}

	public void mouseClicked(MouseEvent e)
	{ setValues("Clicked",e.getX(),e.getY());}

	public void mousePressed(MouseEvent e)
	{ 
		//ivGeneration = izRandomizeReset-10;
		setValues("Pressed",e.getX(),e.getY());
		ivPrev[(int)Math.floor(e.getX()/izCellSize)][(int)Math.floor(e.getY()/izCellSize)]=1;
	}

	public void mouseReleased(MouseEvent e)
	{ 
		setValues("Released",e.getX(),e.getY());
		ivPrev[(int)Math.floor(e.getX()/izCellSize)][(int)Math.floor(e.getY()/izCellSize)]=1;
	}

	public void mouseEntered(MouseEvent e)
	{ 
		setValues("Entered",e.getX(),e.getY());
		//color2 = Color.cyan;
		ivGridOn=1;
	}

	public void mouseExited(MouseEvent e)
	{ 
		setValues("Exited",e.getX(),e.getY());
		//color2 = Color.white;
		ivGridOn=0;
	}

	public void mouseDragged(MouseEvent e)
	{
		setValues("Dragging",e.getX(),e.getY());
		ivPrev[(int)Math.floor(e.getX()/izCellSize)][(int)Math.floor(e.getY()/izCellSize)]=1;
	}

	public void mouseMoved(MouseEvent e)
	{ 
		setValues("Moving",e.getX(),e.getY());
		//ivPrev[(int)Math.floor(e.getX()/2)][(int)Math.floor(e.getY()/2)]=1;
	}

	// capture popup menu event
	public void processMouseEvent(MouseEvent e)
	{
		if(e.isPopupTrigger())
			aPopupMenu.show(this,e.getX(),e.getY());
		super.processMouseEvent(e);
	}
	
	// process popup menu event
	public void actionPerformed(ActionEvent e)
	{
		//"Erase"
		if(e.getSource()==thePopupMenuItems[0])
		{
		// reset previous and next grid to zero
		for(int i=0;i<izGridWidth;i++)
			for(int j=0;j<izGridHeight;j++)
				ivNext[i][j]=ivPrev[i][j]=0;
		}
		
		//"Reset"
		if(e.getSource()==thePopupMenuItems[1])
		{
			init();
		}
		
		
		//"Randomize"
		if(e.getSource()==thePopupMenuItems[2])
		{
			ivGeneration = izRandomizeReset-10;
		}
	}

   	// start the applet
   	public void start()
   	{
   	}

   	// display the image in the Applet's Graphics context
   	public void paint(Graphics g)		
   	{
		int x0,y0,x1,y1,xp,yp,zp,ivCell,ivSurround;		
      	// set background of buffer to black
      	//g.setColor(Color.black);
      	//g.fillRect(0,0,izMaxWidth,izMaxHeight);
		//if(ivFirstTime>100)
		//{
	      	g.drawImage(buffer,0,0,this);
	      	// clear previous image from buffer
	      	gContext.setColor(Color.black);
    	  	gContext.fillRect(0,0,izMaxWidth,izMaxHeight);
		//}
		//else
		//{
		//	ivFirstTime++;
	    //  	g.drawImage(buffer,0,0,this);
		//	gContext.setColor(Color.white);
		//	gContext.fillRect(11+(int)Math.floor(ivFirstTime/4)*2,10,1,4);

			//g.setColor(Color.red);
			//g.drawRect(0,0,100,100);
			//gContext.setColor(Color.red);
			//gContext.fillRect(0,0,100,100);
	
		// display grid

		// grid has become static, randomize the center values
		//if(ivGridTotal1==ivGridTotal2)

		if(izRandomize==1)
		{
			if(ivGeneration > izRandomizeReset-10)
			{
				gContext.setColor(Color.cyan);
				gContext.drawString("Randomizing Grid",(int)Math.floor(izMaxWidth/2)-45,15);
			}
		
			if(ivGeneration++ >izRandomizeReset)
			{			
				ivGeneration=0;
				for(int i=20;i<izGridWidth-20;i++)
				{
					for(int j=20;j<(int)Math.floor(izGridHeight/2);j++)
						if((int)Math.floor(Math.random()*100+1)>50)
	 						ivPrev[i][j]=1;

					for(int j=(int)Math.floor(izGridHeight/2);j<izGridHeight-20;j++)
						if((int)Math.floor(Math.random()*100+1)>85)
			 				ivPrev[i][j]=1;
				}}}
		else
			ivGeneration++;

		ivGridTotal1=ivGridTotal2;
		ivGridTotal2=ivGridGreen2=0;

		// draw grid if enabled
		if(ivGridOn==1)
		{
			//gContext.setColor(Color.green);		
			if(ivGridCounter<20)
				ivGridCounter++;
			else
				ivGridCounter=0;
			if(ivGridCounter<10)
				gContext.setColor(Color.green);
			else
				gContext.setColor(Color.red);		
	   		for(int i=0;i<izMaxHeight;i+=(izCellSize*2))
			{
	   			gContext.fillRect(xPos,i,izCellSize-1,izCellSize-1);
	   			//gContext.fillRect(xPos-i,yPos,1,1);
			}
	   		for(int j=0;j<izMaxWidth;j+=(izCellSize*2))
			{
	   			gContext.fillRect(j,yPos,izCellSize-1,izCellSize-1);
	   			//gContext.fillRect(xPos,yPos-j,1,1);
			}
		 }
	
		for(int i=1;i<izGridWidth-2;i++)
		{
			//System.out.println(i);
			for(int j=1;j<izGridHeight-2;j++)
			{
				ivSurround=ivPrev[i-1][j-1]+ivPrev[i][j-1]+ivPrev[i+1][j-1]+
					ivPrev[i-1][j]+ivPrev[i+1][j]+//ivPrev[i][j]+
					ivPrev[i-1][j+1]+ivPrev[i][j+1]+ivPrev[i+1][j+1];
				//System.out.println(ivSurround);
				ivCell=izT[ivSurround];
				ivGridTotal2+=ivSurround;

				//System.out.println(ivCell);

				if(ivPrev[i][j]==0)
				{
					if(ivCell==2)
					{
						if(ivGeneration > izRandomizeReset-6)
							gContext.setColor(color1);
						else
							gContext.setColor(Color.red);

						ivNext[i][j]=1;
						ivGridGreen2++;
						gContext.fillRect(i*izCellSize,j*izCellSize,izCellSize,izCellSize);
						//gContext.fillRect(i*2,j*2,i*2,j*2);
					}
					else
					{
						ivNext[i][j]=0;
      				}
				}
				else
				{
					if(ivCell==0)
					{
						//gContext.setColor(Color.black);					
						ivNext[i][j]=0;
						//gContext.fillRect(i*2,j*2,1,1);
      				}
					else
					{
						gContext.setColor(color2);
						ivNext[i][j]=1;											  
						gContext.fillRect(i*izCellSize,j*izCellSize,izCellSize,izCellSize);
						//gContext.fillRect(i*2,j*2,i*2,j*2);
					}}}}

		// transfer new grid to old
		for(int i=0;i<izGridWidth;i++)
			for(int j=0;j<izGridHeight;j++)
				ivPrev[i][j]=ivNext[i][j];
				
		//gContext.setColor(getBackground());
		//if(counter>30){counter=0;}
		counter++;
		//if(counter > 100)
		//   	g.setColor(Color.green);
		//else
		//   	g.setColor(Color.blue);
   	  	//g.fillRect(450,0,2, counter);
		g.setColor(Color.black);
   	  	g.fillRect(0,izMaxHeight,izMaxWidth,30);
		//g.setColor(Color.blue);
   	  	//g.fillRect(0,izMaxHeight+1,izMaxWidth,2);
		g.setColor(Color.cyan);
		//g.drawString("FMO 1999 :",10,izMaxHeight+16);
		//ivoHolder.value=ivGeneration;
		for(int i=0;i<10;i++)
			g.drawString((new Integer(izT[i])).toString(),86+6*i,izMaxHeight+16);
		g.drawString((new Integer(ivGeneration)).toString(),25,izMaxHeight+16);
		g.setColor(Color.green);
		g.drawString("G:",0,izMaxHeight+16);		
		g.drawString("T:",60,izMaxHeight+16);		
		g.setColor(Color.blue);
		g.fillRect(0,izMaxHeight+27,(int)Math.floor(ivGridGreen2/5),2);
		g.setColor(Color.white);
		
		g.fillRect(0,izMaxHeight+24,(int)Math.floor((ivGridTotal2-ivGridGreen2)/100),2);
		//g.fillRect(10,izMaxHeight+3,(int)Math.floor(izRandomizeReset/2),2);
		//g.setColor(Color.green);
   	  	//g.fillRect(10,izMaxHeight+3,(int)Math.floor(ivGeneration/2),2);		

      	try {Thread.sleep( izSleepTime );}
      	catch ( InterruptedException e ){showStatus(e.toString());}
      	repaint();  // display buffered image
   	}

   	// override update to eliminate flicker
   	public void update(Graphics g){paint(g);}

	private void loadGliderHeadingSE(int x, int y)
	{
		// origin is at 0,0 lower right
		ivPrev[x][y-1]=1;
		ivPrev[x-1][y]=1;
		ivPrev[x-1][y-1]=1;
		ivPrev[x-2][y-2]=1;
		ivPrev[x][y-2]=1;
		// end of glider
	}

	private void loadGosperGliderGunNE(int x, int y)
	{
		// origin is at 0,0 lower right
		ivPrev[x][y+1]=1;
		ivPrev[x-1][y]=1;
		ivPrev[x-1][y+1]=1;
		ivPrev[x-2][y+2]=1;
		ivPrev[x][y+2]=1;
		// end of glider
		ivPrev[x+10][y+8]=1;
		ivPrev[x+10][y+9]=1;
		ivPrev[x+11][y+8]=1;
		ivPrev[x+11][y+9]=1;

		ivPrev[x+6][y+6]=1;
		ivPrev[x+6][y+7]=1;
		ivPrev[x+1][y+5]=1;
		ivPrev[x+1][y+6]=1;
		ivPrev[x+1][y+10]=1;
		ivPrev[x+1][y+11]=1;

		ivPrev[x][y+6]=1;
		ivPrev[x][y+7]=1;
		ivPrev[x][y+8]=1;
		ivPrev[x][y+9]=1;
		ivPrev[x][y+10]=1;
		ivPrev[x-1][y+6]=1;
		ivPrev[x-1][y+7]=1;
		ivPrev[x-1][y+9]=1;
		ivPrev[x-1][y+10]=1;
		ivPrev[x-2][y+6]=1;
		ivPrev[x-2][y+7]=1;
		ivPrev[x-2][y+9]=1;
		ivPrev[x-2][y+10]=1;
		ivPrev[x-3][y+7]=1;
		ivPrev[x-3][y+8]=1;
		ivPrev[x-3][y+9]=1;
		ivPrev[x-8][y+5]=1;
		ivPrev[x-8][y+6]=1;
		ivPrev[x-8][y+7]=1;
		ivPrev[x-9][y+5]=1;
		ivPrev[x-9][y+6]=1;
		ivPrev[x-9][y+7]=1;
		ivPrev[x-10][y+4]=1;
		ivPrev[x-10][y+8]=1;
		ivPrev[x-11][y+3]=1;
		ivPrev[x-11][y+9]=1;
		ivPrev[x-12][y+4]=1;
		ivPrev[x-12][y+8]=1;
		ivPrev[x-13][y+5]=1;
		ivPrev[x-13][y+6]=1;
		ivPrev[x-13][y+7]=1;

		ivPrev[x-23][y+6]=1;
		ivPrev[x-23][y+7]=1;
		ivPrev[x-24][y+6]=1;
		ivPrev[x-24][y+7]=1;
	}

	private void loadGosperGliderGunSE(int x, int y)
	{
		// origin is at 0,0 lower right
		ivPrev[x][y-1]=1;
		ivPrev[x-1][y]=1;
		ivPrev[x-1][y-1]=1;
		ivPrev[x-2][y-2]=1;
		ivPrev[x][y-2]=1;
		// end of glider
		ivPrev[x+10][y-8]=1;
		ivPrev[x+10][y-9]=1;
		ivPrev[x+11][y-8]=1;
		ivPrev[x+11][y-9]=1;

		ivPrev[x+6][y-6]=1;
		ivPrev[x+6][y-7]=1;
		ivPrev[x+1][y-5]=1;
		ivPrev[x+1][y-6]=1;
		ivPrev[x+1][y-10]=1;
		ivPrev[x+1][y-11]=1;

		ivPrev[x][y-6]=1;
		ivPrev[x][y-7]=1;
		ivPrev[x][y-8]=1;
		ivPrev[x][y-9]=1;
		ivPrev[x][y-10]=1;
		ivPrev[x-1][y-6]=1;
		ivPrev[x-1][y-7]=1;
		ivPrev[x-1][y-9]=1;
		ivPrev[x-1][y-10]=1;
		ivPrev[x-2][y-6]=1;
		ivPrev[x-2][y-7]=1;
		ivPrev[x-2][y-9]=1;
		ivPrev[x-2][y-10]=1;
		ivPrev[x-3][y-7]=1;
		ivPrev[x-3][y-8]=1;
		ivPrev[x-3][y-9]=1;
		ivPrev[x-8][y-5]=1;
		ivPrev[x-8][y-6]=1;
		ivPrev[x-8][y-7]=1;
		ivPrev[x-9][y-5]=1;
		ivPrev[x-9][y-6]=1;
		ivPrev[x-9][y-7]=1;
		ivPrev[x-10][y-4]=1;
		ivPrev[x-10][y-8]=1;
		ivPrev[x-11][y-3]=1;
		ivPrev[x-11][y-9]=1;
		ivPrev[x-12][y-4]=1;
		ivPrev[x-12][y-8]=1;
		ivPrev[x-13][y-5]=1;
		ivPrev[x-13][y-6]=1;
		ivPrev[x-13][y-7]=1;

		ivPrev[x-23][y-6]=1;
		ivPrev[x-23][y-7]=1;
		ivPrev[x-24][y-6]=1;
		ivPrev[x-24][y-7]=1;
	}

	private void loadGosperGliderGunSW(int x, int y)
	{
		// origin is at 0,0 lower right
		ivPrev[x][y-1]=1;
		ivPrev[x+1][y]=1;
		ivPrev[x+1][y-1]=1;
		ivPrev[x+2][y-2]=1;
		ivPrev[x][y-2]=1;
		// end of glider
		ivPrev[x-10][y-8]=1;
		ivPrev[x-10][y-9]=1;
		ivPrev[x-11][y-8]=1;
		ivPrev[x-11][y-9]=1;

		ivPrev[x-6][y-6]=1;
		ivPrev[x-6][y-7]=1;
		ivPrev[x-1][y-5]=1;
		ivPrev[x-1][y-6]=1;
		ivPrev[x-1][y-10]=1;
		ivPrev[x-1][y-11]=1;

		ivPrev[x][y-6]=1;
		ivPrev[x][y-7]=1;
		ivPrev[x][y-8]=1;
		ivPrev[x][y-9]=1;
		ivPrev[x][y-10]=1;
		ivPrev[x+1][y-6]=1;
		ivPrev[x+1][y-7]=1;
		ivPrev[x+1][y-9]=1;
		ivPrev[x+1][y-10]=1;
		ivPrev[x+2][y-6]=1;
		ivPrev[x+2][y-7]=1;
		ivPrev[x+2][y-9]=1;
		ivPrev[x+2][y-10]=1;
		ivPrev[x+3][y-7]=1;
		ivPrev[x+3][y-8]=1;
		ivPrev[x+3][y-9]=1;
		ivPrev[x+8][y-5]=1;
		ivPrev[x+8][y-6]=1;
		ivPrev[x+8][y-7]=1;
		ivPrev[x+9][y-5]=1;
		ivPrev[x+9][y-6]=1;
		ivPrev[x+9][y-7]=1;
		ivPrev[x+10][y-4]=1;
		ivPrev[x+10][y-8]=1;
		ivPrev[x+11][y-3]=1;
		ivPrev[x+11][y-9]=1;
		ivPrev[x+12][y-4]=1;
		ivPrev[x+12][y-8]=1;
		ivPrev[x+13][y-5]=1;
		ivPrev[x+13][y-6]=1;
		ivPrev[x+13][y-7]=1;

		ivPrev[x+23][y-6]=1;
		ivPrev[x+23][y-7]=1;
		ivPrev[x+24][y-6]=1;
		ivPrev[x+24][y-7]=1;
	}

	private void loadGosperGliderGunNW(int x, int y)
	{
		// origin is at 0,0 lower right
		ivPrev[x][y+1]=1;
		ivPrev[x+1][y]=1;
		ivPrev[x+1][y+1]=1;
		ivPrev[x+2][y+2]=1;
		ivPrev[x][y+2]=1;
		// end of glider
		ivPrev[x-10][y+8]=1;
		ivPrev[x-10][y+9]=1;
		ivPrev[x-11][y+8]=1;
		ivPrev[x-11][y+9]=1;

		ivPrev[x-6][y+6]=1;
		ivPrev[x-6][y+7]=1;
		ivPrev[x-1][y+5]=1;
		ivPrev[x-1][y+6]=1;
		ivPrev[x-1][y+10]=1;
		ivPrev[x-1][y+11]=1;

		ivPrev[x][y+6]=1;
		ivPrev[x][y+7]=1;
		ivPrev[x][y+8]=1;
		ivPrev[x][y+9]=1;
		ivPrev[x][y+10]=1;
		ivPrev[x+1][y+6]=1;
		ivPrev[x+1][y+7]=1;
		ivPrev[x+1][y+9]=1;
		ivPrev[x+1][y+10]=1;
		ivPrev[x+2][y+6]=1;
		ivPrev[x+2][y+7]=1;
		ivPrev[x+2][y+9]=1;
		ivPrev[x+2][y+10]=1;
		ivPrev[x+3][y+7]=1;
		ivPrev[x+3][y+8]=1;
		ivPrev[x+3][y+9]=1;
		ivPrev[x+8][y+5]=1;
		ivPrev[x+8][y+6]=1;
		ivPrev[x+8][y+7]=1;
		ivPrev[x+9][y+5]=1;
		ivPrev[x+9][y+6]=1;
		ivPrev[x+9][y+7]=1;
		ivPrev[x+10][y+4]=1;
		ivPrev[x+10][y+8]=1;
		ivPrev[x+11][y+3]=1;
		ivPrev[x+11][y+9]=1;
		ivPrev[x+12][y+4]=1;
		ivPrev[x+12][y+8]=1;
		ivPrev[x+13][y+5]=1;
		ivPrev[x+13][y+6]=1;
		ivPrev[x+13][y+7]=1;

		ivPrev[x+23][y+6]=1;
		ivPrev[x+23][y+7]=1;
		ivPrev[x+24][y+6]=1;
		ivPrev[x+24][y+7]=1;
	}
	

/** allow application use outside of browser, appletviewer */
public static void main(String[] args) {
	System.out.print("\nInitializing.");	// part of the startup feedback
//	bvApplet=false;
	// cl-parameters = Population MaxHeight MaxWidth GridHeight MutationMultiplier dzReplacementRatio
	// enumerate arguments passed from the console
	if(args.length > 5) {
		//for(int i=0;i<args.length;i++)
		//	if(args[i].equals("-debug")) 
				//bzDebug = true; // set global debug flag
		//	else { usage(); System.exit(0); }
		// get arguments
		//population=Integer.parseInt(args[0]);	
		izMaxHeight=Integer.parseInt(args[2]);
		izMaxWidth=Integer.parseInt(args[1]);
		izGridHeight=Integer.parseInt(args[4]);
		izGridWidth=Integer.parseInt(args[3]);
		//probMutation=Integer.parseInt(args[5])/10000;	// out of 10000
		//probCrossover=Integer.parseInt(args[6])/100;
		if(Integer.parseInt(args[7])>0)
			bvRandomize=true;
		else
			bvRandomize=false;

/*
<param name="maxWidth" value=500>
<param name="maxHeight" value=500>
<param name="cellSize" value=5>
<param name="randomize" value=0>
<param name="randomizeReset" value=1000>
<param name="presets" value=4>
<param name="preset1" value="gliderGunSE">
<param name="preset1x" value=30>
<param name="preset1y" value=30>
<param name="preset2" value="gliderGunNE">
<param name="preset2x" value=35>
<param name="preset2y" value=38>
<param name="preset3" value="gliderGunSE">
<param name="preset3x" value=41>
<param name="preset3y" value=14>
<param name="preset4" value="gliderGunSW">
<param name="preset4x" value=75>
<param name="preset4y" value=31>
<param name="transSequence09" value="0,0,1,2,0,0,0,0,0,0">
*/
	} else {
		// set defaults
		//bzDebug = false;
		usage(); System.exit(0);
	}	

	Frame aFrame = new Frame("2-Cellular Automata Simulator (c) F.Michael O'Brien V1.11.04");
	LifeAnimApplet anApplet=new LifeAnimApplet();
	aFrame.add("Center",anApplet);
	aFrame.setSize(izMaxWidth+10,izMaxHeight+30);
	aFrame.show();	
	// must alt-tab out and back in to step past 
	// sun.awt.windows.WGraphics.drawImage(WGraphics.java:360) bug
	// when using double-buffering
	anApplet.init();
	anApplet.start();
}
/** print the expected parameters to the console */
public static void usage() {
	/** 
	-debug : turn console debugging output on, default=off
	 : select grid size
	*/
	System.out.println("\nCommand-Line Options are...\njava LifeAnimApplet " +
		"pixelWidth pixelHeight cellSize [0/1 periodic Randomize] randomizePeriod" + 
		"  [-debug]\n");
}

	public String getAppletInfo() {return "Written by F.Michael O'Brien (michael_obrien@mitel.com)";}
}

                                        

