New: the new thread is in the new state. Ready: after the thread is created, it will be in the ready state, waiting for the start() method to be called. Running: the thread enters the running state when it starts execution. Sleeping: the execution of the thread can be temporarily suspended by using the sleep() method. After sleep, the thread will be ready
import java.awt.*;
import java.util.*;
import java.applet.*;
import java.text.SimpleDateFormat;
public class Clock extends Applet implements Runnable
{
private Thread timer=null;
private int lastxh,lastyh,lastxm,lastym,lastxs,lastys; //时分秒针线的位置
private SimpleDateFormat sdf; //日期格式
public void init()
{
lastxs=lastys=lastxm=lastym=lastxh=lastyh=0;
setBackground(Color.white); //设置小程序窗口背景为白色
//下面的语句是设定文字日期的显示格式
sdf=new SimpleDateFormat("yyyy年MM月dd日 a hh时mm分ss秒 EEEEE");
}
public void paint(Graphics g) //显示数字和图形时钟
{
int xh,yh,xm,ym,xs,ys,s,m,h,xcenter,ycenter;
Calendar cal= Calendar.getInstance(); //生成一个日历类对象
Date sdate = new Date(); //获取当前日期和时间
String today = sdf.format(sdate); //转换成一串规定格式的日期和时间字符串
cal.setTime(sdate); //设置日历对象的内容(日期和时间)
s=cal.get(Calendar.SECOND); //获取时钟的秒数
m=cal.get(Calendar.MINUTE); //获取时钟的分钟数
h=cal.get(Calendar.HOUR); //获取时钟的小时数
xcenter=getWidth()/2; ycenter=80; //表盘时钟的原点
xs=(int)(Math.cos(s* Math.PI /30-Math.PI/2)*45+xcenter);
ys=(int)(Math.sin(s*Math.PI/30-Math.PI/2)*45+ycenter);
xm=(int)(Math.cos(m*Math.PI/30-Math.PI/2)*40+xcenter);
ym=(int)(Math.sin(m*Math.PI/30-Math.PI/2)*40+ycenter);
xh=(int)(Math.cos((h*30+m/2)*Math.PI/180-Math.PI/2)*30+xcenter);
yh=(int)(Math.sin((h*30+m/2)*Math.PI/180-Math.PI/2)*30+ycenter);
g.setFont(new Font("TimesRoman",Font.PLAIN,14));
g.setColor(Color.blue);
g.drawOval(xcenter-52,ycenter-52,104,104); //画表盘
g.setColor(Color.darkGray);
g.drawString("9",xcenter-45,ycenter+5);
g.drawString("3",xcenter+40,ycenter+3);
g.drawString("12",xcenter-7,ycenter-37);
g.drawString("6",xcenter-4,ycenter+45);
//时间变化时,需要重新画各个指针,即先消除原有指针,然后画新指针
g.setColor(getBackground()); //用背景色画线,可以消除原来画的线
if (xs!=lastxs||ys!=lastys) //秒针变化
{
g.fillOval(lastxs-5,lastys-5,10,10); //擦除秒针头上的小圆
g.drawLine(xcenter,ycenter,lastxs,lastys); //擦除秒针
}
if (xm!=lastxm||ym!=lastym) //分针变化
{
g.drawLine(xcenter,ycenter-1,lastxm,lastym);
g.drawLine(xcenter-1,ycenter,lastxm,lastym);
}
if (xh!=lastxh||yh!=lastyh) //时针变化
{
g.drawLine(xcenter,ycenter-1,lastxh,lastyh);
g.drawLine(xcenter-1,ycenter,lastxh,lastyh);
}
g.setColor(Color.darkGray);
g.drawString(today,30,180); //显示数字时钟
g.setColor(Color.red);
g.fillOval(xs-5,ys-5,10,10); //画秒针上的小圆
g.drawLine(xcenter,ycenter,xs,ys); //画秒针
g.setColor(Color.blue);
g.drawLine(xcenter,ycenter-1,xm,ym); //用两条线画分针
g.drawLine(xcenter-1,ycenter,xm,ym);
g.drawLine(xcenter,ycenter-1,xh,yh); //用两条线画时针
g.drawLine(xcenter-1,ycenter,xh,yh);
lastxs=xs; lastys=ys; //保存指针位置
lastxm=xm; lastym=ym;
lastxh=xh; lastyh=yh;
}
public void start() //启动线程
{
if(timer==null)
{
timer=new Thread(this); //生成Thread对象实体
timer.start(); //启动生成的线程
}
}
public void stop()
{
if(timer!=null)
{
timer.interrupt(); //中断线程
timer=null; //去掉Thread对象,让系统将这个垃圾对象收走
}
}
public void run() //每隔一秒钟,刷新一次画面
{
while (timer!=null)
{
try { Thread.sleep(1000); }
catch (InterruptedException e) { }
repaint(); //调用paint()方法重画时钟
}
}
}
相应的HTML文件如下:
<Clock.html>
<html>
<applet code="Clock.class"
width=350
height=200 >
</applet>
</html>