用java写的音乐时钟万年历
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
import java.awt.*;
import java.util.*;
import java.awt.image.*;
import java.applet.*;
//Film是描述滚动图片的类
class Film extends Canvas implements Runnable
{
Thread move;
Image thumb;
int startPoint=0;//起始位置
final int thumbWidth=23;//小图片的宽度
public Film(Image thumb)
{
this.thumb=thumb;
if(move==null)
{
move=new Thread(this);
move.start();
}
}
public void run()
{
while(true)
{
repaint();
try
{
Thread.sleep(100);
}
catch(InterruptedException e)
{}
startPoint-=1;//向左移一位
startPoint%=thumbWidth;//超过小图片宽度后回到起始点}
}
public void paint(Graphics g)
{
update(g);
}
public void update(Graphics g)
{int i;
Dimension d=size();
Image offImage=createImage(d.width,d.height); Graphics offG=offImage.getGraphics();
offG.setColor(new Color(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是描述底部音乐播放条的类
class AudioPlayer extends Panel
{
AudioClip au;
Button play,loop,stop;
Label label;
AudioPlayer(AudioClip au)
{
setLayout(new FlowLayout(FlowLayout.RIGHT,10,5)); add(play=new Button("play"));
add(loop=new Button("loop"));
add(stop=new Button("stop"));
this.au=au;
setBachground(new Color(160,255,160));
}
public boolean action(Event e,Object p)
{
if(e.target==play)
{au.play();
}
else if(e.target==loop)
{au.loop();}
else if(e.target==stop)
{au.stop();}
return super.action(e,p);
}
}
//Calendar是描述日历和时钟的类
class Calendar extends Panel implements Runnable {
Button nextYear,nextMonth,nextDate,lastYear,lastMonth,lastDate;
TextField year,month,date;
Film myFilm;
Thread clock;
AudioClip chirp;
Date today=new Date();
String weekName[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
String monthName[]={"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
int weekColor[]={0xcc6633,0xcccc33,0xcccc33,0xcccc33,0xcccc33,0xcccc33,0x3399cc};
Calendar(Image thumb,AudioClip au)
{
setLayout(new FlowLayout(FlowLayout.LEFT,3,13));
add(lastYear=new Button("<"));
add(year=new TextField(""+(1900+today.getYear()),4);
add(nextYear=new Button(">"));
add(lastMonth=new Button("<"));
add(month=new TextField(""+(today.getMonth()+1),2));
add(nextMonth=new Button(">"));
add(lastDate=new Button("<"));
add(date=new TextField(""+today.getDate(),2));
add(nextDate=new Button(">"));
myFilm=new Film(thumb);
myFilm.resize(270,15);
add(myFilm);
chirp=au;
}
public void run()
{
long startTime=System.currentTimeMillis();//记录线程开始的时间
while(true)
{
repaint();
startTime+=1000;//计算下次醒来的时间
try{
Thread.sleep(Math.max(0,startTime-System.currentTimeMillis()));
//保证绘图时间和睡眠时间相加为1s,若绘图超过1s,则线程不睡眠
}
catch(InterruptedException e) {}
}
}
public void paint(Graphics g)