import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.util.Date;
public class clock extends Applet implements Runnable{
public final double Ro=100.0d;
public final double Ri=93.0d;
public final double Ri2=97.0d;
public final double Rtemp=10.0d;
public final int pt=110;
public final double s=6.0d;//每秒钟秒针走的度数
public final double m=6.0d;//每分钟分针走的度数
public final double h=30.0d;//每小时时针走的度数
public final double ms=6.0d/60.0d;//每秒钟分针走的度数
public final double hm=30.0d/60.0d;//每分钟时针走的度数
public final double hs=30.0d/1200.0d;//每秒钟时针走的度数
Thread hourhand,minutehand,secondhand;//时针线程,分针线程,秒针线程
Graphics hourpen,minutepen,secondpen;//时针画笔,分针画笔,秒针画笔
Color c;
public void init(){
setSize(2*pt,2*pt);
c=new Color(225,255,255);//表盘面颜色
setBackground(Color.white);//背景色
hourhand=new Thread(this);//线程初始化
minutehand=new Thread(this);
secondhand=new Thread(this);
hourpen=getGraphics();//画笔初始化
hourpen.setColor(Color.black);
minutepen=getGraphics();
minutepen.setColor(Color.black);
secondpen=getGraphics();
secondpen.setColor(Color.red);
}
public void start(){
hourhand.start();//线程启动
minutehand.start();
secondhand.start();
}
public void paint(Graphics g){
int i,angle;
int ox,oy,ix,iy;
g.setPaintMode();
g.setColor(c);
int temp=pt-206/2;
g.fillOval(temp, temp, 206, 206);//画出钟表盘面
g.setColor(Color.black);
g.drawOval(temp, temp, 206, 206);//画出钟表外围轮廓
//画出表点
for(i=0;i<12;i++){
angle=(-1)*i*30;
ox=pt+(int)(Ro*Math.cos(3.1415926/180.0*angle));
oy=pt+(int)(Ro*Math.sin(3.1415926/180.0*angle));
ix=pt+(int)(Ri*Math.cos(3.1415926/180.0*angle));
iy=pt+(int)(Ri*Math.sin(3.1415926/180.0*angle));
switch(i){
case 0:ix=86+pt;break;
case 3:iy=-86+pt;break;
case 6:ix=-86+pt;break;
case 9:iy=86+pt;break;
default:break;
}
g.drawLine(ix,iy,ox,oy);
}
for(i=0;i<60;i++){
angle=(-1)*i*6;
ox=pt+(int)(Ro*Math.cos(3.1415926/180.0*angle));
oy=pt+(int)(Ro*Math.sin(3.1415926/180.0*angle));
ix=pt+(int)(Ri2*Math.cos(3.1415926/180.0*angle));
iy=pt+(int)(Ri2*Math.sin(3.1415926/180.0*angle));
g.drawLine(ix,iy,ox,oy);
}
}
@SuppressWarnings("deprecation")
public void run(){
int hours,minutes,seconds;
double angleh,anglem,angles;
double angleh2,anglem2,angles2;
int x1,y1,x2,y2,x3,y3;
int x4,y4,x5,y5,x6,y6;
Date time=new Date();//时间类初始化
seconds=time.getSeconds();
minutes=time.getMinutes();
hours=time.getHours();
angles=((double)seconds)*s;
angles-=90.0d;
angles2=(angles+180.0)%360;
anglem=((double)minutes)*m+((double)seconds)*ms;
anglem-=90.0d;
anglem2=(anglem+180.0)%360;
angleh=((double)hours)*h+((double)minutes)*hm+((double)seconds)*hs;
angleh-=90.0d;
angleh2=(angleh+180.0)%360;
x1=(int)(90.0d*Math.cos(3.1415926/180.0*angles))+pt;
y1=(int)(90.0d*Math.sin(3.1415926/180.0*angles))+pt;
x4=(int)(Rtemp*Math.cos(3.1415926/180.0*angles2))+pt;
y4=(int)(Rtemp*Math.sin(3.1415926/180.0*angles2))+pt;
x2=(int)(75.0d*Math.cos(3.1415926/180.0*anglem))+pt;
y2=(int)(75.0d*Math.sin(3.1415926/180.0*anglem))+pt;
x5=(int)(Rtemp*Math.cos(3.1415926/180.0*anglem2))+pt;
y5=(int)(Rtemp*Math.sin(3.1415926/180.0*anglem2))+pt;
x3=(int)(65.0d*Math.cos(3.1415926/180.0*angleh))+pt;
y3=(int)(65.0d*Math.sin(3.1415926/180.0*angleh))+pt;
x6=(int)(Rtemp*Math.cos(3.1415926/180.0*angleh2))+pt;
y6=(int)(Rtemp*Math.sin(3.1415926/180.0*angleh2))+pt;
while(true){
Date time1=new Date();
seconds=time1.getSeconds();
minutes=time1.getMinutes();
hours=time1.getHours();
angles=((double)seconds)*s;
angles-=90.0d;
angles2=(angles+180.0)%360;
anglem=((double)minutes)*m+((double)seconds)*ms;
anglem-=90.0d;
anglem2=(anglem+180.0)%360;
angleh=((double)hours)*h+((double)minutes)*hm+((double)seconds)*hs;
angleh-=90.0d;
angleh2=(angleh+180.0)%360;
if(Thread.currentThread()==secondhand){//画秒针
secondpen.setColor(c);
secondpen.drawLine(pt, pt, x1, y1);
secondpen.drawLine(pt, pt, x4, y4);
x1=(int)(90.0d*Math.cos(3.1415926/180.0*angles))+pt;
y1=(int)(90.0d*Math.sin(3.1415926/180.0*angles))+pt;
x4=(int)(Rtemp*Math.cos(3.1415926/180.0*angles2))+pt;
y4=(int)(Rtemp*Math.sin(3.1415926/180.0*angles2))+pt;
secondpen.setColor(Color.red);
secondpen.drawLine(pt, pt, x1, y1);
secondpen.drawLine(pt, pt, x4, y4);
try{
Thread.sleep(40);
}
catch(InterruptedException e){
}
}
else if(Thread.currentThread()==minutehand){//画分针
minutepen.setColor(c);
minutepen.drawLine(pt, pt, x2, y2);
minutepen.drawLine(pt, pt, x5, y5);
x2=(int)(75.0d*Math.cos(3.1415926/180.0*anglem))+pt;
y2=(int)(75.0d*Math.sin(3.1415926/180.0*anglem))+pt;
x5=(int)(Rtemp*Math.cos(3.1415926/180.0*anglem2))+pt;
y5=(int)(Rtemp*Math.sin(3.1415926/180.0*anglem2))+pt;
minutepen.setColor(Color.black);
minutepen.drawLine(pt, pt, x2, y2);
minutepen.drawLine(pt, pt, x5, y5);
try{
Thread.sleep(40);
}
catch(InterruptedException e){
}
}
else if(Thread.currentThread()==hourhand){//画时针
hourpen.setColor(c);
hourpen.drawLine(pt, pt, x3, y3);
hourpen.drawLine(pt, pt, x6, y6);
x3=(int)(50.0d*Math.cos(3.1415926/180.0*angleh))+pt;
y3=(int)(50.0d*Math.sin(3.1415926/180.0*angleh))+pt;
x6=(int)(Rtemp*Math.cos(3.1415926/180.0*angleh2))+pt;
y6=(int)(Rtemp*Math.sin(3.1415926/180.0*angleh2))+pt;
hourpen.setColor(Color.black);
hourpen.drawLine(pt, pt, x3, y3);
hourpen.drawLine(pt, pt, x6, y6);
try{
Thread.sleep(40);
}
catch(InterruptedException e){
}
}
}
}
}