import java.util.*;
作為一家“創(chuàng)意+整合+營銷”的成都網(wǎng)站建設(shè)機構(gòu),我們在業(yè)內(nèi)良好的客戶口碑。創(chuàng)新互聯(lián)公司提供從前期的網(wǎng)站品牌分析策劃、網(wǎng)站設(shè)計、做網(wǎng)站、網(wǎng)站建設(shè)、創(chuàng)意表現(xiàn)、網(wǎng)頁制作、系統(tǒng)開發(fā)以及后續(xù)網(wǎng)站營銷運營等一系列服務(wù),幫助企業(yè)打造創(chuàng)新的互聯(lián)網(wǎng)品牌經(jīng)營模式與有效的網(wǎng)絡(luò)營銷方法,創(chuàng)造更大的價值。
import java.awt.*;
import java.applet.*;
import java.text.*;
public class AlarmClock extends Applet implements Runnable
{
Thread timer=null; //創(chuàng)建線程timer
Image clockp,gif1,gif2,clock6,clock7; //clockp:鬧鐘的外殼,鬧鈴和報時鳥
int s,m,h,hh;
AudioClip ipAu,danger,chirp;
boolean canPaint=true;
boolean flag=false;
boolean strike=true;
int counter=0;
int lasts;
Image offscreen_buf=null;
int i,j,t=0;
int timeout=166;
int lastxs=0,lastys=0,lastxm=0,lastym=0,lastxh=0,lastyh=0;
Date dummy=new Date(); //生成Data對象
GregorianCalendar cal=new GregorianCalendar();
SimpleDateFormat df=new SimpleDateFormat("yyyy MM dd HH:mm:ss");//設(shè)置時間格式
String lastdate=df.format(dummy);
Font F=new Font("TimesRoman",Font.PLAIN,14);//設(shè)置字體格式
Date dat=null;
Date timeNow=null;
Color fgcol=Color.blue;
Color fgcol2=Color.darkGray;
Panel setpanel;
Color backcolor=Color.pink;
TextField showhour,showmin,showsec,sethour,setmin,setsec;//顯示當(dāng)前時間文本框和定時文本框
Button onbutton;
Button offbutton;
Label hlabel1,mlabel1,slabel1,hlabel2,mlabel2,slabel2;//顯示時間單位時所用的標(biāo)簽(時、分、秒)
Label info1=new Label("歡迎使用定時提醒鬧鐘"),info2=new Label("");
Label note1=new Label("當(dāng)前時間:"),note2=new Label("鬧鐘設(shè)置:");
boolean setalerm=false,clickflag=false;//判斷是否響鈴和振動
int fixh=0,fixm=0,fixs=0;//記錄鬧鐘的定時
public void init()//初始化方法
{
Integer gif_number;
int fieldx=50,fieldy1=120,fieldy2=220,fieldw=30,fieldh=20,space=50;//顯示時間和定時文本框的定位參數(shù)
setLayout(null); //將布局管理器初始化為null
setpanel=new Panel();
setpanel.setLayout(null);
setpanel.add(note1);
setpanel.add(note2);
note1.setBounds(30,100,60,20);
note1.setBackground(backcolor);
note1.setForeground(Color.black);
note2.setBounds(30,180,60,20);
note2.setBackground(backcolor);
note2.setForeground(Color.black);
hlabel1=new Label();
mlabel1=new Label();
slabel1=new Label();
hlabel2=new Label();
mlabel2=new Label();
slabel2=new Label();
//顯示當(dāng)前時間用的文本框
showhour=new TextField("00",5);
showmin=new TextField("00",5);
showsec=new TextField("00",5);
//定時用的文本框(時、分、秒)
sethour=new TextField("00",5);
setmin=new TextField("00",5);
setsec=new TextField("00",5);
//當(dāng)前時間用的文本框的位置、大小
setpanel.add(showhour);
showhour.setBounds(fieldx,fieldy1,fieldw,fieldh);
showhour.setBackground(Color.white);
//在文本框后加入單位“時”
setpanel.add(hlabel1);
hlabel1.setText("時");
hlabel1.setBackground(backcolor);
hlabel1.setForeground(Color.black);
hlabel1.setBounds(fieldx+fieldw+3,fieldy1,14,20);
fieldx=fieldx+space;
//當(dāng)前時間的分鐘文本框的位置、大小
setpanel.add(showmin);
showmin.setBounds(fieldx,fieldy1,fieldw,fieldh);
showmin.setBackground(Color.white);
//在文本框后加入單位“分”
setpanel.add(mlabel1);
mlabel1.setText("分");
mlabel1.setBackground(backcolor);
mlabel1.setForeground(Color.black);
mlabel1.setBounds(fieldx+fieldw+3,fieldy1,14,20);
fieldx=fieldx+space;
//當(dāng)前時間的秒文本框的位置、大小
setpanel.add(showsec);
showsec.setBounds(fieldx,fieldy1,fieldw,fieldh);
showsec.setBackground(Color.white);
//在文本框后加入單位“秒”
setpanel.add(slabel1);
slabel1.setText("秒");
slabel1.setBackground(backcolor);
slabel1.setForeground(Color.black);
slabel1.setBounds(fieldx+fieldw+3,fieldy1,14,20);
fieldx=50;
//定時的小時文本框的位置、大小
setpanel.add(sethour);
sethour.setBounds(fieldx,fieldy2,fieldw,fieldh);
sethour.setBackground(Color.white);
//在文本框后加入單位“時”
setpanel.add(hlabel2);
hlabel2.setText("時");
hlabel2.setBackground(backcolor);
hlabel2.setForeground(Color.black);
hlabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);
fieldx=fieldx+space;
//定時的分鐘文本框的位置、大小
setpanel.add(setmin);
setmin.setBounds(fieldx,fieldy2,fieldw,fieldh);
setmin.setBackground(Color.white);
//在文本框后加入單位“分”
setpanel.add(mlabel2);
mlabel2.setText("分");
mlabel2.setBackground(backcolor);
mlabel2.setForeground(Color.black);
mlabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);
fieldx=fieldx+space;
//定時的秒文本框的位置、大小
setpanel.add(setsec);
setsec.setBounds(fieldx,fieldy2,fieldw,fieldh);
setsec.setBackground(Color.white);
//在文本框后加入單位“秒”
setpanel.add(slabel2);
slabel2.setText("秒");
slabel2.setBackground(backcolor);
slabel2.setForeground(Color.black);
slabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);
//設(shè)置鬧鐘控制按鈕(on,off)
onbutton=new Button("開");
offbutton=new Button("關(guān)");
setpanel.add(onbutton);
setpanel.add(offbutton);
onbutton.setBounds(90,180,40,20);
offbutton.setBounds(140,180,40,20);
//加入一些附加的信息標(biāo)簽(題頭,題尾)
setpanel.add(info1);
info1.setBackground(backcolor);
info1.setForeground(Color.blue);
info1.setBounds(50,50,150,20);
setpanel.add(info2);
info2.setBackground(backcolor);
info2.setForeground(Color.blue);
info2.setBounds(150,280,100,20);
//將面板加入當(dāng)前容器中,并設(shè)置面板的大小和背景色
add(setpanel);
setpanel.setBounds(300,1,250,420);
setpanel.setBackground(backcolor);
//獲取聲音文件
ipAu=getAudioClip(getDocumentBase(),"bells/仙劍.mid");
danger=getAudioClip(getDocumentBase(),"bells/0.mid");
chirp=getAudioClip(getDocumentBase(),"bells/3.mid");
int xcenter,ycenter,s,m,h;
xcenter=145;
ycenter=162;
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)*30+xcenter);
lastys=(int)(Math.sin(s*3.14f/30-3.14f/2)*30+ycenter);
lastxm=(int)(Math.cos(m*3.14f/30-3.14f/2)*25+xcenter);
lastym=(int)(Math.sin(m*3.14f/30-3.14f/2)*25+ycenter);
lastxh=(int)(Math.cos((h*30+m/2)*3.14f/180-3.14f/2)*18+xcenter);
lastyh=(int)(Math.sin((h*30+m/2)*3.14f/180-3.14f/2)*18+ycenter);
lasts=s;
MediaTracker mt=new MediaTracker(this);//創(chuàng)建Tracke對象
clockp=getImage(getDocumentBase(),"休閑.png");
gif1=getImage(getDocumentBase(),"gif1.gif");
gif2=getImage(getDocumentBase(),"gif2.gif");
clock6=getImage(getDocumentBase(),"clock6.gif");
clock7=getImage(getDocumentBase(),"clock7.gif");
mt.addImage(clockp,i++);
mt.addImage(gif1,i++);
mt.addImage(gif2,i++);
mt.addImage(clock6,i++);
mt.addImage(clock7,i++);
try{mt.waitForAll();}catch(InterruptedException e){};//等待加載結(jié)束
resize(600,420);//設(shè)置窗口大小
}
public void paint(Graphics g){//重寫paint()方法
int xh,yh,xm,ym,xs,ys,strike_times;
int xcenter,ycenter;
String today;
Integer gif_number;
xcenter=148;
ycenter=186;
dat=new Date();
cal.setTime(dat);
//讀取當(dāng)前時間
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)*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);
//設(shè)置字體和顏色
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);
//以數(shù)字方式顯示年、月、日和時間
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;//將系統(tǒng)時間變換到0-11區(qū)間
else hh=h-12;
if(hh==0) strike_times=12;//計算整點時鐘聲數(shù)
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;//記錄當(dāng)前時間與鬧鈴定時的時差
Integer currh,currm,currs;//分別記錄當(dāng)前的時、分、秒
timeNow=new Date();
currh=new Integer(timeNow.getHours());
currm=new Integer(timeNow.getMinutes());
currs=new Integer(timeNow.getSeconds());
//判斷是否要更新當(dāng)前顯示的時間,這樣可以避免文本框出現(xiàn)頻率閃動
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){ //判斷是否設(shè)置了鬧鐘
//判斷當(dāng)前時間是否為鬧鐘所定的時間
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)){ //若當(dāng)前時間與鬧鐘相差時間達到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){ //采用雙緩沖技術(shù)的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){ //按鈕事件處理函數(shù)
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("關(guān)")){
setalerm=false;
if(chirp!=null)
chirp.stop();
clickflag=false;
}
return true;
}
return false;
}
}
其實這個很有趣。自己寫很有意思
先畫每個數(shù)字.....
每個數(shù)字圖樣分7段,給每一段編號,
5 1
6
4 2
3
然后設(shè)計10個長度為7的數(shù)組,作為每個數(shù)字的字體
boolean[][] font={
{true, true,true,true,true,true,false},//數(shù)字0的字樣控制開關(guān)
{false, true,true,false,false,false,false},//數(shù)字1的字樣控制開關(guān)
//等等等等
{true, true,true,true,false,true,true},//數(shù)字9的字樣控制開關(guān)
}
或者,熟練的就用一個byte數(shù)組就夠了
byte[] font={
0x3F, ////數(shù)字0的字樣控制開關(guān)
0x06,//數(shù)字1的字樣控制開關(guān)
//等等
0x6F//數(shù)字9的字樣控制開關(guān)
}
然后畫幾個黑段拼出一個數(shù)字,對應(yīng)數(shù)組顯示,用6個數(shù)字實例拼出最終效果,
這是模擬真機的寫法.
如果要簡單,直接拿0-9共10張圖片對應(yīng)數(shù)值就可以了。
import java.util.*;
import java.awt.*;
import java.applet.*;
import java.text.*;
import java.awt.event.*;
public class Alarm extends Applet implements Runnable
{
Thread timer=null; //創(chuàng)建線程timer
Image gif1; //clockp:鬧鐘的外殼,鬧鈴和報時物
boolean setflag=false,stopflag=false,cancelflag=false;
Panel setpanel;
//獲取聲音文件
AudioClip ring=getAudioClip(getCodeBase(), "1.mid");
Button setbutton=new Button("SET");
Button cancelbutton=new Button("CANCEL");
Button stopbutton=new Button("STOP");
//響應(yīng)按鈕事件
private ActionListener setli=new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setflag=true;
}
};
private ActionListener cancelli=new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setflag=true;
}
};
private ActionListener stopli=new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
ring.stop();
//清除的方法
//g.clearRect(83,280,20,30);
}
};
Label note1=new Label("Alarm clock:");
//GregorianCalendar提供的是一個日歷式的東東,上面又多了很多的參數(shù),是方便操作了不少。而Date類的功能遠不及其,求個和日期有聯(lián)系的還要自己計算。
GregorianCalendar cal=new GregorianCalendar();
GregorianCalendar cal2=new GregorianCalendar();
SimpleDateFormat df=new SimpleDateFormat("yyyy MM dd HH:mm:ss");//設(shè)置時間格式
Date dummy=new Date(); //生成Data對象
String lastdate=df.format(dummy);
Font F=new Font("TimesRoman",Font.PLAIN,14);//設(shè)置字體格式
Date dat=null;
Date timeNow;
Color fgcol=Color.blue;
Color fgcol2=Color.darkGray;
Color backcolor=Color.blue;
Label hlabel2,mlabel2,slabel2;//顯示時間單位時所用的標(biāo)簽(時、分、秒)
int i;
int s,m,h;
TextField sethour,setmin,setsec;//顯示當(dāng)前時間文本框和定時文本框
//在Applet程序中,首先自動調(diào)用初始化完成必要的初始化工作,緊接著自動調(diào)用start,在進入執(zhí)行程序和返回到該頁面時被調(diào)用,而從該頁面轉(zhuǎn)到別的頁面時,stop被調(diào)用,關(guān)閉瀏覽器時,執(zhí)行destroy。
public void init()//初始化方法
{
int fieldx=50,fieldy1=120,fieldy2=220,fieldw=30,fieldh=20,space=50;//顯示時間和定時文本框的定位參數(shù)
setLayout(null); //將布局管理器初始化為null
setpanel=new Panel();
setpanel.setLayout(null);
setpanel.add(note1);
note1.setBounds(30,100,60,20);
note1.setBackground(backcolor);
note1.setForeground(Color.black);
//定時用的文本框(時、分、秒)
sethour=new TextField("00",5);
setmin=new TextField("00",5);
setsec=new TextField("00",5);
hlabel2=new Label();
mlabel2=new Label();
slabel2=new Label();
//定時的小時文本框的位置、大小
setpanel.add(sethour);
sethour.setBounds(fieldx,fieldy2,fieldw,fieldh);
sethour.setBackground(Color.white);
//在文本框后加入單位“時”
setpanel.add(hlabel2);
hlabel2.setText("h");
hlabel2.setBackground(backcolor);
hlabel2.setForeground(Color.black);
hlabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);
fieldx=fieldx+space;
//定時的分鐘文本框的位置、大小
setpanel.add(setmin);
setmin.setBounds(fieldx,fieldy2,fieldw,fieldh);
setmin.setBackground(Color.white);
//在文本框后加入單位“分”
setpanel.add(mlabel2);
mlabel2.setText("m");
mlabel2.setBackground(backcolor);
mlabel2.setForeground(Color.black);
mlabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);
fieldx=fieldx+space;
//定時的秒文本框的位置、大小
setpanel.add(setsec);
setsec.setBounds(fieldx,fieldy2,fieldw,fieldh);
setsec.setBackground(Color.white);
//在文本框后加入單位“秒”
setpanel.add(slabel2);
slabel2.setText("s");
slabel2.setBackground(backcolor);
slabel2.setForeground(Color.black);
slabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);
//設(shè)置鬧鐘控制按鈕(on,off)
setpanel.add(cancelbutton);
setpanel.add(setbutton);
setpanel.add(stopbutton);
cancelbutton.setBounds(90,180,40,20);
setbutton.setBounds(140,180,40,20);
stopbutton.setBounds(522,180,40,20);
setbutton.addActionListener(setli);
cancelbutton.addActionListener(cancelli);
stopbutton.addActionListener(stopli);
stopbutton.setVisible(false);
//將面板加入當(dāng)前容器中,并設(shè)置面板的大小和背景色
add(setpanel);
setpanel.setBounds(300,1,250,420);
setpanel.setBackground(backcolor);
/*int xcenter,ycenter,s,m,h;
//鬧鐘中心點所在位置
xcenter=145;
ycenter=162;
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)*30+xcenter);
lastys=(int)(Math.sin(s*3.14f/30-3.14f/2)*30+ycenter);
lastxm=(int)(Math.cos(m*3.14f/30-3.14f/2)*25+xcenter);
lastym=(int)(Math.sin(m*3.14f/30-3.14f/2)*25+ycenter);
lastxh=(int)(Math.cos((h*30+m/2)*3.14f/180-3.14f/2)*18+xcenter);
lastyh=(int)(Math.sin((h*30+m/2)*3.14f/180-3.14f/2)*18+ycenter);
lasts=s; */
MediaTracker mt=new MediaTracker(this);//為給定組件創(chuàng)建一個跟蹤媒體的MediaTracker對象,把圖片添加到被跟蹤的圖片組
//Java允?Sapplet??HTML所在的位置(decument base)下?d?Y料,也允?Sapplet?鈉涑淌醬a所在的位置(code base)下?d?Y料。藉由呼叫g(shù)etDocumentBase()?cgotCodeBase()可得到URL物件。?@些函?????湍閼業(yè)僥閬胂螺d的?n案的位置
//clockp=getImage(getDocumentBase(),"11.png");
gif1=getImage(getCodeBase(),"2.gif");
//i為id號
mt.addImage(gif1,i++);
try
{
mt.waitForAll();
}
catch(InterruptedException e)
{};//等待加載結(jié)束
resize(600,420);//設(shè)置窗口大小
}
//窗口顯示有改變的時候調(diào)用paint
public void paint(Graphics g)
{//重寫paint()方法
int xh,yh,xm,ym,xs,ys,strike_times;
int xcenter,ycenter;
String today;
xcenter=148;
ycenter=186;
dat=new Date();
//用當(dāng)前時間初始化日歷時間
cal.setTime(dat);
//讀取當(dāng)前時間
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)*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)*12+xcenter);
yh=(int)(Math.sin((h*30+m/2)*3.14f/180-3.14f/2)*12+ycenter);
//設(shè)置字體和顏色
g.setFont(F);
//前景色
g.setColor(getBackground()); //取背景色的
g.drawImage(gif1,75,110,this);
//以數(shù)字方式顯示年、月、日和時間
g.drawString(today,55,415);
//畫指針
g.drawLine(xcenter,ycenter,xs,ys);
g.drawLine(xcenter,ycenter-1,xm,ym); //(x1,y1,x2,y2)
g.drawLine(xcenter-1,ycenter,xm,ym);
g.drawLine(xcenter,ycenter-1,xh,yh);
g.drawLine(xcenter-1,ycenter,xh,yh);
int timedelta;//記錄當(dāng)前時間與鬧鈴定時的時差
Integer currh,currm,currs;//分別記錄當(dāng)前的時、分、秒
Date dat2=new Date();
cal2.setTime(dat2);
//讀取當(dāng)前時間
currh=(int)cal2.get(Calendar.SECOND);
currm=(int)cal2.get(Calendar.MINUTE);
currs=(int)cal2.get(Calendar.HOUR_OF_DAY);
//這樣做的話說我API已過時
//timeNow=new Date();
//currh=new Integer(timeNow.getHours());
//currm=new Integer(timeNow.getMinutes());
//currs=new Integer(timeNow.getSeconds());
if(setflag)
{ //判斷是否設(shè)置了鬧鐘
//判斷當(dāng)前時間是否為鬧鐘所定的時間
if((currh.intValue()==Integer.valueOf(sethour.getText()).intValue())(currm.intValue()==Integer.valueOf(setmin.getText()).intValue())(currs.intValue()==Integer.valueOf(setsec.getText()).intValue()))
{
ring.play();
g.drawImage(gif1,83,280,this);
stopbutton.setVisible(true);
}
timedelta=currm.intValue()*60+currs.intValue()-Integer.valueOf(setmin.getText()).intValue()*60-Integer.valueOf(setsec.getText()).intValue();
if((timedelta=30))
{
//若當(dāng)前時間與鬧鐘相差時間超過30秒,鬧鐘自動停
ring.stop();
//清除的方法
g.clearRect(83,280,20,30);
}
}
dat=null;
}
public void start()
{
if(timer==null)
{
timer=new Thread(this);//將timer實例化
timer.start();
}
}
public void stop()
{
timer=null;
}
//給創(chuàng)建線程后start之后自動執(zhí)行的函數(shù)
public void run()
{
//在run()方法中,調(diào)用repaint()方法,以重繪小程序區(qū),進行時鐘顯示的更新。接著調(diào)用sleep方法讓當(dāng)前線程(也就是我們創(chuàng)建的線程clockthread)睡眠1000毫秒,因為我們每秒鐘要更新一下顯示,所以讓它睡眠1秒
while(timer!=null)
{
try
{
timer.sleep(1000);
}
catch(InterruptedException e)
{}
//調(diào)用repaint時,會首先清除掉paint方法之前的畫的內(nèi)容,再調(diào)用paint方法
repaint();//刷新畫面
}
timer=null;
}
//當(dāng)AWT接收到一個applet的重繪請求時,它就調(diào)用applet的 update(),默認地,update() 清除applet的背景,然后調(diào)用 paint()。重載 update(),將以前在paint()中的繪圖代碼包含在update()中,從而避免每次重繪時將整個區(qū)域清除
//有兩種方法可以明顯地減弱閃爍:重載 update()或使用雙緩沖。
//使用雙緩沖技術(shù):另一種減小幀之間閃爍的方法是使用雙緩沖,它在許多動畫Applet中被使用。其主要原理是創(chuàng)建一個后臺圖像,將需要繪制的一幀畫入圖像,然后調(diào)用DrawImage()將整個圖像一次畫到屏幕上去;好處是大部分繪制是離屏的,將離屏圖像一次繪至屏幕上比直接在屏幕上繪制要有效得多,大大提高做圖的性能。
// 雙緩沖可以使動畫平滑,但有一個缺點,要分配一張后臺圖像,如果圖像相當(dāng)大,這將需要很大一塊內(nèi)存;當(dāng)你使用雙緩沖技術(shù)時,應(yīng)重載 update()。
public void update(Graphics g)
{
Image offscreen_buf=null;
//采用雙緩沖技術(shù)的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);
}
/** Creates a new instance of AlarmClock */
}
網(wǎng)頁題目:java音樂時鐘源代碼 java編寫時鐘
網(wǎng)頁網(wǎng)址:http://aaarwkj.com/article14/hhpege.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、Google、企業(yè)建站、自適應(yīng)網(wǎng)站、App設(shè)計、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)