/*
* @(#) Tokei.java v1.00 2002. 2.14.
* By Fukuzo Kuroki
*/
import java.applet.Applet;
import java.util.Calendar;
import java.lang.Math;
import java.awt.*;
import java.text.*;
/**
Java2 アップレット版、アナログ時計プログラム(Ver.1.00)
*/
public class Tokei extends Applet implements Runnable {
Thread timer; // The thread that displays clock
int xs=0,ys=0,xm=0,ym=0,xh=0,yh=0,xcenter=150, ycenter=110,hankei=100;
double xyrad = Math.PI / 180.0;
String xstr="";
char[] wday=new char[]{'日','月','火','水','木','金','土'};
Image xxbuf;
Graphics xxbuff;
Thread xt;
Dimension xxd =getSize();
Color handColor; // Color of main hands and dial
Color numberColor; // Color of second hand and numbers
public void init(){
handColor = Color.blue;
numberColor = Color.darkGray;
try {
Dimension xxd =getSize();
xxbuf =createImage(xxd.width,xxd.height);
} catch (Exception E) { }
}
public void start() {
timer = new Thread(this);
timer.start();
}
public void stop() {
timer = null;
}
//1秒毎の描写
public void run() {
Thread me = Thread.currentThread();
while (timer == me) {
try {
Thread.currentThread().sleep(1000);
}
catch (InterruptedException e) {
}
repaint();
}
}
//ウィンドゥを消去しないようにする
public void updata(Graphics xxg){
paint(xxg);
}
public void paint(Graphics xxg){
//現在の日付と時刻の取得
Calendar xcal = Calendar.getInstance();
int xxye = xcal.get(Calendar.YEAR);
int xxmo = xcal.get(Calendar.MONTH) + 1;
int xxda = xcal.get(Calendar.DATE);
int xxwe = xcal.get(Calendar.DAY_OF_WEEK) - 1;
int xii=xcal.get( Calendar.SECOND );
int xmi=xcal.get( Calendar.MINUTE );
int xhi=xcal.get( Calendar.HOUR_OF_DAY );
int xi=xii*6-90,xmm=xmi*6-90+xii/10,xhh=xhi*30-90+xmi*6/12;
//バッファのグラフィックコンテキストの取得
if(xxbuff ==null)
xxbuff=xxbuf.getGraphics();
//バッファの描写
Dimension xd =getSize();
xxbuff.setColor(Color.white);
xxbuff.fillRect(0,0,xd.width,xd.height);
//アナログ時計の絵の表示
//Image xim=getImage(getDocumentBase(),"mojiban.gif");
//xxbuff.drawImage(xim,50,0,this);
//アナログ時計の表示の為のX,Y座標の取得
xs=xcenter+(int)(0.7*hankei*Math.cos(xi*xyrad));//秒のX座標
ys=ycenter+(int)(0.7*hankei*Math.sin(xi*xyrad));//秒のY座標
xm=xcenter+(int)(0.6*hankei*Math.cos(xmm*xyrad));//分のX座標
ym=ycenter+(int)(0.6*hankei*Math.sin(xmm*xyrad));//分のY座標
xh=xcenter+(int)(0.4*hankei*Math.cos(xhh*xyrad));//時間のX座標
yh=ycenter+(int)(0.4*hankei*Math.sin(xhh*xyrad));//時間のY座標
//デジタル時計の表示
xstr="";
if(xhi<10) xstr+=" ";
xstr +=xhi+"時";
if(xmi<10) xstr+="0";
xstr +=xmi+"分";
if(xii<10) xstr+="0";
xstr +=xii+"秒";
xxbuff.setColor(Color.magenta);
xxbuff.drawString(xxye+"年"+xxmo+"月"+xxda+"日 "+"("+wday[xxwe]+")",100,225);
xxbuff.drawString(xstr,120,240);
//アナログ時計の表示
xxbuff.setColor(Color.blue);//長針(青)
xxbuff.drawLine(xcenter,ycenter,xh,yh);
xxbuff.setColor(Color.green);//短針(緑)
xxbuff.drawLine(xcenter,ycenter,xm,ym);
xxbuff.setColor(Color.red);//秒針(赤)
xxbuff.drawLine(xcenter,ycenter,xs,ys);
// 時計文字盤の描画開始
xxbuff.setColor(handColor);
xxbuff.drawArc(xcenter-hankei,ycenter-hankei,2*hankei,2*hankei,0,360);// 円の描画
xxbuff.drawArc(xcenter-hankei-1,ycenter-hankei-1,2*hankei,2*hankei,0,360);// 円の描画
xxbuff.setColor(numberColor);
for(int i=1;i<13;i++){//文字(時刻数字)の描画
xxbuff.drawString(Integer.toString(i),xcenter
+(int)(0.8*hankei*Math.cos((i*30-90)*xyrad)),ycenter
+(int)(0.8*hankei*Math.sin((i*30-90)*xyrad)));
}
// 時計文字盤の描画終了
//ウィンドゥの更新
xxg.drawImage(xxbuf,0,0,this);
}
}