/*
 * DigitalClock.java
 * copyright (c) since 2004 "Kimmy" all right reserved.
 */

/*
<applet code="DigitalClock.class" width=400 height=300>
</applet>
*/

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

public class DigitalClock extends Applet implements Runnable{

  Font font;
  Date date;
  Thread clock;

  Dimension d;
  Graphics bg;
  Image bi;

  // 初期化
  public void init(){
    setBackground(Color.white);
    font = new Font("TimesRoman", Font.BOLD, 24);
    date = new Date();
    d = getSize();
    bi = createImage(d.width, d.height);
    bg = bi.getGraphics();
  }

  // 開始
  public void start(){
    if(clock==null){
      clock = new Thread(this);
      clock.start();
    }
  }

  // 停止
  public void stop(){
    if(clock!=null){
      //clock.stop();
      clock = null;
    }
  }

  // デジタル時計の実行
  public void run(){
    while(true){
      date = new Date();
      repaint();
      try{
        Thread.sleep(1000);
      }
      catch(InterruptedException e){
      }
    }
  }

  // 描画
  public void paint(Graphics g){
    bg.clearRect(0, 0, d.width, d.height);
    bg.setFont(font);
    bg.drawString(date.toString(), 10, 30);
    g.drawImage(bi, 0, 0, this);
  }

}
