// Clock.java: Show a running clock on the panel
import java.util.*;

public class Clock extends StillClock implements Runnable
{
  // Declare a thread for running the clock
  private Thread timer = null;

  // Determine if the thread is suspended
  private boolean suspended = false;

  // Default constructor
  public Clock()
  {
    super();

    // Create the thread
    timer = new Thread(this);

    // Start the thread
    timer.start();
  }

  // Construct a clock with specified locale and time zone
  public Clock(Locale locale, TimeZone tz)
  {
    super(locale, tz);

    // Create the thread
    timer = new Thread(this);

    // Sstart the thread
    timer.start();
  }

  // Implement the run() method to dictate what the thread will do
  public void run()
  {
    while (true)
    {
      repaint();
      try
      {
        timer.sleep(1000);
        synchronized (this)
        {
          while (suspended)
            wait();
        }
      }
      catch (InterruptedException ex)
      {
      }
    }
  }

  // Resume the clock
  public synchronized void resume()
  {
    if (suspended)
    {
      suspended = false;
      notify();
    }
  }

  // Suspend the clock
  public synchronized void suspend()
  {
    suspended = true;
  }
}
