/* 
 * Copyright (c) 1999-2002, Xiaoping Jia.  
 * All Rights Reserved. 
 */

import java.awt.*;
import java.util.Calendar;

/**
 *  This is an applet that displays the time in the following format:
 *        HH:MM:SS
 */
public class DigitalClock1c
  extends java.applet.Applet implements Runnable {

  protected Thread clockThread = null;
  protected Font font = new Font("Monospaced", Font.BOLD, 18);
  protected Color color = Color.green;

  static int appletId = 0;
  static int[] counts = new int[10];
  static int timesStopCalled = 0;
  
  int id = 0;
  int time = 0;  
  int timesStartCalled = 0;
  int timesStartCalledWithNullThread = 0;

  static int timesDestroyCalled = 0;
  int timesRunLoopExecuted = 0;

  public DigitalClock1c () {
    id = appletId++; 
  }

  public void start() {
    timesStartCalled++;   
    if (clockThread == null) {
      timesStartCalledWithNullThread++;
      clockThread = new Thread(this);
      clockThread.start();
      //time = 0;
    }
   }

  public void stop() {
      timesStopCalled++;
      //clockThread = null;
  }

  public void Destroy() {
     timesDestroyCalled++;
  }

  public void run() {
    //while (Thread.currentThread()  ==  clockThread) {
    while (true) {
      timesRunLoopExecuted++;	
      time++; 
      counts[id] = time;

      repaint();
      try {
        clockThread.sleep(1000); 
	//Thread.currentThread().sleep(1000);
      } catch (InterruptedException e) {}
    }
  }

  public void paint(Graphics g) {
    Calendar calendar = Calendar.getInstance();
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);
    int second = calendar.get(Calendar.SECOND);
    g.setFont(font);
    g.setColor(color);
    g.drawString(Integer.toString(time), 10, 60);
    g.drawString(threadInfo(Thread.currentThread()), 10, 100);
    g.drawString("times Start called " + Integer.toString(timesStartCalled), 10, 120);
    g.drawString("times Start called with Null thread " + Integer.toString(timesStartCalledWithNullThread), 10, 140);
    g.drawString("times Stop/Destroy called " + Integer.toString(timesStopCalled) + " " + Integer.toString(timesDestroyCalled), 10, 160);
    g.drawString("number of Run loop iterations " + Integer.toString(timesRunLoopExecuted), 10, 180);
    g.drawString("applet id: " + Integer.toString(id), 10, 200);
    g.drawString("counts: " + Integer.toString(counts[0]) + " " + Integer.toString(counts[1]) + " " + Integer.toString(counts[2]),
		 10, 220);
  }

    String threadInfo(Thread t) {
        return "thread=" + t.getName() + ", "
               + "thread group=" + t.getThreadGroup().getName();
    }


}
