时钟日历设计JAVA实验报告_第1页
时钟日历设计JAVA实验报告_第2页
时钟日历设计JAVA实验报告_第3页
时钟日历设计JAVA实验报告_第4页
时钟日历设计JAVA实验报告_第5页
已阅读5页,还剩20页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

PAGEPAGE1佛山科学技术学院《可视化编程技术》课程设计报告时钟日历设计学生姓名:学号:年级专业:指导老师:学院:广东★佛山提交日期:2014年6月目录1.前言………………22.概要设计…………32.1开发环境……………………32.2时钟日历功能……………32.3界面设计……………………32.4类的框架结构图……………43.详细设计………………………53.1使用的java类的说明……………………53.2类的主要方法………………63.2.1设置3个按钮…………63.2.2日历、时钟、界面的绘制……………73.2.3导入素材文件…………103.2.4设计总体认识………114.运行结果………………………115.测试分析………………………115.1程序运行情况……………115.2程序异常处理……………126.源程序…………13参考文献……………23设计总结……………24PAGEPAGE24摘要:该程序是一个图形界面的简单的java时钟日历,具有良好的界面。该程序可以自动获取系统日期和时间。还可以翻看万年历,在点选日期的时候会有音效。程序中还有个会移动的图形条,在右侧则有一个时钟图形,会跟着实时时间动。下方三个按钮可以播放一个声音,PLAY则播放一次,LOOP则循环播放,STOP则停止播放声音。关键字:java、日历类、时钟类、声音类、方法、事件1前言随着科学技术的不断发展,计算机已经成为我们工作学习和生活中不可缺少的工具。文本编辑器是个人计算机最司空见惯的应用程序了,在学习了Java语言之后,我决定使用Java语言编写一个简单的计算器,可以实现简单的运算功能,满足日常基本的工作学习和娱乐需要。Java是由Sun公司开发的新一代纯面向对象的网络编程语言。其目标是建立一种在任意种机器、任一种操作系统的网络环境中运行的软件,实行所谓的“程序写一次,到处运行”的目标。正因为如此,Java已成为当今Internet上最流行、最受欢迎的一种程序开发语言。Java开发小组把Java按特性分为基本版、移动版、企业版,每个版本有一个软件开发包。Java基本版本叫Java2标准版(Java2StandardEdition,J2SE),它包含建立Java应用程序或者是Applet所需的应用程序编程接口(API)。Java2移动版(TheJava2MobileEdition,J2ME)包含创建无线Java应用程序的API。还有Java2企业版(TheJava2Enterprise,J2EE)是J2SE的增强版本,包含建立多层架构应用程序API。Java语言是由C++语言发展起而来的,是一种彻底的面向对象的程序设计语言。作为一种纯面向对象的程序设计语言,它非常适合大型软件的开发。Java语言去掉了C++语言的一些容易引起错误的特性。Java语言的特点有:面向对象、跨平台、安全性、多线程和图形功能强。2概要设计2.1开发环境 开发平台:MicrosoftWindows7旗舰版ServicePack1 开发工具:Eclipse+JDK1.6.02.2时钟日历功能功能1:显示实时日期

功能2:显示实时时间功能3:翻看万年历

功能4:单次播放声音

功能5:循环播放声音功能6:停止播放声音2.3界面设计图2.3时钟日历面板2.4类的框架结构//Film是描述滚动图片的类.classFilmextendsCanvasimplementsRunnable{ publicvoidrun(){} publicvoidpaint(Graphicsg){} publicvoidupdate(Graphicsg){}}//AudioPlayer是描述底部音乐播放条的类.classAudioPlayerextendsPanel{ publicbooleanaction(Evente,Objectp){}}//Calendar是描述日历和时钟的类.classCalendarextendsPanelimplementsRunnable{ publicvoidrun(){} publicvoidpaint(Graphicsg){} publicvoidupdate(Graphicsg){}publicbooleanaction(Eventevt,Objectarg){} publicbooleanmouseDown(Eventevt,intx,inty){} publicbooleanlostFocus(Eventevt,Objectwhat){}publicbooleankeyDown(Eventevt,intkey){}}//主类publicclassMyAppletextendsApplet{ publicvoidinit(){} publicvoidstart(){} publicvoidstop(){} publicvoiddestroy(){}}3详细设计3.1使用的Java类的说明类名:Film作用:描述滚动图片的

继承的父类:Canvas类

实现的接口:没有

classFilmextendsCanvasimplementsRunnable{ publicvoidrun(){} publicvoidpaint(Graphicsg){} publicvoidupdate(Graphicsg){} }类名:AudioPlayer

作用:描述底部音乐播放条的类.

继承的父类:Panel类

实现的接口:没有classAudioPlayerextendsPanel{ publicbooleanaction(Evente,Objectp){}}类名:Calendar

作用:描述日历和时钟的类.

继承的父类:Panel类

实现的接口:FilmclassCalendarextendsPanelimplementsRunnable{ publicvoidrun(){} publicvoidpaint(Graphicsg){} publicvoidupdate(Graphicsg){}publicbooleanaction(Eventevt,Objectarg){} publicbooleanmouseDown(Eventevt,intx,inty){} publicbooleanlostFocus(Eventevt,Objectwhat){}publicbooleankeyDown(Eventevt,intkey){}}类名:MyApplet

作用:主类.

继承的父类:Applet类

