用java写的音乐时钟万年历.docx

上传人:b****3 文档编号:5394240 上传时间:2023-05-08 格式:DOCX 页数:16 大小:17.60KB
下载 相关 举报
用java写的音乐时钟万年历.docx_第1页
第1页 / 共16页
用java写的音乐时钟万年历.docx_第2页
第2页 / 共16页
用java写的音乐时钟万年历.docx_第3页
第3页 / 共16页
用java写的音乐时钟万年历.docx_第4页
第4页 / 共16页
用java写的音乐时钟万年历.docx_第5页
第5页 / 共16页
用java写的音乐时钟万年历.docx_第6页
第6页 / 共16页
用java写的音乐时钟万年历.docx_第7页
第7页 / 共16页
用java写的音乐时钟万年历.docx_第8页
第8页 / 共16页
用java写的音乐时钟万年历.docx_第9页
第9页 / 共16页
用java写的音乐时钟万年历.docx_第10页
第10页 / 共16页
用java写的音乐时钟万年历.docx_第11页
第11页 / 共16页
用java写的音乐时钟万年历.docx_第12页
第12页 / 共16页
用java写的音乐时钟万年历.docx_第13页
第13页 / 共16页
用java写的音乐时钟万年历.docx_第14页
第14页 / 共16页
用java写的音乐时钟万年历.docx_第15页
第15页 / 共16页
用java写的音乐时钟万年历.docx_第16页
第16页 / 共16页
亲,该文档总共16页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

用java写的音乐时钟万年历.docx

《用java写的音乐时钟万年历.docx》由会员分享,可在线阅读,更多相关《用java写的音乐时钟万年历.docx(16页珍藏版)》请在冰点文库上搜索。

用java写的音乐时钟万年历.docx

用java写的音乐时钟万年历

importjava.awt.*;

importjava.util.*;

importjava.awt.image.*;

importjava.applet.*;

//Film是描述滚动图片的类

classFilmextendsCanvasimplementsRunnable

{

Threadmove;

Imagethumb;

intstartPoint=0;//起始位置

finalintthumbWidth=23;//小图片的宽度

publicFilm(Imagethumb)

{

this.thumb=thumb;

if(move==null)

{

move=newThread(this);

move.start();

}

}

publicvoidrun()

{

while(true)

{

repaint();

try

{

Thread.sleep(100);

}

catch(InterruptedExceptione)

{}

startPoint-=1;//向左移一位

startPoint%=thumbWidth;//超过小图片宽度后回到起始点

}

}

publicvoidpaint(Graphicsg)

{

update(g);

}

publicvoidupdate(Graphicsg)

{inti;

Dimensiond=size();

ImageoffImage=createImage(d.width,d.height);

GraphicsoffG=offImage.getGraphics();

offG.setColor(newColor(160,255,160));

offG.fillRect(0,0,d.width,d.height);

for(i=startPoint;i<=d.width;i+=thumbWidth)

offG.drawImage(thumb,i,0,this);

g.drawImage(offImage,0,0,this);

}

}

//AudioPlayer是描述底部音乐播放条的类

classAudioPlayerextendsPanel

{

AudioClipau;

Buttonplay,loop,stop;

Labellabel;

AudioPlayer(AudioClipau)

{

setLayout(newFlowLayout(FlowLayout.RIGHT,10,5));

add(play=newButton("play"));

add(loop=newButton("loop"));

add(stop=newButton("stop"));

this.au=au;

setBachground(newColor(160,255,160));

}

publicbooleanaction(Evente,Objectp)

{

if(e.target==play)

{au.play();

}

elseif(e.target==loop)

{au.loop();}

elseif(e.target==stop)

{au.stop();}

returnsuper.action(e,p);

}

}

//Calendar是描述日历和时钟的类

classCalendarextendsPanelimplementsRunnable

