/*
 *  StopWatch, by Joe Palen (1998)
 */
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

import TimerDisplay;

public class StopWatch extends Applet implements ActionListener{

   //=============== Data
   Button startButton;
   Button stopButton;
   Button resetButton;
   TimerDisplay timerDisplay;

   Panel  controlPanel;

   //=============== Applet initialization
   public void init() {

      // Instantiate objects
      timerDisplay = new TimerDisplay();
      startButton = new Button("Start");
      stopButton = new Button("Lap");
      resetButton = new Button("Reset");
      controlPanel = new Panel(new GridLayout(1,4));

      // Add panel to applet frame
      add(controlPanel);

      // Add objects to control panel
      controlPanel.add(startButton);
      controlPanel.add(stopButton);
      controlPanel.add(timerDisplay);
      controlPanel.add(resetButton);

      // Connect buttons to actions
      startButton.addActionListener(this);
      stopButton.addActionListener(this);
      resetButton.addActionListener(this);

      // Set initial button states
      startButton.setEnabled(true);
      stopButton.setEnabled(false);
      resetButton.setEnabled(false);

      // Do some cosmetic stuff
      this.setBackground(Color.gray);  // applet frame color
      // controlPanel.setBackground(Color.black);

   }

   public void start() {}

   public void stop() {}

   public void destroy() {}

   // ActionListener method
   public void actionPerformed(ActionEvent event) {
     if (event.getSource() == startButton){
        // Start timer
        System.out.println("[Start] button pressed"); 
        timerDisplay.start();
        startButton.setEnabled(false);
        stopButton.setEnabled(true);
        resetButton.setEnabled(true);
     } else if (event.getSource() == stopButton) {
        // Stop timer
        System.out.println("[Stop] button pressed"); 
        timerDisplay.stop();
     } else if (event.getSource() == resetButton) {
        // Reset timer
        System.out.println("[Reset] button pressed"); 
        timerDisplay.reset();
        startButton.setEnabled(true);
        stopButton.setEnabled(false);
        resetButton.setEnabled(false);
     }
  }

   //=============== Application main
   public static void main(String args[]) {
      Frame f = new Frame("StopWatch");
      StopWatch mainStopWatch = new StopWatch();

      mainStopWatch.init();
      mainStopWatch.start();

      f.add("Center", mainStopWatch);
      f.setSize(500, 500);
      f.show();
    }
}
