/**
 * Description: A math game for Mr. Borland to use in his class
 * 
 * 
 * Author:
 * Date:
 */

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.text.*;

//notice that I say implements actionlistener, this is to say that I will be listening for actions
public class MathGame extends JFrame implements ActionListener, Runnable  
{ 
    JTextField time=new JTextField(5);
    long startTime = System.currentTimeMillis();

    public static void main(String[] args) {
        MathGame m = new MathGame();
        m.init();
        m.start();
        m.setSize(520,520);
        m.setVisible(true);

    }

    public void init()
    {
        Container screen = getContentPane();
        screen.setBackground(Color.white);
        screen.setLayout (new FlowLayout() );
        screen.add(time);
       
    }

   
    public void paint (Graphics g)
    {
        super.paint(g);
    }

    //now we have to have a method actionPerformed that will be called if an action happens
    //we then see which component had the action on it
    public void actionPerformed(ActionEvent thisEvent)
    {
        Object source = thisEvent.getSource();

    }

    public void update()
    {
        //Ive given you some code that rounds the seconds to hundredths place and puts it in the time field
        double elapsedTimeSeconds = (System.currentTimeMillis()-startTime)/1000.0; 
       	//The class DecimalFormat below is useful for rounding to number of places.        
        String timeString= new DecimalFormat("#######.##").format(elapsedTimeSeconds);
        time.setText(""+timeString);
    }

    /*********************************************************************************************/
    /* BELOW IS FOR ANIMATION.  THE ONLY THING THAT YOU NEED TO CHANGE IS DELAY */

    int frame;
    int delay=50;   // this is the time of the delay in milliseconds.
    Thread animator;

    /**
     * This method is called when the applet becomes visible on
     * the screen. Create a thread and start it.
     */
    public void start()
    {
        animator = new Thread(this);
        animator.start();
    }

    /**
     * This method is called by the thread that was created in
     * the start method. It does the main animation.
     */
    public void run()
    {
        // Remember the starting time
        long tm = System.currentTimeMillis();
        while (Thread.currentThread() == animator)
        {
            // Display the next frame of animation.
            update();
            try
            {
                tm += delay;
                Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
            }
            catch (InterruptedException e)
            {
                break;
            }
            // Advance the frame
            frame++;
        }
    }

    /**
     * This method is called when the applet is no longer
     * visible. Set the animator variable to null so that the
     * thread will exit before displaying the next frame.
     */
    public void stop()
    {
        animator = null;
    }
}       