闹钟源代码Java 闹钟java设计代码(13)


//以数字方式显示年、月、日和时间
g.drawString(today,55,415);
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);
lastxs=xs;lastys=ys;
lastxm=xh;lastym=ym;
lastxh=xh;lastyh=yh;
lastdate=today;
if(h12)hh=h;//将系统时间变换到0-11区间
else hh=h-12;
if(hh==0) strike_times=12;//计算整点时钟声数
else strike_times=hh;
if((s==0m==0)||flag){//判断是否整点,是否是主动刷新
if(counterstrike_times){
flag=true;
g.drawImage(gif2,115,35,this);
if(lasts!=s){
if(strike){
counter++;
danger.play();//播放闹铃声
}
if(strike)strike=false;
else strike=true;
}
}
else {
counter=0;
flag=false;
}
}
else
g.drawImage(gif1,115,35,this);
int timedelta;//记录当前时间与闹铃定时的时差
Integer currh,currm,currs;//分别记录当前的时、分、秒
timeNow=new Date();
currh=new Integer(timeNow.getHours());
currm=new Integer(timeNow.getMinutes());
currs=new Integer(timeNow.getSeconds());
//判断是否要更新当前显示的时间,这样可以避免文本框出现频率闪动
if(currh.intValue()!=Integer.valueOf(showhour.getText()).intValue())
showhour.setText(currh.toString());
if(currm.intValue()!=Integer.valueOf(showmin.getText()).intValue())
showmin.setText(currh.toString());
if(currs.intValue()!=Integer.valueOf(showsec.getText()).intValue())
showsec.setText(currh.toString());
if(setalerm){ //判断是否设置了闹钟
//判断当前时间是否为闹钟所定的时间
if((currh.intValue()==fixh)(currm.intValue()==fixm)(currs.intValue()==fixs))
clickflag=true;
timedelta=currm.intValue()*60+currs.intValue()-fixm*60-fixs;
if((timedelta60)(clickflag==true)){ //若当前时间与闹钟相差时间达到60秒
chirp.play();
g.drawImage(clock7,83,280,this);
}
else{
chirp.stop();
clickflag=false;
}
}
if(lasts!=s)
ipAu.play();//播放滴答声
lasts=s;
if(canPaint){
t+=1;
if(t==12)t=0;
}
canPaint=false;
dat=null;
}
public void start(){
if(timer==null){
timer=new Thread(this);//将timer实例化
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){ //采用双缓冲技术的update()方法
if(offscreen_buf==null)
offscreen_buf=createImage(600,420);
Graphics offg=offscreen_buf.getGraphics();
offg.clipRect(1,1,599,419);
paint(offg);
Graphics ong=getGraphics();
ong.clipRect(1,1,599,419);
ong.drawImage(offscreen_buf,0,0,this);
}
public boolean action(Event evt,Object arg){ //按钮事件处理函数
if(evt.target instanceof Button){
String lable=(String)arg;
if(lable.equals("开")){
setalerm=true;
//获取输入的时间
fixh=Integer.valueOf(sethour.getText()).intValue();
fixm=Integer.valueOf(setmin.getText()).intValue();
fixs=Integer.valueOf(setsec.getText()).intValue();
clickflag=false;
}
if(lable.equals("关")){
setalerm=false;
if(chirp!=null)
chirp.stop();
clickflag=false;
}
return true;
}
return false;
}
}
求java闹钟程序源代码import java.util.*;
import java.awt.*;
import java.applet.*;

推荐阅读