// Import files...
import java.awt.*;
import java.applet.Applet;
import java.net.URL;
import java.net.MalformedURLException;

// Main Class
public class Decide extends Applet implements Runnable {
    int frameNumber = -1;			//Holds current Frame no
    int delay;						//Thread Delay
    int current, ballx, bally = 0;
    int goes, rounds, position = 0;
   
    Thread animatorThread;			//The thread
    boolean frozen = false;
    boolean pressedButt = false;
    boolean running = false;		//Is ball rolling?

	// Graphics stuff
    Dimension offDimension;
    Image offImage, iback, iball, idecide1;
    Graphics offGraphics;

	// 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
        iback = getImage(getCodeBase(), "data/board.jpg");
        iball = getImage(getCodeBase(), "data/ball.gif");
        idecide1 = getImage(getCodeBase(), "data/decide1.gif");

		ballx = 99;
		bally = 39;
		position = 0;
    }

	// Start the thread
    public void start() {
        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();
        }
    }

	// Stop the thread
    public void stop() {
        //Stop the animating thread.
        animatorThread = null;
        //Get rid of the objects necessary for double buffering.
        offGraphics = null;
        offImage = 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) {
	    //Display it.
            repaint();

		// Set the delay
	    if (running) {
		    if (rounds < 6)
  		        delay = 50;
		    else if (rounds < 8)
		        delay = 100;
		    else delay = 150;
	    }
	    else
		    delay = 500;

            //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 (!running) {
	  	    running = true;
          		current += (int)(Math.random() * 8 + 1);
		    if (current > 8)
				current -= 8;
		    frameNumber = 0;
		    rounds = 0;
		    repaint();
	    }
	    return(true);
    }//end mouseDown


/*******************************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(200, 200);
                offGraphics = offImage.getGraphics();
	    }
        // Erase the previous image.
        offGraphics.setColor(Color.black);
	    offGraphics.fillRect(0, 0, 200, 200);
		// Draw the images
 	    offGraphics.drawImage(iback, 0, 0, this);
 	    offGraphics.drawImage(idecide1, 0, 0, this);
	
		if (running) {
	   	    position++;
	        if (position == 9) {
		    position = 1;
	    }
		
		// Find the position and set ball coords
		switch (position){
			case 1 : ballx = 120;
		    		 bally = 43;
					 break;
			case 2 : ballx = 155;
		    		 bally = 75;
					 break;
			case 3 : ballx = 155;
		    		 bally = 125;
					 break;
			case 4 : ballx = 120;
		    		 bally = 160;
					 break;
			case 5 : ballx = 77;
		    		 bally = 160;
					 break;
			case 6 : ballx = 42;
		    		 bally = 125;
					 break;
			case 7 : ballx = 42;
		    		 bally = 75;
					 break;
			case 8 : ballx = 77;
		    		 bally = 43;
					 rounds++;
					 break;
	    }
		
		// Stop the ball spinning fif at right pos
	    if ((rounds == 8) && (position == current))
		    running = false;
	}

 	offGraphics.drawImage(iball, ballx-12, bally-11, this);
	
	//Paint the image onto the screen.
	g.drawImage(offImage, 0, 0, this);        
    }
}
