//
/* munch.java    --- rrs@isi.edu ---   05-30-96   */
/* Munching Squares is a neat display hack!!! this applet should provide about
12 hours  of different displays before repeating. This version is for bitmap
displays and Xor's the pixels for a multi-pas display. Originally this was five
machine language instructions on the Dec PDP-1	*/
// created from munch8.java ... this is for color displays ... rrs@isi.edu ... 05-25-96

/*              HTML Call



*/

import java.awt.*;
import java.applet.*;

public class colorMunch8 extends Applet  implements Runnable
{
	int wx, wy;			// window width and heigth
	int x, y, t;
	int lim = 255;
	Thread munchThread;
	int delay = 10;
	static boolean keepRunning;
	Graphics myG;
	Color colors[] = { Color.white, Color.red,Color.orange,Color.yellow,
			       Color.green, Color.cyan, Color.blue, Color.magenta,
			       Color.gray, Color.black, Color.darkGray, Color.lightGray };

	
	 public void init( )
		{
			wx = 256;
			wy = 256;
			myG = this.getGraphics( );
			Dimension d = size();
			if (( d.width != wx ) || ( d.height != wy))  { resize( wx, wy); }
			setBackground( Color.black);
			setForeground( Color.white );
			keepRunning = true;
			String spdstr = getParameter( "delay" );
			String swStr;
			if( spdstr == null )
			  { delay = 10;	}
			else
			  { delay = Integer.valueOf( spdstr ).intValue( ); }			
	   	}

	public void start( ) 
		{
			keepRunning = true;
			munchThread = new Thread(this);
			myG.setColor( Color.white );
			myG.fillRect( 0, 0,   wx-1, wy-1);
			try {Thread.currentThread().sleep(2*delay);} 
			      catch (InterruptedException e){}
			munchThread.start();
		}


	public void stop() 
		{
			keepRunning = false;
			munchThread.stop();
		}



	public void run (  )
		{
			int sw = 1, sdel = 300;
			int x, y, lim = 255;
			boolean fullcycle = true;
			t=0;
			try {Thread.currentThread().sleep(delay);} 
			      catch (InterruptedException e){}
			while( keepRunning )
			   {
				Integer st = new Integer(sw );
				showStatus(  st.toString() );
				update( myG );
				if( t == 0 ) 
					{
						fullcycle = ! fullcycle;
						if ( fullcycle ) { sw++; sw &= 255; }
					}
				t = ( t + sw ) & 255;
				try {Thread.currentThread().sleep(delay);} 
				   catch (InterruptedException e){}
			  }
		}
	

	public void update( Graphics g )
		{
			paint( g );
		}

	public void paint ( Graphics g )
		{
			int lim = 255;
			Integer ti = new Integer( t );
			//showStatus( "         " + ti.toString() );
			g.setXORMode( colors[ ( t /12) % 12 ] );
			for( int x = 0; x <= lim; x++)
	 		   {							
				g.drawLine( x, x^t,  x, x^t );
			   }
			g.setPaintMode();
		}


}
//