我有个时钟,多线程的,你改一下就可以了。import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.applet.*;
public class Lian70 extends Applet implements Runnable
{
Thread shi=null,fen=null,miao=null;
int houra,hourb,munitea,muniteb,seconda,secondb;
int hour=0,munite=0,second=0;
Graphics gs=null,gm=null,gh=null;
double pointy[]=new double[61],pointx[]=new double[61],scaledx[]=new double[61],scaledy[]=new double[61];
int starc=0;
public void init()
{
gh=this.getGraphics();
gh.setColor(Color.cyan);
gs=this.getGraphics();
gs.setColor(Color.red);
gm=this.getGraphics();
gm.setColor(Color.blue);
gs.translate(200,200);
gm.translate(200,200);
gh.translate(200,200);
pointx[0]=0;
pointy[0]=-120;
scaledx[0]=0;
scaledy[0]=-140;
double jiaodu=6*Math.PI/180;
for(int i=0;i<60;i++)
{
pointx[i+1]=pointx[i]*Math.cos(jiaodu)-pointy[i]*Math.sin(jiaodu);
pointy[i+1]=pointy[i]*Math.cos(jiaodu)+pointx[i]*Math.sin(jiaodu);
}
pointx[60]=0;
pointy[60]=-120;
for(int i=0;i<60;i++)
{
scaledx[i+1]=scaledx[i]*Math.cos(jiaodu)-Math.sin(jiaodu)*scaledy[i];
scaledy[i+1]=scaledy[i]*Math.cos(jiaodu)+Math.sin(jiaodu)*scaledx[i];
}
scaledx[60]=0;scaledy[60]=-140;
}
public void start()
{
if(starc>=1)
{
miao.interrupt();
fen.interrupt();
shi.interrupt();
}
miao=new Thread(this);
fen=new Thread(this);
shi=new Thread(this);
miao.start();
fen.start();
shi.start();
starc++;
if(starc>=2)
{
starc=1;
}
}
public void stop()
{
miao.interrupt();
fen.interrupt();
shi.interrupt();
}
public void paint(Graphics g)
{
this.start();
g.drawOval(50,50,300,300);
g.translate(200,200);
for(int i=0;i<60;i++)
{
if(i%5==0)
{
g.setColor(Color.red);
g.fillOval((int)scaledx[i],(int)scaledy[i],8,8);
}
else
{
g.fillOval((int)scaledx[i],(int)scaledy[i],3,3);
}
}
}
public void run()
{
Date date=new Date();
String s=date.toString();
hour=Integer.parseInt(s.substring(11,13));
munite=Integer.parseInt(s.substring(14,16));
second=Integer.parseInt(s.substring(17,19));
if(Thread.currentThread()==miao)
{
seconda=(int)pointx[second];
secondb=(int)pointy[second];
gs.drawLine(0,0,seconda,secondb);
int i=second;
while(true)
{
try
{
miao.sleep(1000);
Color c=getBackground();
gs.setColor(c);
gs.drawLine(0,0,seconda,secondb);
if((seconda==munitea)&&(secondb==muniteb))
{
gm.drawLine(0,0,munitea,muniteb);
}
if((seconda==houra)&&(secondb==hourb))
{
gh.drawLine(0,0,houra,hourb);
}
}
catch(InterruptedException e)
{
Color c=getBackground();
gs.setColor(c);
gs.drawLine(0,0,seconda,secondb);
return;
}
seconda=(int)pointx[(i+1)%60];
secondb=(int)pointy[(i+1)%60];
gs.setColor(Color.red);
gs.drawLine(0,0,seconda,secondb);
i++;
}
}
if(Thread.currentThread()==fen)
{
munitea=(int)pointx[munite];
muniteb=(int)pointy[munite];
gm.drawLine(0,0,munitea,muniteb);
int i=munite;
while(true)
{
try
{
fen.sleep(1000*60-second*1000);second=0;
Color c=getBackground();
gm.setColor(c);
gm.drawLine(0,0,munitea,muniteb);
if((houra==munitea)&&(hourb==muniteb))
{
gh.drawLine(0,0,houra,hourb);
}
}
catch(InterruptedException e)
{
return;
}
munitea=(int)pointx[(i+1)%60];
muniteb=(int)pointy[(i+1)%60];
gm.setColor(Color.blue);
gm.drawLine(0,0,munitea,muniteb);
i++;
second=0;
}
}
if(Thread.currentThread()==shi)
{
int h=hour%12;
houra=(int)pointx[h*5+munite/12];
hourb=(int)pointy[h*5+munite/12];
int i=h*5+munite/12;
gh.drawLine(0,0,houra,hourb);
while(true)
{
try
{
shi.sleep(1000*60*12-1000*60*(munite%12)-second*1000);
munite=0;
Color c=getBackground();
gh.setColor(c);
gh.drawLine(0,0,houra,hourb);
}
catch(InterruptedException e)
{
return;
}
houra=(int)pointx[(i+1)%60];
hourb=(int)pointy[(i+1)%60];
gh.setColor(Color.cyan);
gh.drawLine(0,0,houra,hourb);
i++;
munite=0;
}
}
}
}
试试吧。
[此贴子已经被作者于2005-6-22 9:26:50编辑过]