//ClockCanvas.java
/* A Java class for
 * LossyWide.java
 * Electromagnetic Transmission Line Applet
 * Applet without Smith Chart - Prepared by Umberto Ravaioli 
 * for 6th edition of Fundamentals of Applied Electromagnetics Book
 * May 2009 - All Rights Reserved
 */ 
import java.awt.*;


public class ClockCanvas extends Canvas implements Runnable{
//private static final Color bgcolor = new Color(180,147,112);
private static final Color bgcolor = new Color(200,200,180);
private static final Color maroon = new Color(185,50,50);

    private int CL_START, CL_FINISH, CL_DELTA, CL_ROTOR;
    private int current_angle;
    private int clock_width, clock_height;
    private int clock_centerx, clock_centery;
    private Image im;
    private Graphics buf;
    private boolean CLEAR_FLAG;
    private Thread tron;
    private boolean isTronRunning;
    private int SleepTime;
    public ClockCanvas(){
	super();
	setBackground(bgcolor);
	CL_START = 0;
	CL_FINISH = 360;
	CL_DELTA = 10;
	current_angle = 0;
	CL_ROTOR = 90;
	CLEAR_FLAG = false;
	SleepTime = 200;
	isTronRunning = false;
    }
    
    public void paint(Graphics g){
	clock_centerx=getSize().width/2;
	clock_centery=getSize().height/2;
	clock_width=2*clock_centerx*84/100;
	clock_height=clock_width;
	if(im == null){
	    im = createImage(getSize().width,getSize().height);
	    buf = im.getGraphics();
	    drawClock(buf);
	}
	drawClock(buf);
    	g.drawImage(im,0,0,null);
    }
    
    private void drawClock(Graphics g){
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

	if(CLEAR_FLAG){
	    g.setColor(Color.lightGray);
	    g.fillRect(0,0,getSize().width-1,getSize().height-1);
	    g.setColor(Color.black);
	    g.drawRect(0,0,getSize().width-1,getSize().height-1);
	    //g.setColor(Color.lightGray);
	    g.setColor(Color.cyan);
	    g.fillOval(clock_centerx-clock_width/2,clock_centery-clock_height/2,clock_width,clock_height);
	    CLEAR_FLAG = false;
	}
        
        g.setColor(Color.gray);
	g.fillOval(clock_centerx-clock_width/2-1,clock_centery-clock_height/2-1,clock_width+2,clock_height+2);
        g.setColor(Color.cyan);
	g.fillOval(clock_centerx-clock_width/2,clock_centery-clock_height/2,clock_width,clock_height);
	    
	//g.setColor(bgcolor.brighter());
	g.setColor(Color.white);
	g.fillArc(clock_centerx-clock_width/2,clock_centery-clock_height/2,clock_width,clock_height,CL_START+CL_ROTOR,-current_angle);
	
	//g.setColor(maroon);
	g.setColor(Color.black);
	//g.drawOval(clock_centerx-clock_width/2,clock_centery-clock_height/2,clock_width,clock_height);
	
	//g.setColor(Color.white);
	//g.drawArc(clock_centerx-clock_width/2,clock_centery-clock_height/2,clock_width,clock_height,CL_START+CL_ROTOR,-current_angle);
	
	//draw cross 
	g.setColor(Color.gray);
	
	g.drawLine(clock_centerx-clock_width/2,clock_centery,clock_centery+clock_width/2,clock_centery);
	g.drawLine(clock_centerx,clock_centery-clock_height/2,clock_centerx,clock_centery+clock_height/2);
    }
    
    public void increment(){
	current_angle += CL_DELTA;
	if(current_angle>CL_FINISH) { current_angle -= (CL_FINISH-CL_START); CLEAR_FLAG = true; }
	repaint();
    }
    
    public void decrement(){
	current_angle -= CL_DELTA;
	if(current_angle<CL_START) { current_angle += (CL_FINISH-CL_START); }
	CLEAR_FLAG = true;
	repaint();
    }
    
    public synchronized void setStatus(int CL_START, int CL_FINISH, int CL_DELTA){
	this.CL_START=CL_START;
	this.CL_FINISH = CL_FINISH;
	this.CL_DELTA = CL_DELTA;
    }
    
    public synchronized void reset(){
	current_angle = 0;
	CLEAR_FLAG = true;
	repaint();
    }
    
    public void start(){
	if(tron == null){
	    tron = new Thread(this);
	    tron.start();
	}
    }
	
	
    public void run(){
	while(true){
	    try{
		   Thread.sleep(SleepTime);
	    }
	catch(InterruptedException e){e.printStackTrace();}   
	    if(isTronRunning){
		increment();
		repaint();
	    }
	}
    }
	
   
    public synchronized void setSleepTime(int SleepTime){
	this.SleepTime = SleepTime;
    }
    
    public void update(Graphics g){
	paint(g);
    }
    
}