/*******************************************************************************
*                                                                              *
*  File:        Clock.java                              Revision:  1.0         *
*                                                                              *
*  Contents:    an applet which continously shows the current time             *
*                                                                              *
*  Creation:    10.09.2000                     Last Modification:  10.09.2000  *
*                                                                              *
*  Platform:    IBM-compatible PC running Windows 98SE                         *
*                                                                              *
*  Environment: Java 1.2                                                       *
*                                                                              *
*  Author:      Andreas Rozek           Phone:  ++49 (711) 6770682             *
*               Kirschblütenweg 15      Fax:    -                              *
*             D-70569 Stuttgart         EMail:  Andreas.Rozek@T-Online.De      *
*               Germany                                                        *
*                                                                              *
*  URL:         http://Home.T-Online.De/home/Andreas.Rozek                     *
*                                                                              *
*  Copyright:   this software is published under the "GNU Public License" (see *
*               "http://www.gnu.org/copyleft/gpl.html" for details)            *
*                                                                              *
*  Comments:    as soon as more than one applet is running within the same     *
*               browser window the "paint" methods of all these applets have   *
*               to be synchronized!                                            *
*                                                                              *
*******************************************************************************/

import java.applet.*;                                    // basic applet classes
import java.awt.*;                           // basic AWT classes (& interfaces)
import java.awt.image.*;                             // image processing classes
import java.text.*;               // (language-dependent) text document handling
import java.util.*;                  // Java utility classes and data structures

public class Clock extends Placeholder implements Runnable {

/*******************************************************************************
*                                                                              *
*                          Non-Public Class Constants                          *
*                                                                              *
*******************************************************************************/

/**** information about author, version and copyright of this applet ****/

  final static String AppletInfo =
    "Clock - an applet which continously shows the current time\n" +
    "\n" +
    "Andreas Rozek\n" +
    "Kirschblütenweg 15\n" +
    "D-70569 Stuttgart\n" +
    "Germany\n" +
    "\n" +
    "Phone: ++49 (711) 6770682\n" +
    "Fax: -\n" +
    "EMail: Andreas.Rozek@T-Online.De";

/**** characteristics of font used to render the time ****/

  final static String FontName  = "Times New Roman";
  final static int    FontStyle = Font.BOLD;
  final static int    FontSize  = 14;

/*******************************************************************************
*                                                                              *
*                         Non-Public Instance Variables                        *
*                                                                              *
*******************************************************************************/

/**** working variables ****/

  Thread Timer;  // periodically wakes up this applet to update the time display
  String TimeString;                     // a string indicating the current time

  Font        TextFont;                      // font used to render a given time
  FontMetrics TextMetrics;                              // metrics of "TextFont"

/*******************************************************************************
*                                                                              *
*                           Public Instance Methods                            *
*                                                                              *
*******************************************************************************/

/*******************************************************************************
*                                                                              *
* init                 initializes an applet each time it's loaded or reloaded *
*                                                                              *
*******************************************************************************/

  public void init () {
    super.init();                                // handles any given parameters

    Timer      = null;                // the actual thread will be started later
    TimeString = "";

    TextFont = null;
  };

/*******************************************************************************
*                                                                              *
* paint                                              (re)draws applet contents *
*                                                                              *
*******************************************************************************/

  public synchronized void paint (Graphics GrafContext) {
    super.paint(GrafContext);                                 // draw background

  /**** if need be: initialize font metric settings ****/

    Graphics2D Graf = (Graphics2D) GrafContext;     // switch to Java2D graphics

    if (TextFont == null) {
      TextFont    = new Font(FontName, FontStyle, FontSize);
      TextMetrics = Graf.getFontMetrics(TextFont);
    };

  /**** calculate proper text rendering positions ****/

    int Width  = getWidth();
    int Height = getHeight();

    int X =  Width-TextMetrics.stringWidth(TimeString);
    int Y = Height-TextMetrics.getDescent()-2;

  /**** then show the actual time ****/

    Graf.setColor(Color.black);
    Graf.setFont(TextFont);
    Graf.drawString(TimeString, X,Y);
  };

/*******************************************************************************
*                                                                              *
* run                                     implements a "timer" for this applet *
*                                                                              *
*******************************************************************************/

  public void run () {
    while (Timer == Thread.currentThread()) {
      TimeString = DateFormat.getTimeInstance().format(new Date());
      this.repaint();                   // force the applet to show the new time

      try {
        long currentTime = System.currentTimeMillis();
        Timer.sleep(1100L - (currentTime % 1000L));
      } catch (InterruptedException Signal) {
        /* nop */
      };
    };
  };

/*******************************************************************************
*                                                                              *
* start                     starts an applet when its document comes into view *
*                                                                              *
*******************************************************************************/

  public void start () {                         // may be called several times!
    if (Timer == null) {                              // for safety reasons only
      Timer = new Thread(this);
      Timer.start();
    };
  };

/*******************************************************************************
*                                                                              *
* stop                      stops an applet when its document gets out of view *
*                                                                              *
*******************************************************************************/

  public void stop () {                          // may be called several times!
    Timer = null;                                      // stop the current timer
  };
};