{

ButtonnextYear,nextMonth,nextDate,lastYear,lastMonth,lastDate;

TextFieldyear,month,date;

FilmmyFilm;

Threadclock;

AudioClipchirp;

Datetoday=newDate();

StringweekName[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};

StringmonthName[]={"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};

intweekColor[]={0xcc6633,0xcccc33,0xcccc33,0xcccc33,0xcccc33,0xcccc33,0x3399cc};

Calendar(Imagethumb,AudioClipau)

{

setLayout(newFlowLayout(FlowLayout.LEFT,3,13));

add(lastYear=newButton("<"));

add(year=newTextField(""+(1900+today.getYear()),4);

add(nextYear=newButton(">"));

add(lastMonth=newButton("<"));

add(month=newTextField(""+(today.getMonth()+1),2));

add(nextMonth=newButton(">"));

add(lastDate=newButton("<"));

add(date=newTextField(""+today.getDate(),2));

add(nextDate=newButton(">"));

myFilm=newFilm(thumb);

myFilm.resize(270,15);

add(myFilm);

chirp=au;

}

publicvoidrun()

{

longstartTime=System.currentTimeMillis();//记录线程开始的时间

while(true)

{

repaint();

startTime+=1000;//计算下次醒来的时间

try{

Thread.sleep(Math.max(0,startTime-System.currentTimeMillis()));

//保证绘图时间和睡眠时间相加为1s,若绘图超过1s,则线程不睡眠

}

catch(InterruptedExceptione){}

}

}

publicvoidpaint(Graphicsg)

{

update(g);

}

publicvoidupdate(Graphicsg)

{

inti,j;

Dimensiond=size();

ImageoffImage=createImage(d.width,d.height);

GraphicsoffG=offImage.getGraphics();

//设置背景色

setBackground(newColor(160,255,160));

offG.setColor(getBackground());

offG.fillRect(0,0,500,350);

//画日历背景

offG.setColor(newColor(80,150,80));

offG.fillRect(25,100,340,230);

//drawcalendarbase

offG.setColor(newColor(100,230,195));

offG.fillRect(15,90,340,230);

offG.setFont(newFont("Helvetica",Font.BOLD,20));

for(i=0;i<=6;i++)

{

offG.setColor(Color.darkGray);

offG.drawString(weekName[i],26+i*48,110);

offG.setColor(Color.white);

offG.drawString(weekName[i],24+i*48,108);

}

//画垂直滚动条

for(i=350;i>0;i-=10)

{

offG.setColor(newColor(170+85*i/350,255,170+85*i/350));

offG.fillRect(380,i,40,9);

}

//画年

offG.setColor(newColor(200,255,200));

offG.setFont(newFont("Courier",Font.ITALIC,66));

StringyearString=""+(1900+today.getYear());

for(j=0;j<=6;j+=2)

for(i=0;i<=3;i++)

offG.drawString(yearString.substring(i,i+1),415+j,168+49*i);

//画水平滚动条

offG.setColor(Color.black);

offG.drawLine(6,60,250,60);

offG.setColor(newColor(100,200,100));

offG.drawLine(5,65,495,65);

offG.drawLine(5,66,495,66);

//画圆

offG.setColor(Color.yellow);

offG.drawOval(360,5,120,120);

offG.setColor(Color.orange);

offG.drawOval(370,15,100,100);

//画月

offG.setFont(newFont("Monospaced",Font.BOLD+Font.ITALIC,25));

offG.setColor(Color.orange);

offG.drawString(monthName[today.getMonth()],280,57);

//画日期

FontmyFont=newFont("DialogInput",Font.BOLD,50);

FontMetricsfontSize=getFontMetrics(myFont);

offG.setFont(myFont);

Stringtemp=""+today.getDate();

ColordateColor=newColor(weekColor[today.getDay()]);

intx=380-fontSize.stringWidth(temp)/2;

inty=59;

offG.setColor(dateColor.brighter());

offG.drawString(temp,x-1,y-1);

offG.setColor(dateColor.darker());

offG.drawString(temp,x+1,y+1);

offG.setColor(dateColor);

offG.drawString(temp,x,y);

//画时钟

offG.setColor(Color.yellow);

offG.drawOval(360,60,10,10);//9点

offG.drawOval(470,60,10,10);//3点

offG.drawOval(415,5,10,10);//12点

offG.drawOval(415,115,10,10);//6点

offG.drawOval(371,36,5,5);//10点

offG.drawOval(391,16,5,5);//11点

offG.drawOval(444,16,5,5);//1点

offG.drawOval(463,36,5,5);//2点

offG.drawOval(363,89,5,5);//4点

offG.drawOval(444,109,5,5);//5点

offG.drawOval(391,109,5,5);//7点

offG.drawOval(371,89,5,5);//8点

Datenow=newDate();

doublehour=now.getHours()+now.getMinutes()/60;

doubleminute=now.getMinutes()+now.getSeconds()/60;

doublesecond=now.getSeconds();

offG.setColor(Color.black);

offG.drawLine(420,65,(int)(420+30*Math.sin(hour*Math.PI/6)),(int)(65-30*Math.cos(hour*Math.PI/6)));

offG.drawLine(420,65,(int)(420+45*Math.sin(minute*Math.PI/30)),(int)(65-45*Math.cos(minute*Math.PI/30)));

offG.setColor(Color.pink);

offG.drawLine(420,65,(int)(420+48*Math.sin(second*Math.PI/30)),(int)(65-48*Math.cos(second*Math.PI/30)));

//绘日期

booleanraised;

offG.setFont(newFont("Serif",Font.PLAIN,15));

DatenewDay=newDate(today.getYear(),today.getMonth(),1);

newDay.setDate(1-newDay.getDay());//找出日历左上角的日期

for(j=0;j<=5;j++)

{

for(i=0;i<=6;i++)

{

offG.setColor(newColor(220,255,165));

if(today.getDate()!

=newDay.getDate()||today.getMonth!

=newDay.getMonth())

raised=true;//其他日期,按钮上浮

else

raised=false;//当前日期,按钮下浮

offG.fill3DRect(17+i*48,114+j*34,48,34,raised);

if(today.getMonth()==newDay.getMonth())

offG.setColor(Color.black);//本月日期,黑色

else

offG.setColor(Color.lightGray);//他月日期,浅色

offG.drawString(""+newDay.getDate(),20+i*48,129+j*34);

newDay.setDate(newDay.getDate+1);

}

}

g.drawImage(offImage,0,0,this);

}

publicbooleanaction(Eventevt,Objectarg)

{

if(evt.target.equals(lastYear))

{//上一年

today.setYear(today.getYear()-1);

repaint();

}

if(evt.target.equals(nextYear))

{//下一年

today.setYear(today.getYear()+1);

repaint();

}

if(evt.target.equals(lastMonth))

{//上个月

today.setMonth(today.getMonth()-1);

repaint();

}

if(evt.target.equals(nextMonth))

{//下个月

today.setMonth(today.getMonth()+1);

repaint();

}

if(evt.target.equals(lastDate))

{//前一天

today.setDate(today.getDate()-1);

repaint();

}

if(evt.target.equals(nextDate))

{//后一天

today.setDate(today.getDate()+1);

repaint();

}

setDateText();

returntrue;

}

publicbooleanmouseDown(Eventevt,intx,inty)

{

if(x>16&&x<354&&y>113&&y<319)

{

DatenewDay=newDate(today.getYear(),today.getMonth(),1);

newDay.setDate(1-newDay.getDay());

intColumn=(int)(x-17)/48;//鼠标点在日历的哪一列

intRow=(int)(y-114)/34;//鼠标点在日历的哪一行

newDay.setDate(newDay.getDate()+Row*7+Column);

today=newDate(newDay.getTime());

chirp.play();

setDateText();

repaint();

returntrue;

}

returnfalse;

}

publicbooleanlostFocus(Eventevt,Objectwhat)

{

intnum;

try

{

if(evt.target.equals(year))

{

num=Integer.valueOf(year.getText()).intValue();

if(num!

=today.getYear()&&num>=1000&&num<=9999)

{

today.setYear(num-1900);

repaint();

}

}

if(evt.target.equals(month))

{num=Integer.valueOf(month.getText()).intValue();

if(num!

=today.getMonth()&&num>=1&&num<=12)

{

today.setMonth(num-1);

repaint();

}

}

if(evt.target.equals(date))

{num=Integer.valueOf(date.getText()).intValue();

if(num!

=today.getDate())

{

DatenewDay=newDate(today.getTime());

newDay.setDate(num);

if(newDay.getMonth()==today.getMonth())

{

today.setDate(num);

repaint();

}

}

}

}

catch(NumberFormatExceptione)

{//输入的不是数字

}

setDateText();输入不合法,将日期设为原值

returntrue;

}

publicbooleankeyDown(Eventevt,intkey)

{

if(key==10)return(lostFocus(evt,evt.arg));//按了回车键

elsereturn(super.keyDown(evt,key));

}

voidsetDateText()

{

year.setText(""+(1900+today.getYear()));

month.setText(""+(today.getMonth()+1));

date.setText(""+today.getDate());

}

}

publicclassMyAppletextendsApplet

{

Calendarcalendar;

AudioPlayeraudioPlayer;

publicvoidinit()

{

Stringauname=getParameter("auname");

if(auname==null)

auname="ding.au";

AudioClipau=getAudioClip(getCodeBase(),auname);

audioPlayer=newAudioPlayer(au);

Imagethumb=getImage(getCodeBase(),"thumb.gif");

au=getAudioClip(getCodeBase(),"chirp.au");

calendar=newCalendar(thumb,au);

setLayout(newBorderLayout());

add("Center",calendar);

add("South",audioPlayer);

}

publicvoidstart()

{

if(calendar.myFilm.move==null)

{

calendar.myFilm.move=newThead(calendar.myFilm);

calendar.myFilm.move.start();

}

if(calendar.clock==null)

{

calendar.clock=newThread(calendar);

calendar.clock.start();

}

}

publicvoidstop()

{

calendar.myFilm.move.stop();

calendar.myFilm.move=null;

calendar.clock.stop(0;

calendar.clock=null;

}

publicvoiddestroy()

{

audioPlayer.au.stop();

super.destroy();

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2