//
/* munchSw4.java    --- rrs@isi.edu ---   06-07-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. It has an 8 bit user setable switch register	*/
/*



*/

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

public class munchSw4 extends Applet  implements Runnable
{
	int wx, wy;			// window width and heigth
	int x, y, t;
	int lim = 255;
	Sw8Reg swreg;
	Thread munchThread;
	static boolean killMe = false;
	int delay = 10;
	static boolean keepRunning;
	Graphics myG;
	Color penColor;
	
	 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.white );
			keepRunning = true;
			String spdstr = getParameter( "delay" );
			if( spdstr == null )
			  { delay = 10; }
			else
			  { delay = Integer.valueOf( spdstr ).intValue( ); }
	   	}

	public void start( ) 
		{
			swreg = new Sw8Reg();
			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; 
			swreg.destroy();
			swreg = null;
			munchThread.stop();
		}


	public void run (  )
		{
			int sw = 0, sdel = 300;
			int x, y;
			boolean fullcycle = true;
			t=0;
			try {Thread.currentThread().sleep(sdel);} 
			   catch (InterruptedException e){}
			while( keepRunning )
			   {
				Integer st = new Integer(sw );
				showStatus( st.toString() );
				update( myG );
				if( swreg.getRst() == true)
					{
						t = 0;
						fullcycle = true;
						sw = swreg.getSwReg( );
						swreg.setRst( false );
						myG.setColor( Color.white );
						myG.fillRect( 0, 0,   wx-1, wy-1);
						try {Thread.currentThread().sleep(sdel);} 
						  catch (InterruptedException e){}										penColor = Color.green;
						//update( myG );
					}
				else
				  {
				   if( t == 0 ) 
					{
						fullcycle = ! fullcycle;
						penColor = Color.white;
						if ( fullcycle ) { sw = swreg.getSwReg( );											  penColor=Color.green;
							            }
					}
					update( myG );
				  }
				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;
			//g.setXORMode( Color.green );
			g.setColor( penColor );
			for( int x = 0; x <= lim; x++)
	 		   {							
				g.drawLine( x, x^t,  x, x^t );
			   }
			//g.setPaintMode();
		}


}
//