
import java.util.*;
import java.awt.*;
import java.applet.*;
// import java.text.*;

public class Clk extends Applet implements Runnable {
  Thread timer = null;
  Date dat = null;  // System clock is read in this
  String lastdate;  // String to hold date displayed

  String city = null; // Name of the city (input from HTML page)
  int    Gmt  = 0 ;   // Time zone        (input from HTML page)

  // Initialization of the applet 
  public void init() {
    // Now read input parameters from the HTML page
    try {
      city = getParameter("cityName");
    } catch (Exception E) { }
    try {
      Gmt = Integer.parseInt(getParameter("GMT"));
    } catch (Exception E) { }
}

  // Paint is the main part of the program
  public void paint(Graphics g) {
    Date olddat = null;
    String today;

    Font F = new Font("TimesRoman", Font.PLAIN, 36);
    int s,m,h;

    // Read system date and find hour, minute and seconds
    dat = new Date();
    // Compare new time with the last displayed. If it is changed 
    // display new time
    if (olddat!=dat) {
      h = dat.getHours();
      m = dat.getMinutes();
      s = dat.getSeconds();

      // Find the current hour in the corresponding time zone
      h=h+Gmt-2;

      // Correct hour if it is out of range
      h+=24;
      h%=24;

      // Now construct date string to be displayed
      today="";
      if (h<10)
        today="0";
      today+=h+":";
      if (m<10)
        today+="0";
      today+=m+":";
      if (s<10)
        today+="0";
      today+=s;
  
      g.setFont(F);
      g.drawString(city, 30, 35);
 
   // Erase if necessary, and reprint
      g.setColor(getBackground());
      g.drawString(lastdate, 35, 80);
      g.setColor(Color.black);
      g.drawString(today, 35, 80);
      lastdate=today;
      olddat=dat;
      dat=null;
    }
  }

  // Start method of the applet
  public void start() {
    if(timer == null)
      {
        timer = new Thread(this);
        timer.start();
      }
  }

  // Stop method of the applet
  public void stop() {
    timer = null;
  }

  // Run method of the applet
  public void run() {
    while (timer != null) {
    // wait for 100 milisecond and if necessary paint the screen again 
      try {Thread.sleep(100);} catch (InterruptedException e){}
      repaint();
    }
    timer = null;
  }

  // update method of the applet
  public void update(Graphics g) {
    paint(g);
  }
}
