Clock2.java

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class Clock2 implements ActionListener
{
   NTJFrame frame = new NTJFrame("Clock");
   Drawing drawing = new Drawing();
   TimeKeeper time = new TimeKeeper();
   int hours=0, minutes=0, seconds=0;
   boolean stopped = false;
   JButton toggle = new JButton("Stop Timer");
   
   public Clock2()
   {
      frame.getContentPane().add(toggle, "North");
      toggle.addActionListener(this);
      frame.getContentPane().add(drawing,"Center");
      frame.setVisible(true);
      time.start();
   }
   
   public static void main (String[] args)
   {
      new Clock2();
   }
   
   public void actionPerformed(ActionEvent e)
   {
      if (stopped)
      {
         stopped = false;
         toggle.setText("Stop Timer");
      }
      else
      {
         stopped = true;
         toggle.setText("Start Timer");
      }
   }

   class Drawing extends JComponent
   {
      public Drawing()
      {
         repaint();
      }
      
      public void paint(Graphics g)
      {
         g.drawString(hours + ":" + minutes + ":" + seconds, 100,100);
      }
   }
   
   class TimeKeeper extends Thread
   {
      public void run()
      {
         try
         {
            while (true)
            {
               sleep(1000);
               if (!stopped)
               {
                  seconds++;
                  if (seconds > 59)
                  {
                     seconds = 0;
                     minutes++;
                     if (minutes > 59)
                     {
                        minutes = 0;
                        hours++;
                     }
                  }
                  drawing.repaint();
               }
            }
         }
         catch (InterruptedException e)
         {
         }
      }
   }
}