當前位置:編程學習大全網 - 源碼下載 - JAVA 鬧鐘程序

JAVA 鬧鐘程序

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; //創建線程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");

//響應按鈕事件

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提供的是壹個日歷式的東東,上面又多了很多的參數,是方便操作了不少。而Date類的功能遠不及其,求個和日期有聯系的還要自己計算。

GregorianCalendar cal=new GregorianCalendar();

GregorianCalendar cal2=new GregorianCalendar();

SimpleDateFormat df=new SimpleDateFormat("yyyy MM dd HH:mm:ss");//設置時間格式

Date dummy=new Date(); //生成Data對象

String lastdate=df.format(dummy);

Font F=new Font("TimesRoman",Font.PLAIN,14);//設置字體格式

Date dat=null;

Date timeNow;

Color fgcol=Color.blue;

Color fgcol2=Color.darkGray;

Color backcolor=Color.blue;

Label hlabel2,mlabel2,slabel2;//顯示時間單位時所用的標簽(時、分、秒)

int i;

int s,m,h;

TextField sethour,setmin,setsec;//顯示當前時間文本框和定時文本框

//在Applet程序中,首先自動調用初始化完成必要的初始化工作,緊接著自動調用start,在進入執行程序和返回到該頁面時被調用,而從該頁面轉到別的頁面時,stop被調用,關閉瀏覽器時,執行destroy。

public void init()//初始化方法

{

int fieldx=50,fieldy1=120,fieldy2=220,fieldw=30,fieldh=20,space=50;//顯示時間和定時文本框的定位參數

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);

//設置鬧鐘控制按鈕(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);

//將面板加入當前容器中,並設置面板的大小和背景色

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);//為給定組件創建壹個跟蹤媒體的MediaTracker對象,把圖片添加到被跟蹤的圖片組

//Java允?Sapplet?HTML所在的位置(decument base)下?d?Y料,也允?Sapplet?鈉涑淌醬a所在的位置(code base)下?d?Y料。藉由呼叫getDocumentBase()?cgotCodeBase()可得到URL物件。?@些函?湍閼業僥閬胂螺d的?n案的位置

//clockp=getImage(getDocumentBase(),"11.png");

gif1=getImage(getCodeBase(),"2.gif");

//i為id號

mt.addImage(gif1,i++);

try

{

mt.waitForAll();

}

catch(InterruptedException e)

{};//等待加載結束

resize(600,420);//設置窗口大小

}

//窗口顯示有改變的時候調用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();

//用當前時間初始化日歷時間

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)*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);

//設置字體和顏色

g.setFont(F);

//前景色

g.setColor(getBackground()); //取背景色的

g.drawImage(gif1,75,110,this);

//以數字方式顯示年、月、日和時間

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;//記錄當前時間與鬧鈴定時的時差

Integer currh,currm,currs;//分別記錄當前的時、分、秒

Date dat2=new Date();

cal2.setTime(dat2);

//讀取當前時間

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)

{ //判斷是否設置了鬧鐘

//判斷當前時間是否為鬧鐘所定的時間

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))

{

//若當前時間與鬧鐘相差時間超過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;

}

//給創建線程後start之後自動執行的函數

public void run()

{

//在run()方法中,調用repaint()方法,以重繪小程序區,進行時鐘顯示的更新。接著調用sleep方法讓當前線程(也就是我們創建的線程clockthread)睡眠1000毫秒,因為我們每秒鐘要更新壹下顯示,所以讓它睡眠1秒

while(timer!=null)

{

try

{

timer.sleep(1000);

}

catch(InterruptedException e)

{}

//調用repaint時,會首先清除掉paint方法之前的畫的內容,再調用paint方法

repaint();//刷新畫面

}

timer=null;

}

//當AWT接收到壹個applet的重繪請求時,它就調用applet的 update(),默認地,update() 清除applet的背景,然後調用 paint()。重載 update(),將以前在paint()中的繪圖代碼包含在update()中,從而避免每次重繪時將整個區域清除

//有兩種方法可以明顯地減弱閃爍:重載 update()或使用雙緩沖。

//使用雙緩沖技術:另壹種減小幀之間閃爍的方法是使用雙緩沖,它在許多動畫Applet中被使用。其主要原理是創建壹個後臺圖像,將需要繪制的壹幀畫入圖像,然後調用DrawImage()將整個圖像壹次畫到屏幕上去;好處是大部分繪制是離屏的,將離屏圖像壹次繪至屏幕上比直接在屏幕上繪制要有效得多,大大提高做圖的性能。

// 雙緩沖可以使動畫平滑,但有壹個缺點,要分配壹張後臺圖像,如果圖像相當大,這將需要很大壹塊內存;當妳使用雙緩沖技術時,應重載 update()。

public void update(Graphics g)

{

Image offscreen_buf=null;

//采用雙緩沖技術的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 */

}

  • 上一篇:美國電影壹向以簡單粗暴著稱,他們有拍攝過什麽值得壹看的推理片嗎?
  • 下一篇:網絡課程軟件源代碼
  • copyright 2024編程學習大全網