1、用java写的音乐时钟万年历import java.awt.*;import java.util.*;import java.awt.image.*;import java.applet.*;/Film是描述滚动图片的类class Film extends Canvas implements RunnableThread move;Image thumb;int startPoint=0;/起始位置final int thumbWidth=23;/小图片的宽度 public Film(Image thumb) this.thumb=thumb; if(move=null) move=new T
2、hread(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
3、(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 PanelAudioClip au
4、;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(
5、); 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; D
6、ate 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=ne
7、w Button(); add(lastMonth=new Button(); add(lastDate=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
8、.currentTimeMillis(); /保证绘图时间和睡眠时间相加为1s,若绘图超过1s,则线程不睡眠 catch(InterruptedException e) public void paint(Graphics g)update(g);public void update(Graphics g)int i,j;Dimension d=size();Image offImage=createImage(d.width,d.height);Graphics offG=offImage.getGraphics();/设置背景色setBackground(new Color(160,255
9、,160);offG.setColor(getBackground();offG.fillRect(0,0,500,350);/画日历背景offG.setColor(new Color(80,150,80);offG.fillRect(25,100,340,230);/draw calendar baseoffG.setColor(new Color(100,230,195);offG.fillRect(15,90,340,230);offG.setFont(new Font(Helvetica,Font.BOLD,20);for(i=0;i0;i-=10)offG.setColor(new
10、Color(170+85*i/350,255,170+85*i/350);offG.fillRect(380,i,40,9);/画年offG.setColor(new Color(200,255,200);offG.setFont(new Font(Courier,Font.ITALIC,66);String yearString=+(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.setCo
11、lor(Color.black);offG.drawLine(6,60,250,60);offG.setColor(new Color(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(new Font(Monospaced,Font.BOLD+F
12、ont.ITALIC,25);offG.setColor(Color.orange);offG.drawString(monthNametoday.getMonth(),280,57);/画日期Font myFont=new Font(DialogInput,Font.BOLD,50);FontMetrics fontSize=getFontMetrics(myFont);offG.setFont(myFont);String temp=+today.getDate();Color dateColor=new Color(weekColortoday.getDay();int x=380-fo
13、ntSize.stringWidth(temp)/2;int y=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,1
14、0,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,
15、5,5);/8点Date now=new Date();double hour=now.getHours()+now.getMinutes()/60;double minute=now.getMinutes()+now.getSeconds()/60;double second=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
16、,(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);/绘日期boolean raised;offG.setFont(new Font(Serif,Font.PLAIN,15);Date newDay=new Date(today.g
17、etYear(),today.getMonth(),1);newDay.setDate(1-newDay.getDay();/找出日历左上角的日期for(j=0;j=5;j+) for(i=0;i16&x113&y=1000&num=1&num=12) today.setMonth(num-1); repaint(); if(evt.target.equals(date) num=Integer.valueOf(date.getText().intValue(); if(num!=today.getDate() Date newDay=new Date(today.getTime(); new
18、Day.setDate(num); if(newDay.getMonth()=today.getMonth() today.setDate(num); repaint(); catch(NumberFormatException e)/输入的不是数字setDateText();输入不合法,将日期设为原值return true;public boolean keyDown(Event evt,int key)if(key=10) return(lostFocus(evt,evt.arg);/按了回车键else return (super.keyDown(evt,key);void setDate
19、Text()year.setText(+(1900+today.getYear();month.setText(+(today.getMonth()+1);date.setText(+today.getDate();public class MyApplet extends AppletCalendar calendar;AudioPlayer audioPlayer;public void init()String auname=getParameter(auname);if(auname=null) auname=ding.au;AudioClip au=getAudioClip(getC
20、odeBase(),auname);audioPlayer=new AudioPlayer(au);Image thumb=getImage(getCodeBase(),thumb.gif);au=getAudioClip(getCodeBase(),chirp.au);calendar=new Calendar(thumb,au);setLayout(new BorderLayout();add(Center,calendar);add(South,audioPlayer);public void start() if(calendar.myFilm.move=null) calendar.
21、myFilm.move=new Thead(calendar.myFilm); calendar.myFilm.move.start(); if(calendar.clock=null) calendar.clock=new Thread(calendar); calendar.clock.start(); public void stop() calendar.myFilm.move.stop(); calendar.myFilm.move=null; calendar.clock.stop(0; calendar.clock=null;public void destroy()audioPlayer.au.stop();super.destroy();
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1