// Import files...
import java.awt.*;
import java.applet.Applet;
import java.net.URL;
import java.net.MalformedURLException;

// Main Class
public class Main extends Applet implements Runnable {
    int frameNumber = -1;			//Holds current Frame no
    int changing, frameTemp = 0;
    int delay;						//Thread Delay
    int currOver = 0;				//What cursor is over
    int oldOver = 12;				//What cursor was over
    Thread animatorThread;			//The thread
    boolean frozen = false;
    boolean pressedButt = false;

	// Graphics stuff
    Dimension offDimension;
    Image offImage, offImage2, ildoor, irdoor, 
	iclock;
    Image[] imageArray = new Image[13];
    Graphics offGraphics, offGraph2;

	// Initialization
    public void init() {
        String str;
        int fps = 10;

        //How many milliseconds between frames?
        str = getParameter("fps");
        try {
            if (str != null) {
                fps = Integer.parseInt(str);
            }
        } catch (Exception e) {}
		delay=150;

		// Load graphic files
        iclock = getImage(getCodeBase(), "data/clock.jpg");
	    ildoor = getImage(getCodeBase(), "data/ldoor.jpg");
		irdoor = getImage(getCodeBase(), "data/rdoor.jpg");
		for (int loop = 1; loop <= 12; loop++)
			imageArray[loop] = getImage(getCodeBase(), "data/time" + loop + ".jpg");
    }

	// simply call startThread
    public void start() {
		startThread();
    }

	// Start the thread
    public void startThread() {
        if (frozen) {
            //Do nothing.  The user has requested that we
            //stop changing the image.
        } else {
            //Start animating!
            if (animatorThread == null) {
                animatorThread = new Thread(this);
            }
            animatorThread.start();
        }
    }

	// stops the Thread
    public void stopThread() {
        //Stop the animating thread.
        animatorThread = null;
    }

	// Stop the thread
    public void stop() {
		stopThread();
        //Get rid of the objects necessary for double buffering.
        offGraphics = null;
		offGraph2 = null;
        offImage = null;
		offImage2 = null;
    }

	// Runnig the thread
    public void run() {

        //Just to be nice, lower this thread's priority
        //so it can't interfere with other processing going on.
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
 
        //Remember the starting time.
        long startTime = System.currentTimeMillis();

        //This is the animation loop.
        while (Thread.currentThread() == animatorThread) {
        	//Advance the animation frame.
	  	    if ((frameTemp == 0) && (currOver != 0)) {
				frameTemp = 3;
				changing = 0;
		    }
		    if ((frameTemp == 1) && (currOver == 0))
				frameTemp = 2;
		    if (frameTemp == 3) {
		        frameNumber++;
				changing = 0;
		    	if (frameNumber==6)
		 			frameTemp = 1;
		    }
		    if (frameTemp == 2) {
		        frameNumber--;
		    	if (frameNumber==0)
					if (currOver != 0)
		 				frameTemp = 3;
					else
						frameTemp = 0;
		    }
		    if (frameNumber < 0) {
				frameNumber = 0; 
				frameTemp = 0;
		    }
		    if (frameNumber > 6) {
				frameNumber = 6; 
				frameTemp = 1;
		    }

            //Display it.
            repaint();

			// Set the delay
		    if ((frameTemp == 0) || (frameTemp == 1)) 
				delay = 500;
		    else
				delay = 50;

            //Delay depending on how far we are behind.
            try {
                startTime += delay;
                Thread.sleep(Math.max(0, 
                                      startTime-System.currentTimeMillis()));
            } catch (InterruptedException e) {
                break;
            }
        }
    }

 /*******************************EVENTS****************************************/  

	// When mousebutton is pressed
    public boolean mouseDown(Event e, int x, int y){
		pressedButt = true;
		//If mouse is over something then load the page
		if (currOver != 0) {
			try{
			      getAppletContext().showDocument(new URL("http://www.cms.dmu.ac.uk/~c97pl/page" + currOver + ".html"));
			}catch(MalformedURLException mal){showStatus("Error loading HTML");}
			repaint();
		}
		return(true);
    }//end mouseDown

