java小闹钟代码 java闹钟小程序( 三 )


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)*30+xcenter);
ys=(int)(Math.sin(s*3.14f/30-3.14f/2)*30+ycenter);
xm=(int)(Math.cos(m*3.14f/30-3.14f/2)*25+xcenter);
ym=(int)(Math.sin(m*3.14f/30-3.14f/2)*25+ycenter);
xh=(int)(Math.cos((h*30+m/2)*3.14f/180-3.14f/2)*18+xcenter);
yh=(int)(Math.sin((h*30+m/2)*3.14f/180-3.14f/2)*18+ycenter);
//设置字体和颜色
g.setFont(F);
g.setColor(fgcol);
g.setColor(fgcol2);
g.setColor(getBackground());
g.fillRect(1,1,634,419);
g.drawImage(clockp,75,110,this);
g.drawImage(clock6,83,280,this);
g.setColor(fgcol2);
g.setColor(getBackground());
g.setColor(fgcol2);
//以数字方式显示年、月、日和时间
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){//判断是否设置java小闹钟代码了闹钟
//判断当前时间是否为闹钟所定的时间
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();

推荐阅读