/* Clock.java */
import java.util.*;
import java.awt.*;
import java.applet.*;
import java.text.*;
public class Clock extends Applet implements Runnable {
Thread timer = null;
Image clockp, simgs[], animal[]; //clockp 钟面图像 simgs[]:钟摆图像数组
//animal[]:整点时出现的动画
int s,m,h,hh; // s秒 m分 h小时
AudioClip ipAu,towerAu; //ipAu为每秒发出的声音 towerAu 为整点报时声音
boolean canPaint=true;
boolean flag=false;
boolean strike=true;
int counter=0;
int lasts;
Image offscreen_buf = null;
Graphics offg;
int i,j,t = 0;
int timeout= 166;
String img_name[]={"t0","t3","t2","t1","t2","t3", //钟摆出现的顺序是t0 t3 t2...t5 t6 t5 t4
"t0","t4","t5","t6","t5","t4"}; //(钟摆的一个周期)
String gif_name[]={"1","2","3","4","5","6", //整点动画的文件名
"7","8","9","10","11","12"};
int lastxs=0, lastys=0, lastxm=0, lastym=0, lastxh=0, lastyh=0;
Date dummy = new Date();
GregorianCalendar cal = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy MM dd HH:mm:ss ");
String lastdate = df.format(dummy);
Font F=new Font("TimesRoman",Font.PLAIN,14);
Font S=new Font("宋体",Font.BOLD,90);
Date dat = null;
Color fgcol = Color.black;
Color fgcol2 = Color.blue;
Color white=new Color(255,255,255);
public void init() {
ipAu=getAudioClip(getCodeBase(),"ip.au");
towerAu=getAudioClip(getCodeBase(),"TowerClock.au");
offscreen_buf = createImage(700,500);
offg = offscreen_buf.getGraphics();
simgs = new Image[12];
animal =new Image[12];
int xcenter,ycenter,s,m,h;
xcenter=315;
ycenter=112;
s = (int)cal.get(Calendar.SECOND); //初始化指针位置
m = (int)cal.get(Calendar.MINUTE);
h = (int)cal.get(Calendar.HOUR_OF_DAY);
lastxs = (int)(Math.cos(s * 3.14f/30 - 3.14f/2) * 15 + xcenter);
lastys = (int)(Math.sin(s * 3.14f/30 - 3.14f/2) * 15 + ycenter);
lastxm = (int)(Math.cos(m * 3.14f/30 - 3.14f/2) * 13 + xcenter);
lastym = (int)(Math.sin(m * 3.14f/30 - 3.14f/2) * 13 + ycenter);
lastxh = (int)(Math.cos((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 9 + xcenter);
lastyh = (int)(Math.sin((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 9 + ycenter);
lasts=s;
if(h if(h!=11) hh=h+1;
else hh=0;
else
if(h!=23) hh=h-12+1;
else hh=0;
MediaTracker mt = new MediaTracker(this);
for(i = 0; i simgs[i] = getImage(getCodeBase(),img_name[i]+".gif");
mt.addImage (simgs[i],i);
}
clockp = getImage(getDocumentBase(),"clockp.gif");
mt.addImage (clockp,i++);
try{mt.waitForAll();} catch(InterruptedException e){};
//MediaTracker mt = new MediaTracker(this);
for(j = 0; j animal[j] = getImage(getCodeBase(),gif_name[j]+".gif");
mt.addImage (animal[j],j);
}
try{mt.waitForAll();} catch(InterruptedException e){};
}
public void paint(Graphics g) {
int xh, yh, xm, ym, xs, ys,strike_times;
int xcenter,ycenter;
String today;
Integer gif_number;
g.setColor(white);
g.fillRect(1,1,738,499);
xcenter=315;
ycenter=112;
dat = new Date();
cal.setTime(dat);
s = (int)cal.get(Calendar.SECOND);
m = (int)cal.get(Calendar.MINUTE);
h = (int)cal.get(Calendar.HOUR_OF_DAY);
today = df.format(dat); //指针位置
xs = (int)(Math.cos(s * 3.14f/30 - 3.14f/2) * 15 + xcenter);
ys = (int)(Math.sin(s * 3.14f/30 - 3.14f/2) * 15 + ycenter);
xm = (int)(Math.cos(m * 3.14f/30 - 3.14f/2) * 13 + xcenter);
ym = (int)(Math.sin(m * 3.14f/30 - 3.14f/2) * 13 + ycenter);
xh = (int)(Math.cos((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 9 + xcenter);
yh = (int)(Math.sin((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 9 + ycenter);
for(int loop=0;loop {int x=(int)(java.lang.Math.random()*740);
int y=(int)(java.lang.Math.random()*500);
int red=(int)(java.lang.Math.random()*255);
int green=(int)(java.lang.Math.random()*255);
int blue=(int)(java.lang.Math.random()*255);
g.setColor(new Color(red,green,blue));
if((red>70) &&(green>70)&&(blue>70))
g.drawString("*",x,y);
}
g.setFont(F);
lastxs=xs; lastys=ys;
lastxm=xm; lastym=ym;
lastxh=xh; lastyh=yh;
lastdate = today;
if(h else hh=h-12;
if(hh==0) strike_times=12;
else strike_times=hh;
if((s==0 && m==0)||flag){ //判断整点报时
if (counter flag=true;
if( lasts != s){
if(strike){
counter++;
towerAu.play();
}
if (strike) strike=false;
else strike=true;
}
}
else {
counter=0;
flag=false;
}
}
else
if( lasts!=s)
ipAu.play();
lasts = s;
if(s g.drawImage(animal[hh],390,105,this);
if(canPaint){
t +=1;
if(t==12)t=0;
}
g.drawImage(simgs[t],270,140,this); //画钟摆
switch(t)
{
case 0: g.setColor(Color.black);
break;
case 1: g.setColor(Color.magenta);
break;
case 2: g.setColor(Color.blue);
break;
}
g.setFont(S);
g.drawString("大 摆 钟",155,380);
for(int delay=0;delay g.setFont(F);
g.drawImage(clockp,280,71,this); //画钟面
g.setColor(fgcol2);
g.setColor(getBackground());
g.setColor(fgcol2);
g.drawString(today,275, 420);
g.drawLine(xcenter, ycenter, xs, ys); //画指针
g.setColor(fgcol);
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);
g.setColor(new Color(150,200,150));
canPaint=false;
dat=null;
}
public void start() {
int i;
if(timer == null)
{
timer = new Thread(this);
timer.start();
}
}
public void stop() {
timer = null;
}
public void run() {
while (timer != null) {
try {timer.sleep(timeout);} catch (InterruptedException e){}
canPaint=true;
repaint();
}
timer = null;
}
public void update(Graphics g){
if (offscreen_buf == null){
offscreen_buf = createImage(740,500);
}
offg.clipRect(1,1,739,499);
paint(offg);
g.drawImage (offscreen_buf,0,0,this);
}
}