import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
   Just a simple stopwatch that will displays hours, minutes, and seconds elapsed
   @author Sugi
   @version 10/13/02
*/
public class StopWatch
{
   /**
      The driver for StopWatch
   */
   public static void main(String[] args)
   {
      // The width of frame
      final int WIDTH = 550;
      // The height of frame
      final int HEIGHT = 75;
      JFrame frame = new JFrame();
      frame.setSize(WIDTH, HEIGHT);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setTitle("Sugi's Simple StopWatch");
      frame.setResizable(false);
      StopWatchPanel pane = new StopWatchPanel();
      frame.getContentPane().add(pane);
      frame.show();
   }
}

/**
   The stopwatch panel
   @author Sugi
   @version 10/13/02
*/
class StopWatchPanel extends JPanel
{
   /**
      Construct the stop watch panel
   */
   public StopWatchPanel()
   {
      setBackground(Color.blue);
      field = new JTextField(SIZE);
      field.setEditable(false);
      start = new JButton("Start");
      stop = new JButton("Stop");
      quit = new JButton("Quit");
      start.addActionListener(new
         ActionListener()
         {
            public void actionPerformed(ActionEvent e)
            {
               setText("");
               w = new EllapsedTimeUpdater(StopWatchPanel.this);
               //w.activateTheWatch();
               w.start();
               start.setEnabled(false);
               stop.setEnabled(true);
            }
         });
      stop.addActionListener(new
         ActionListener()
         {
            public void actionPerformed(ActionEvent e)
            {
               w.interrupt();
               start.setEnabled(true);
               stop.setEnabled(false);
            }
         });
      quit.addActionListener(new
         ActionListener()
         {
            public void actionPerformed(ActionEvent e)
            {
               if(w.isAlive());
                  w.interrupt();
               w = null;
               System.exit(1);
            }
         });

      addButtons();
   }

   /**
      Add the text field, start button, stop button, and quit button to panel
   */
   private void addButtons()
   {
      add(field);
      add(start);
      add(stop);
      add(quit);
   }

   /**
      Update the elapsed time
      @param curr the current elapsed time
   */
   public void setText(String curr)
   {
      field.setText(curr);
   }

   // The size of text field
   public static final int SIZE = 20;
   // The text field displaying elapsed time
   private JTextField field;
   // The elapsed time updater
   private EllapsedTimeUpdater w;
   // The start button
   private JButton start;
   // The stop button
   private JButton stop;
   // The quit button
   private JButton quit;
}

/**
   Update the elapsed time continuously
   @author Sugi
   @version 10/13/02
*/
class EllapsedTimeUpdater extends Thread
{
   /**
      Construct the updater
      @param pane the stopwatch panel
   */
   public EllapsedTimeUpdater(StopWatchPanel pane)
   {
      this.pane = pane;
   }

   /**
      Keep updating the elapsed time. Only stop if stop or quit button is pressed
   */
   public void run()
   {
      seconds = 0;
      minutes = 0;
      hours = 0;
      try
      {
         while(!interrupted())
         {
            sleep(SECOND);
            seconds++;
            if(seconds % DIV == 0 && seconds != 0)
            {
               seconds -= 60;
               minutes++;
            }
            if(minutes % DIV == 0 && minutes != 0)
            {
               minutes -= 1;
               hours++;
            }
            pane.setText(hours + " hrs : " + minutes + " mins : " + seconds + " secs ");
         }
      }
      catch(InterruptedException e)
      {
      }
   }

   // 1 s = 1000 ms
   public static final int SECOND = 1000;
   // 1 hour = 60 mins, 1 min = 60 secs
   public static final int DIV = 60;
   // seconds elapsed
   private int seconds;
   // minutes elapsed
   private int minutes;
   // hours elapsed
   private int hours;
   // The stopwatch panel
   private StopWatchPanel pane;
}
