/* Clock Applet
                  Coded By.KeN    Sep 24,1996
*/

import java.util.Date;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;

// imprements Runnableでマルチスレッド対応にします
public class Clock extends java.applet.Applet implements Runnable {

  Thread clockdisplay; // Thread型の新しいスレッド
  Font fonts; // Font設定用オブジェクト

  public void init() {
    fonts = new Font("TimesRoman",Font.BOLD,24); // フォントの設定
  }

  public void paint(Graphics scr) { // 表示メソッド

    Date today = new Date();        // Date型の新しいクラス
    String hms = ""; // 表示用文字列変数

    scr.setFont(fonts); // 設定したフォントを画面描画フォントに適用します
    scr.setColor(Color.blue); // 描画色を青にします
    /* 画面に年月日の表示をします
       getYear(),getMonth(),getDate()の各メソッドは、それぞれ
        年(1900年からの経過年数)、
        月(0〜11。0が1月、11が12月)、
        日(1〜31)
       を戻り値とします */
    scr.drawString((1900 + today.getYear()) + "/" +
                   (today.getMonth() + 1 ) + "/" + today.getDate(),20,25);

    if ( today.getHours() < 10 ) { // getHours()メソッドは時(0〜23)を
                                   // 戻り値とします
      hms += "0"; // 1桁の場合0を頭につけます
      }
    hms += today.getHours() + ":";
    if ( today.getMinutes() < 10 ) { // getMinutes()メソッドは分(0〜59)を
                                     // 戻り値とします
      hms += "0";
      }
    hms += today.getMinutes() + ":";
    if ( today.getSeconds() < 10 ) { // getSeconds()メソッドは秒(0〜59)を
                                     // 戻り値とします
      hms += "0";
      }
    hms += today.getSeconds();

    scr.drawString( hms, 20, 50 );   // 作成した時刻の文字列を表示します
  }

  public void start() { // アプレットが開始したら実行するメソッド
    if ( clockdisplay == null ) { // まだ設定していないなら
      clockdisplay = new Thread(this); // run()の部分はこのクラスと同じもの
                                       // を使う新しいスレッドの宣言
      clockdisplay.start(); // スレッドの開始
    }
  }

  public void run() { // スレッドの実行部分
    while ( true ) { // 無限ループ
      try { Thread.currentThread().sleep(1000); } // 1秒間待つ
        catch ( InterruptedException e ){} // 何らかの事情で中断されたら
                                         // ここに来ます
      repaint(); // 画面の更新(画面消去後、paint()メソッドが実行されます)
    }
  }

  public void stop() { // スレッドのストップ
    if ( clockdisplay != null ) {
      clockdisplay.stop();
      clockdisplay = null;
    }
  }
}

