51,409
社区成员
发帖
与我相关
我的任务
分享
import java.awt.*;
import java.util.Date;
import javax.swing.*;
public class DrawClock extends JApplet implements Runnable{
int xCenter,yCenter,width,height,radius;
int xSec,ySec,xMin,yMin,xHour,yHour;
long hour,min,sec;
Date date;
Thread clockThread = null;
public void init(){
width = 100;
height = 100;
xCenter = (int)(width*0.5)+80;
yCenter = (int)(width*0.5)+80;
radius = (int)(Math.min(width, height));
}
public void start(){
if(clockThread==null){
clockThread=new Thread(this);
}
clockThread.start();
}
public void stop(){
clockThread = null;
}
public void run(){
while(null!=clockThread){
repaint();
try{
Thread.sleep(1000);
}
catch(InterruptedException p){
}
}
}
//public synchronized void update(Graphics g){
public void update(Graphics g){
g.setColor(Color.BLUE);
g.fillRect(0, 0,width+1000,height+1000);
//paint(g);
}
public void paint(Graphics g){
int x,y;
g.setColor(Color.red);
g.drawOval(30,30, width+100, height+100);
for(int i=0;i<60;i+=5){
x = (int) (xCenter + radius * Math.sin(i*2*Math.PI/60));
y = (int) (yCenter - radius * Math.cos(i*2*Math.PI/60));
g.fillRect(x-1,y-1,3,3);
}
date = new Date();
hour = date.getHours()%12;
min = date.getMinutes();
sec = date.getSeconds();
xSec = (int)(xCenter + radius*0.9*Math.sin(sec*2*Math.PI/60));
ySec = (int)(yCenter - radius*0.9*Math.cos(sec*2*Math.PI/60));
g.drawLine(xCenter, yCenter, xSec, ySec);
repaint();
}
}