实现的接口:Calendar、AudioPlayerpublicclassMyAppletextendsApplet{ publicvoidinit(){} publicvoidstart(){} publicvoidstop(){} publicvoiddestroy(){}}3.2类的主要方法3.2.1设置3个按钮: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); } 3.2.2日历、时钟、界面的绘制:publicvoidupdate(Graphicsg){ inti,j; Dimensiond=size(); ImageoffImage=createImage(d.width,d.height); GraphicsoffG=offImage.getGraphics(); //setthebackgroundcolor setBackground(newColor(242,160,255)); offG.setColor(getBackground()); offG.fillRect(0,0,500,350); //drawcalendarshadow 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); } //drawauthorinformation offG.setColor(Color.gray); offG.drawString("12教育技术2班刘思华2012914205",10,350); //drawverticalbars 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); } //drawyear 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); //drawhorizontalbars 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); //drawcircles offG.setColor(Color.black); offG.drawOval(560,5,120,120); offG.setColor(Color.black); offG.drawOval(570,15,100,100); //drawMonth offG.setFont(newFont("Monospaced",Font.BOLD+Font.ITALIC,25)); offG.setColor(Color.orange); offG.drawString(monthName[today.getMonth()],280,57); //drawdate 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); //drawclock offG.setColor(Color.black); offG.drawOval(560,60,10,10); //9点 offG.drawOval(670,60,10,10); //3点 offG.drawOval(615,5,10,10); //12点 offG.drawOval(615,115,10,10); //6点 offG.drawOval(571,36,5,5); //10点 offG.drawOval(591,16,5,5); //11点 offG.drawOval(644,16,5,5); //1点 offG.drawOval(663,36,5,5); //2点 offG.drawOval(663,89,5,5); //4点 offG.drawOval(644,109,5,5); //5点 offG.drawOval(591,109,5,5); //7点 offG.drawOval(571,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(620,65,(int)(620+30*Math.sin(hour*Math.PI/6)),(int)(65-30*Math.cos(hour*Math.PI/6))); offG.drawLine(620,65,(int)(620+45*Math.sin(minute*Math.PI/30)),(int)(65-45*Math.cos(minute*Math.PI/30))); offG.setColor(Color.black); offG.drawLine(620,65,(int)(620+48*Math.sin(second*Math.PI/30)),(int)(65-48*Math.cos(second*Math.PI/30))); //drawcalendardates 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.black); //他月日期,浅色 offG.drawString(""+newDay.getDate(),20+i*48,129+j*34); newDay.setDate(newDay.getDate()+1); } } g.drawImage(offImage,0,0,this); }3.2.3导入素材文件: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); }3.2.4JAVA设计的摘要JAVA设计的目录结构JAVA设计的概要和详细设计JAVA设计的版权意识JAVA设计需要编译的环境JAVA设计的实用性4运行结果图4程序运行结果5测试分析5.1程序运行情况:

程序正常的响应按钮事件,输入到输入框的数据数值可能不是数字,导致程序异常,因此使用了try {}catch(NumberFormatExceptione){ } setDateText(); returntrue;将数据变回原来的数据值,防止程序异常。同时为了防止绘制界面时线程冲突,使用publicvoidrun(){longstartTime=System.currentTimeMillis(); //记录线程开始的时间. while(true){ repaint(); startTime+=1000; //计算下次醒来的时间. try{ Thread.sleep(Math.max(0,startTime-System.currentTimeMillis()));//保证绘图时间和睡眠时间相加为一秒,若绘图超过一秒,则线程不睡眠. }catch(InterruptedExceptione){}}5.2程序异常处理:

承自Panel类,然后使用try-catch去捕捉这个异常,异常的处理如下:

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;

6程序清单: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(242,160,255)); 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; setBackground(newColor(242,160,255)); } 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()));//保证绘图时间和睡眠时间相加为一秒,若绘图超过一秒,则线程不睡眠. }catch(InterruptedExceptione){}}} publicvoidpaint(Graphicsg){ update(g); } publicvoidupdate(Graphicsg){ inti,j; Dimensiond=size(); ImageoffImage=createImage(d.width,d.height); GraphicsoffG=offImage.getGraphics(); //setthebackgroundcolor setBackground(newColor(242,160,255)); offG.setColor(getBackground()); offG.fillRect(0,0,500,350); //drawcalendarshadow 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); } //drawauthorinformation offG.setColor(Color.gray); offG.drawString("12教育技术2班刘思华2012914205",10,350); //drawverticalbars 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); } //drawyear 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); //drawhorizontalbars 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); //drawcircles offG.setColor(Color.black); offG.drawOval(560,5,120,120); offG.setColor(Color.black); offG.drawOval(570,15,100,100); //drawMonth offG.setFont(newFont("Monospaced",Font.BOLD+Font.ITALIC,25)); offG.setColor(Color.orange); offG.drawString(monthName[today.getMonth()],280,57); //drawdate 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); //drawclock offG.setColor(Color.black); offG.drawOval(560,60,10,10); //9点 offG.drawOval(670,60,10,10); //3点 offG.drawOval(615,5,10,10); //12点 offG.drawOval(615,115,10,10); //6点 offG.drawOval(571,36,5,5); //10点 offG.drawOval(591,16,5,5); //11点 offG.drawOval(644,16,5,5); //1点 offG.drawOval(663,36,5,5); //2点 offG.drawOval(663,89,5,5); //4点 offG.drawOval(644,109,5,5); //5点 offG.drawOval(591,109,5,5); //7点 offG.drawOval(571,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(620,65,(int)(620+30*Math.sin(hour*Math.PI/6)),(int)(65-30*Math.cos(hour*Math.PI/6))); offG.drawLine(620,65,(int)(620+45*Math.sin(minute*Math.PI/30)),(int)(65-45*Math.cos(minute*Math.PI/30))); offG.setColor(Color.black); offG.drawLine(620,65,(int)(620+48*Math.sin(second*Math.PI/30)),(int)(65-48*Math.cos(second*Math.PI/30))); //drawcalendardates 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.black); //他月日期,浅色 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

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论