////////////////////////////////////////////////////////////////////
//
//    ClockTest class
//    by James Savidge
//    Last Modified 12-5-96
//
//    This applet was developed to begin expirimenting with 
//    multithreading. The applet itself is pretty useless, but
//    it does show implementations for multiple threads and
//    multiple canvases, and uses some of AWT's layout managers
//
////////////////////////////////////////////////////////////////////


import java.awt.*;
import java.util.*;
import java.applet.*;
import java.io.*;

public class ClockTest extends Applet implements Runnable
{
Thread runner;



	public void stop() {
		if (runner!= null) {
		runner.stop();
		runner = null ;
		}
	}

   public void init( )
   {
     // sets grid of 2 rows of 2 cells
     setLayout (new BorderLayout( ) );
     setLayout (new GridLayout(2,2) );

     // instantiate 4 canvas objects
     canvas4 = new Canvas( );
     add(canvas4);
     Graphics g4 = canvas4.getGraphics( );
     g4.setColor(Color.green);
     
     canvas1 = new Canvas( );
     add(canvas1);
     Graphics g1 = canvas1.getGraphics( );
     g1.setColor(Color.pink);
     canvas2 = new Canvas( );
     add( canvas2);
     Graphics g2 = canvas2.getGraphics( );
     g2.setColor(Color.blue);
     canvas3 = new Canvas( );
     add(canvas3);
     Graphics g3 = canvas3.getGraphics( );
     g3.setColor(Color.red);
   } 

   public void start( )
   {

		if (runner  == null){
		runner = new Thread(this);
		runner.setPriority(Thread.MAX_PRIORITY); // main thread
		runner.start();
		}

                // instantiate the external clock objects 
	        Clock clock1 = new Clock(canvas1, 3000 , Color.green ,Color.black  );
      	        Clock clock2 = new Clock(canvas2, 5000 , Color.green , Color.black);
    	        Clock clock3 = new Clock(canvas3, 2000 , Color.black ,Color.green );
   }

	public void run( ){
	    while (true) {
		//repaint(0 , 0 , this.size().width, this.size().height );
		draw_it( );
		try { Thread.sleep(1000) ; }
		catch( InterruptedException e ) { }
		
	    }
	}

	public void draw_it()
	{
     		Graphics g4 = canvas4.getGraphics( );
		g4.setColor(Color.black);
		g4.fillRect( 0 , 0 , this.size().width , this.size().height );
		g4.setColor(Color.green);
		g4.setFont( f );
		get_the_time();		
		int x = canvas4.size().width / 4;
		int y = canvas4.size().height  / 2 ; 
		g4.drawString( theTimeNow , x , y );

	}


	public void get_the_time()
	{
		now = new Date( );
		int a = now.getHours();
		int b = now.getMinutes();
		int c = now.getSeconds();
		if (a == 0)
			a = 12;
		if(a > 12)
			a = a -12;
		if ( a < 10)
			theTimeNow = "0" + a + ":" ;
		else
			theTimeNow = a +":";			
		
		if (b < 10)
			theTimeNow = theTimeNow + "0" + b + ":" ;
		else
			theTimeNow = theTimeNow + b + ":" ;
		
		if (c < 10)
			theTimeNow = theTimeNow + "0" + c ;
		else	
			theTimeNow = theTimeNow + c;
	}


private Clock clock1;
private Clock clock2;
private Clock clock3; 
Canvas canvas1, canvas2, canvas3, canvas4;
Graphics g1, g2, g3, g4;
private Font f = new Font("TimesRoman" , Font.BOLD , 24);
private Date now;
private String theTimeNow = new String();
} // end of class ClockTest

class Clock extends Thread {
   public Clock (Canvas canvas, int speed , Color j , Color k)
   {
      c = canvas;
      bgColor = j;
      textColor = k;
      rate = speed;
      this.start();
   } 

   public void draw_Clock( )
   {
      Graphics gc = c.getGraphics( );
      
	x = c.size().width / 4;
	y = c.size().height  / 2 ; 	
	gc.setColor(bgColor);
	gc.fillRect( 0 , 0 , c.size().width , c.size().height );

        gc.setColor( textColor);
        gc.setFont( f );
	get_the_time();
        gc.drawString( theTime , x , y);                
      
   } // end of draw

   public void run()
   {       
      while(true)
      {                   
        draw_Clock( );
         now = new Date( );
         try { sleep(rate); 
	 }
         catch(InterruptedException e) { }
      }
   } 

	public void get_the_time()
	{
		now = new Date( );
		int a = now.getHours();
		int b = now.getMinutes();
		int c = now.getSeconds();
		if (a == 0)
			a = 12;
		if(a > 12)
			a = a -12;
		if ( a < 10)
			theTime = "0" + a + ":" ;
		else
			theTime = a +":";			
		
		if (b < 10)
			theTime = theTime + "0" + b + ":" ;
		else
			theTime = theTime + b + ":" ;
		
		if (c < 10)
			theTime = theTime + "0" + c ;
		else	
			theTime = theTime + c;
	}

private int rate;
private Color bgColor;
private Color textColor;
private int x ;
private int y;
private Date now;
private int fontSize = 24;
private Font f = new Font("TimesRoman" , Font.BOLD , fontSize);
Canvas c;
private String theTime = new String();
} // end of class Clock
