/*
 * Taken from "The Java Tutorial" online material
 */

import java.awt.Graphics;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.text.DateFormat;

// Note that in this example, we are extending the Applet class instead
// of the Thread class.  We use the interface Runnable to implement our
// Thread.  A class that implements the Runnable interface must implement
// the instance method run.  For an applet we have to subclass the
// Applet class so we cannot also subclass Thread since Java does not
// have multiple inheritance.  Interfaces give us a much simpler
// facility that provides some of the flexibility of multiple inheritance.

public class Clock extends java.applet.Applet implements Runnable {

    Thread clockThread = null;

    public void start() {
        if (clockThread == null) {
            // Create a new thread with a runnable object and a title.
            clockThread = new Thread(this, "Clock");
            clockThread.start();
        }
    }
    public void run() {
        // loop terminates when clockThread is set to null in stop()
        while (Thread.currentThread() == clockThread) {
            repaint();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e){
            }
        }
    }

    // This function "paints" the clock onto the applet.
    // This method uses some of the internationalization features of
    // Java from the java.util and java.text packages, namely
    // the classes Calendar, Date, Locale, and TimeZone from the
    // util package and DateFormat from the text package.
    // Presumably we should get our clock to display the local time,
    // but this program fails to do that for reasons that I do not
    // understand.  However, in the method TimeZone.getDefault()
    // there is a comment " BRIAN FIXME".  Is this related :-).

    public void paint(Graphics g) {
       System.out.println("TimeZone: default = " + 
                          (TimeZone.getDefault()).getID());
       Calendar cal = Calendar.getInstance(TimeZone.getDefault());
       Date date = cal.getTime();
       System.out.println("Clock: Default Locale = " + Locale.getDefault());
       DateFormat dateFormatter = DateFormat.getTimeInstance(DateFormat.DEFAULT,
                                                 Locale.getDefault());
       g.drawString(dateFormatter.format(date), 5, 10);
    }

    // This method is called when the applet is hidden.  If the
    // applet is then redisplayed, start() is called again and
    // creates a new thread.
    public void stop() {
        clockThread = null;
    }
}