	// When mouse is moved
    public boolean mouseMove(Event e, int x, int y){
		int temp = currOver;
		//Check what mouse is over...
		if (x < 68) {
			if (y < 35) 
				currOver = 11;
			else if (y < 50)
				currOver = 10;
			else if (y < 65)
				currOver = 9;
			else if (y < 79)
				currOver = 8;
			else currOver = 7;
		} else if (x > 86) {
			if (y < 35) 
				currOver = 1;
			else if (y < 50)
				currOver = 2;
			else if (y < 65)
				currOver = 3;
			else if (y < 79)
				currOver = 4;
			else currOver = 5;
		} 
		else {
			if (y <= 58) 
				currOver = 12;
			else
				currOver = 6;
		}
		//If mouse outside area...
		if ((x < 25) || (x > 125) || (y < 10) || (y > 110)) {
			currOver = 0;
		}
		//If option changed & doors closed
		if ((currOver != temp) && (changing == 0)) {
    	    oldOver = temp;
		}
		//If mouse has moved over different option & doors open
		if (currOver != temp) {
		    if (frameTemp == 1)
				frameTemp = 2;
		    if (frameTemp == 3)
				frameTemp = 2;
		    changing = 1;
		    repaint();
		}
		return(true);
    }//end mouseMove

    public boolean mouseExit(Event e, int x, int y){
	currOver = 0;
	showStatus("");
	repaint();
	return(true);
    }//end mouseExit

/*******************************END EVENTS*************************************/

	// Paint the graphics
    public void paint(Graphics g) {
		frameNumber = 0;
        update(g);
    }

	//Update the graphics
    public void update(Graphics g) {
        Dimension d = size();

		//Create the offscreen graphics context, if no good one exists.
        if ( (offGraphics == null)
              || (d.width != offDimension.width)
              || (d.height != offDimension.height) ) {
                offDimension = d;
                offImage = createImage(150, 200);
                offGraphics = offImage.getGraphics();
                offImage2 = createImage(120, 50);
                offGraph2 = offImage2.getGraphics();
		}
    	//Erase the previous image.
    	offGraphics.setColor(Color.black);
	    offGraphics.fillRect(0, 0, 150, 200);
		offGraph2.setColor(Color.black);
		offGraph2.fillRect(0, 0, 120, 50);

		//Draw images
		offGraphics.setColor(new Color(85, 68, 58));
 	    offGraphics.drawImage(iclock, 0, 0, this);
	    offGraphics.drawRect(14, 134, 121, 51); 

	    //draw hands
        offGraphics.setColor(new Color(252, 255, 253));
	    offGraphics.drawLine(77, 52, 77, 33); 
        offGraphics.setColor(new Color(168, 178, 181));
	    offGraphics.drawLine(76, 51, 76, 35); 
	    offGraphics.drawLine(78, 51, 78, 35); 
		switch (currOver) {
		case 1  : 
			     offGraphics.drawLine(80, 53, 96, 31); 
	             offGraphics.setColor(new Color(168, 178, 181));
			     offGraphics.drawLine(80, 52, 94, 33); 
			     offGraphics.drawLine(81, 53, 95, 33); 
			     offGraphics.drawLine(95, 34, 93, 37); 
				 break;
		case 2  : 
			     offGraphics.drawLine(83, 56, 106, 44); 
	             offGraphics.setColor(new Color(168, 178, 181));
 			     offGraphics.drawLine(83, 55, 103, 45); 
			     offGraphics.drawLine(83, 57, 104, 46); 
				 break;
		case 3  : 
		    	 offGraphics.drawLine(83, 60, 111, 60); 
            	 offGraphics.setColor(new Color(168, 178, 181));
		    	 offGraphics.drawLine(83, 59, 109, 59); 
		    	 offGraphics.drawLine(83, 61, 109, 61); 
				 break;
		case 4  : 
		    	 offGraphics.drawLine(81, 62, 106, 78); 
            	 offGraphics.setColor(new Color(168, 178, 181));
		    	 offGraphics.drawLine(82, 62, 104, 76); 
		    	 offGraphics.drawLine(81, 63, 103, 77); 
				 break;
		case 5  : 
		    	 offGraphics.drawLine(79, 64, 94, 92); 
            	 offGraphics.setColor(new Color(168, 178, 181));
		     	 offGraphics.drawLine(81, 65, 93, 89); 
		    	 offGraphics.drawLine(79, 65, 92, 90); 
				 break;
		case 6  : 
		    	 offGraphics.drawLine(77, 64, 77, 95); 
            	 offGraphics.setColor(new Color(168, 178, 181));
		    	 offGraphics.drawLine(76, 65, 76, 93); 
		    	 offGraphics.drawLine(78, 65, 78, 93); 
				 break;
		case 7  : 
		    	 offGraphics.drawLine(75, 64, 60, 92); 
            	 offGraphics.setColor(new Color(168, 178, 181));
		    	 offGraphics.drawLine(73, 65, 61, 89); 
		    	 offGraphics.drawLine(75, 65, 62, 90); 
				 break;
		case 8  : 
		    	 offGraphics.drawLine(73, 62, 44, 74); 
            	 offGraphics.setColor(new Color(168, 178, 181));
		    	 offGraphics.drawLine(71, 62, 47, 72); 
		    	 offGraphics.drawLine(72, 63, 48, 73); 
				 break;
		case 9  : 
		     	 offGraphics.drawLine(71, 60, 43, 60); 
            	 offGraphics.setColor(new Color(168, 178, 181));
		    	 offGraphics.drawLine(71, 59, 45, 59); 
		    	 offGraphics.drawLine(71, 61, 45, 61); 
				 break;
		case 10 : 
		    	 offGraphics.drawLine(71, 56, 48, 44); 
            	 offGraphics.setColor(new Color(168, 178, 181));
		    	 offGraphics.drawLine(71, 55, 51, 45); 
		    	 offGraphics.drawLine(71, 57, 52, 46); 
				 break;
		case 11 : 
		    	 offGraphics.drawLine(74, 53, 61, 29); 
             	 offGraphics.setColor(new Color(168, 178, 181));
		    	 offGraphics.drawLine(74, 52, 63, 31); 
		    	 offGraphics.drawLine(73, 53, 63, 33); 
				 break;
		case 12 :
		case 0  : 
	     	 	 offGraphics.drawLine(77, 52, 77, 25); 
            	 offGraphics.setColor(new Color(168, 178, 181));
	    		 offGraphics.drawLine(76, 51, 76, 27); 
	    		 offGraphics.drawLine(78, 51, 78, 27); 
				 break;
		} //Switch currOver
	
        //If doors are closed
		if (frameTemp==0) {
		    offGraph2.drawImage(ildoor, 0, 0, this);
		    offGraph2.drawImage(irdoor, 60, 0, this);
		}
		//If doors are open
		if (frameTemp==1) {
		    offGraph2.drawImage(imageArray[currOver], 0, 0, this);
		}
		//If doors are closing
		if (frameTemp==2) {
		    offGraph2.drawImage(imageArray[oldOver], 0, 0, this);
		    offGraph2.drawImage(ildoor, 0 - (frameNumber * 10), 0, this);
		    offGraph2.drawImage(irdoor, 60 + (frameNumber * 10), 0, this);
		}
		//If doors are opening
		if (frameTemp==3) {
		    offGraph2.drawImage(imageArray[currOver], 0, 0, this);
		    offGraph2.drawImage(ildoor, 0 - (frameNumber * 10), 0, this);
		    offGraph2.drawImage(irdoor, 60 + (frameNumber * 10), 0, this);
		}
		
		//Paint the image onto the screen.
		g.drawImage(offImage, 0, 0, this);        
		g.drawImage(offImage2, 15, 135, this);  
    } //End update
} //End class
