欢迎来到三一办公! | 帮助中心 三一办公31ppt.com(应用文档模板下载平台)
三一办公
全部分类
  • 办公文档>
  • PPT模板>
  • 建筑/施工/环境>
  • 毕业设计>
  • 工程图纸>
  • 教育教学>
  • 素材源码>
  • 生活休闲>
  • 临时分类>
  • ImageVerifierCode 换一换
    首页 三一办公 > 资源分类 > DOC文档下载
     

    JAVA课程设计 时钟的设计实现.doc

    • 资源ID:2387002       资源大小:99.50KB        全文页数:21页
    • 资源格式: DOC        下载积分:8金币
    快捷下载 游客一键下载
    会员登录下载
    三方登录下载: 微信开放平台登录 QQ登录  
    下载资源需要8金币
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    JAVA课程设计 时钟的设计实现.doc

    辽 宁 工 业 大 学JAVA程序设计 课程设计(论文)题目: 时钟 院(系): 软件学院 专业班级: 软件技术091班 学 号: 888888888 学生姓名: tianxi 指导教师: tianxi 教师职称: 助 教 起止时间: 2010.12.1至2010.12.16 程序设计专题(报告)任务及评语院(系):软件学院 教研室:软件教研室学 号8888888学生姓名tianxi专业班级软件技术091程序设计(报告)题目时钟程序设计(报告)任务程序设计专题的任务与要求:(1)掌握Java编程、面向对象的基础知识。(2)较熟练地编写Java应用程序Application。(3)了解Java的常用标准类库、编程技巧、异常处理。(5)联系已学过的内容,巩固所学的理论,增强独立工作能力。(6)通过设计主要使学生有一个独立编写程序的过程,对理论学习及动手能力都有一个很大的提高。(7)通过本次设计,进一步培养学生热爱专业的思想,同时对本专业综合素质的提高起一个积极的推动作用。课程设计过程中,要严格遵守实践环节的时间安排,听从指导教师的指导。正确地完成上述内容,记录实习日记,规范完整地撰写出课程设计报告。指导教师评语及成绩成绩: 指导教师签字: 2009 年 12 月 20 日目 录第1章 课程设计的目的与要求11.1 课程设计目的11.2 课程设计的实验环境11.3 课程设计的预备知识11.4 课程设计要求1第2章 课程设计内容22.1课程设计主要内容22.2概要设计22.2.1自定义类说明22.3详细设计32.4测试分析162.4.1程序运行情况162.4.2程序异常处理16第3章 课程设计总结17参考资料18第1章 课程设计的目的与要求1.1 课程设计目的JAVA程序设计是计算机相关专业的必修专业基础课程,其实践性、应用性很强。实践教学环节是必不可少的一个重要环节。本课程的程序设计专题实际是计算机相关专业学生学习完JAVA程序设计课程后,进行的一次全面的综合训练,JAVA程序设计的设计目的是加深对理论教学内容的理解和掌握,使学生较系统地掌握程序设计及其在网络开发中的广泛应用,基本方法及技巧,为学生综合运用所学知识,利用软件工程为基础进行软件开发、并在实践应用方面打下一定基础。1.2 课程设计的实验环境硬件要求能运行Windows 9.X操作系统的微机系统。JAVA程序设计语言及相应的集成开发环境,J2SDK和ECLIPSE开发工具。1.3 课程设计的预备知识熟悉JAVA语言及ECLIPSE开发工具。1.4 课程设计要求按课程设计指导书提供的课题,要求学生在自行完成各个操作环节,并能实现且达到举一反三的目的,完成一个项目解决一类问题。要求学生能够全面、深入理解和熟练掌握所学内容,并能够用其分析、设计和解答类似问题;对此能够较好地理解和掌握,能够进行简单分析和判断;能编写出具有良好风格的程序;掌握JAVA程序设计的基本技能和面向对象的概念和方法;了解多线程、安全和网络等编程技术。同时培养学生进行分析问题、解决问题的能力;培养学生进行设计分析、设计方法、设计操作与测试、设计过程的观察、理解和归纳能力的提高。第2章 课程设计内容2.1课程设计主要内容我设计的时钟有的界面良好,比较简洁美观,程序有很强的实用性,实现程序与电脑的时间的同步。可以显示时钟,也可以显示分针秒针,并可以在相应位置调整时间。而且初始运行会自动与电脑的时间校对,一般默认为同步,但还可以自己再次调节,提高了实用性。本系统共包括1个java源文件。1、Clock源文件是本程序的主函数其作用是初始化棋盘。2、setCurrentTime源文件实现电脑设置时间。3、paintHourPointer源文件为时针.4、paintSecondPointer源文件实现人与电脑设置秒针.5、paintMinuteDot源文件人与电脑设置分针.2.2概要设计2.2.1自定义类说明* * 类名: Clock * * 作用: 自定义主类,对鼠标拖拽的初始界面进行声明* * 继承的父类: JComponent类 * * 实现的接口: 没有 * *表1-成员变量表表2.1 Clock成员变量成员变量描述变量类型名称时针StringHour分针StringMinute秒针StringSecond时间点TextFieldtext_1表2-方法表表2 Clock方法方法名功能备注setCurrentTime设置当前时间构造方法paintHourPointer设置时针 接口方法paintSecondPointer设置秒针 接口方法paintMinuteDot设置分针 接口方法actionPerformed事件处理 run程序运行 2.3详细设计import java.awt.*; import java.awt.geom.Ellipse2D; import java.awt.geom.GeneralPath; import java.awt.geom.Line2D; import java.awt.geom.Rectangle2D; import java.util.Calendar; import java.util.Date; import javax.swing.BorderFactory; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.UIManager; public class Clock extends JComponent private static final Color INTEGRAL_COLOR = new Color(0, 128, 128); private int radius; private Calendar currentTime = Calendar.getInstance(); private double s = 0.03; public Clock(int radius) this.radius = radius; public void setCurrentTime(Date time) /设置当前时间 this.currentTime.setTime(time); public void setCurrentTime(long millis) this.currentTime.setTimeInMillis(millis); public Dimension getPreferredSize() Insets insets = getInsets(); int r = (int) (radius = -1 ? 0 : radius*(1+s)+1; return new Dimension(r * 2 + insets.left + insets.right,r * 2 + insets.top + insets.bottom); /返回一个指定宽、高的Dimension protected void paintComponent(Graphics g) super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Insets insets = getInsets(); int wid = getWidth() - insets.left - insets.right; int hei = getHeight() - insets.top - insets.bottom; int r = (int) (Math.min(wid, hei) / 2 / (1+s); g2d.translate(insets.left + r * (1+s), insets.top + r * (1+s); g2d.scale(1, -1); for (int i = 0; i < 60; i+) int angle = 90 - i * 6; double pos = calcPos(r, angle); paintMinuteDot(r, g2d, pos0, pos1, i % 5 = 0); paintHourPointer(r, g2d); paintMinutePointer(r, g2d); paintSecondPointer(r, g2d); paintCenterPoint(g2d); g2d.scale(1, -1); g2d.translate(-insets.left - r * (1+s), -insets.top - r * (1+s); private void paintCenterPoint(Graphics2D g2d) g2d.setColor(Color.BLUE); Rectangle2D rect = new Rectangle2D.Double(-2, -2, 4, 4); g2d.fill(rect); private void paintMinutePointer(int r, Graphics2D g2d) int minute = currentTime.get(Calendar.MINUTE); int second = currentTime.get(Calendar.SECOND); double angle = 90 - (minute + second / 60.0) * 6; Shape pointerShape = createPointerShape(r * 0.8, r * 0.04, r * 0.08, angle); g2d.setColor(Color.LIGHT_GRAY); g2d.fill(pointerShape); g2d.setColor(Color.DARK_GRAY); g2d.draw(pointerShape); private void paintHourPointer(int r, Graphics2D g2d) int hour = currentTime.get(Calendar.HOUR); int minute = currentTime.get(Calendar.MINUTE); int second = currentTime.get(Calendar.SECOND); double angle = 90 - (hour + minute / 60.0 + second / 3600.0) * 30; Shape pointerShape = createPointerShape(r * 0.6, r * 0.06, r * 0.1, angle); g2d.setColor(Color.LIGHT_GRAY); g2d.fill(pointerShape); g2d.setColor(Color.DARK_GRAY); g2d.draw(pointerShape); private Shape createPointerShape(double r1, double r2, double r3, double angle) GeneralPath gp = new GeneralPath(); double pos = calcPos(r1, angle); double pos1 = calcPos(r2, angle + 90); gp.append(new Line2D.Double(pos0, pos1, pos10, pos11), true); double pos2 = calcPos(r3, angle + 180); gp.lineTo(float)pos20, (float)pos21); double pos3 = calcPos(r2, angle + 270); gp.lineTo(float)pos30, (float)pos31); gp.closePath(); return gp; private void paintSecondPointer(int r, Graphics2D g2d) g2d.setColor(Color.BLACK); int second = currentTime.get(Calendar.SECOND); int angle = 90 - second * 6; double pos = calcPos(r * 0.9, angle); double pos1 = calcPos(r * 0.2, angle + 180); Line2D line = new Line2D.Double(pos10, pos11, pos0, pos1); g2d.draw(line); private void paintMinuteDot(int r, Graphics2D g2d, double x, double y, boolean flag) g2d.setColor(flag ? Color.RED : Color.BLACK); if (flag) /Rectangle2D rect = new Rectangle2D.Double( Ellipse2D rect = new Ellipse2D.Double( x - r * s, y - r * s, r * s * 2, r * s * 2); g2d.fill(rect); else /Rectangle2D rect = new Rectangle2D.Double( Ellipse2D rect = new Ellipse2D.Double( x - r * 0.02, y - r * 0.02, r * 0.04, r * 0.04); g2d.fill(rect); private double calcPos(double r, double angle) double radian = Math.toRadians(angle); double x = r * Math.cos(radian); double y = r * Math.sin(radian); return new double x, y; public static void main(String args) try UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName(); catch (Exception e) e.printStackTrace(); final Clock clock = new Clock(50); clock.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10); JFrame f = new JFrame( "软件081班 071404011 孙庆贺 "); /f.setBounds(380,200,500,600); f.getContentPane().add(clock, BorderLayout.CENTER); f.pack(); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); new Thread() public void run() while (true) try Thread.sleep(1000); catch (InterruptedException ex) ex.printStackTrace(); clock.setCurrentTime(System.currentTimeMillis(); clock.repaint(); .start(); int y1 = (int)(r - 10) * Math.cos(rad * s);g.drawLine(x + r, y + r, x + r + x1, y + r - y1);/分针g.setColor(Color.BLUE);x1 = (int)(r - r / 2.5) * Math.sin(rad * m);y1 = (int)(r - r / 2.5) * Math.cos(rad * m);g.drawLine(x + r, y + r, x + r + x1, y + r - y1);/时针g.setColor(Color.CYAN);x1 = (int)(r - r / 1.5) * Math.sin(rad * h);y1 = (int)(r - r / 1.5) * Math.cos(rad * h);g.drawLine(x + r, y + r, x + r + x1, y + r - y1);/数字g.setColor(Color.YELLOW);int d = 29;for (int i = 1; i <= 12; i+) x1 = (int)(r - 10) * Math.sin(rad * d);y1 = (int)(r - 10) * Math.cos(rad * d);g.drawString(i + "", x + r + x1 - 4, x + r - y1 + 5);d+=30;/小点d = 0;for (int i = 0; i < 60; i+) x1 = (int)(r - 2) * Math.sin(rad * d);y1 = (int)(r - 2) * Math.cos(rad * d);g.drawString(".", x + r + x1 - 1, x + r - y1 + 1);d+=6;/显示时间Calendar now1 = new GregorianCalendar();g.drawString(now1.get(Calendar.HOUR_OF_DAY) + ":" + now1.get(Calendar.MINUTE) + ":" + now1.get(Calendar.SECOND), 0, 10);public void run() while (true) try Thread.sleep(1000);catch (Exception ex) s+=6;if (s >= 360) s = 0;m+=6;if (m = 72 | m = 144 | m = 216 | m = 288) h+=6;if (m >= 360) m = 0;h+=6;if (h >=360) h = 0;this.repaint();int x, y, r;int h, m, s;/小时,分钟,秒double rad = Math.PI / 180;public ClockPaint(int x, int y, int r) this.x = x;this.y = y;this.r = r;Calendar now = new GregorianCalendar();s = now.get(Calendar.SECOND) * 6;/获得秒转换成度数m = now.get(Calendar.MINUTE) * 6;/获得分钟h = (now.get(Calendar.HOUR_OF_DAY) - 12) * 30 + now.get(Calendar.MINUTE) / 12 * 6;/获得小时Thread t = new Thread(this);t.start();public void paint(Graphics g) /清屏super.paint(g);g.setColor(Color.BLACK);g.fillRect(0, 0, r * 3, r * 3);/画圆g.setColor(Color.WHITE);g.drawOval(x, y, r * 2, r * 2);/秒针g.setColor(Color.RED);int x1 = (int)(r - 10) * Math.sin(rad * s);/定义MyTimer类 class MyTimer1 extends JFrame static int count=0; /判断是否重定义了时间 /构造函数 public MyTimer1() /定义窗口大小 setSize(320, 200); /定义窗口标题 setTitle("测试自定义时钟类!"); Container c = getContentPane(); / new ClockCanvas("北京时间", "GMT+8") c.add(new ClockCanvas("北京时间", "GMT+8"); /定义接口 interface TimerListener1 void timeElapsed(Timer1 t); class Timer1 extends Thread /类Timer1 private TimerListener1 target; private int interval; public Timer1(int i, TimerListener1 t) target = t; interval = i; setDaemon(true); public void run() try while (!interrupted() sleep(interval); target.timeElapsed(this); catch(InterruptedException e) class ClockCanvas extends JPanel /clockcanvas implements TimerListener1 static int seconds = 0; private String city; private GregorianCalendar calendar; /构造函数 public ClockCanvas(String c, String tz) city = c; calendar = new GregorianCalendar(TimeZone.getTimeZone(tz); Timer1 t = new Timer1(1000, this); t.start(); setSize(180, 180); /绘制钟面 public void paintComponent(Graphics g) super.paintComponent(g); g.drawOval(100, 5, 120, 120); g.drawOval(101, 6, 118, 118); /分离时间 double hourAngle = 2 * Math.PI * (seconds - 3 * 60 * 60) / (12 * 60 * 60); double minuteAngle = 2 * Math.PI * (seconds - 15 * 60) / (60 * 60); double secondAngle = 2 * Math.PI * (seconds - 15) / 60; public void timeElapsed(Timer1 t) calendar.setTime(new Date(); if(MyTimer1.count=1) int a=1; seconds=MyTimer.intHour*60*60+MyTimer.intMinute*60+MyTimer.intSecond; seconds+=a;/a为秒自加 repaint(); else seconds = calendar.get(Calendar.HOUR) * 60 * 60 + calendar.get(Calendar.MINUTE) * 60 + calendar.get(Calendar.SECOND); repaint(); /定义时钟类 class MyTimer implements TimerListener /定义时钟类的属性 static int intHour,intMinute,intSecond; /构造函数 public MyTimer() setCurrentTimeAsSystemTime(); Timer t = new Timer(1000, this); /实例Timer类,里面有run方法 t.start(); /显示当前时间 public void displayCurrentTime() JOptionPane.showMessageDialog(null,intHour+":"+intMinute+":"+intSecond); /设定当前时间 public void setCurrentTime() /从对话框输入时,分秒 String strTemp=JOptionPane.showInputDialog(null,"请输入当前小时(24小时制):"); int iHour=Integer.parseInt(strTemp); strTemp=JOptionPane.showInputDialog(null,"请输入当前分:"); int iMinute=Integer.parseInt(strTemp); strTemp=JOptionPane.showInputDialog(null,"请输入当前秒:"); int iSecond=Integer.parseInt(strTemp); /设定当前时间为对话框输入的时间 if(iHour>=0&&iHour<24) intHour=iHour; transform(angle);g.setcolor(color);/设定当前时间为系统时间,构造函数调用 public void setCurrentTimeAsSystemTime() /定义Date类的一个对象,用来获取系统时间 Date timeCurrent=new Date(); catch(InterruptedException e) 2.4测试分析2.4.1程序运行情况当程序正常运行的时候,它能清晰的显示时钟界面。默认为与系统时间同步,如果出现错误,就会进行归零。运行界面如图2.1:图2.1运行界面2.4.2程序异常处理当线程在活动之前或活动期间处于正在等待、休眠或占用状态且该线程被中断时,抛出该异常。有时候,一种方法可能希望测试当前线程是否已被中断,如果已被中断,则立即抛出此异常。利用trycatch来捕获出现的异常。try Thread.sleep(1000); catch (InterruptedException ex) System.out.println(“抛出异常”)第3章 课程设计总结本次Java课程设计我的题目是时钟,题目不算难,但我做的很艰苦。主要就是动手能力太差,理论知识掌握的不全面,自以为会不少,但真做起来,却不知道从哪下手了。又回去温习理论知识,请教老师,上网查资料,忙的不亦乐乎。不过经过努力总算是做出来了,还通过此次课程设计,将我本学期所学的JAVA知识得到巩固和应用。在设计的过程中我遇到的很多问题,在老师的帮助和自己的思考下还是很好的完成了。这此课程设计我懂得了写程序不能脱离实际,只凭主观办事,要努力拓宽知识面,拓展思维。事情就是如此,努力代表收获。通过这次课程设计,我才真正领悟到“艰苦奋斗”这一词的真正含义,我才感受到早期电子设计者为现代的社会付出。设计确实有些辛苦,但苦中也有乐,在如今单一的理论学习中,很少有机会能有实践的机会,但现在可以,这些天的设计中得到的东西却不比理论课上得到的少多少,我感觉我和Java的距离更加近了;我想说,确实很累,但当我看到自己所做的成果时,心中也不免产生兴奋; 学习有成果了么。对Java编程有了一个基本的了解。这些天温习的内容越多,敲得代码越多,自己的学习积极性也越在提高,感觉也越充实。越来越发现现在学习的只是Java的基础,但涉及的面还是很广泛的,这些都为以后进一步学习Java的高级特性打下了扎实的基础。随着课程难度的慢慢加强,今后遇到的困难也必将会越来越多,但我相信自己一定能做好这些的。在本次Java课程设计中,我要非常感谢我的指导老师tian老师,不但在课堂上教给了我很多的Java的编程知tian,编程思想,而且在这次课程设计中更教会了我动手的能力,真正的编出了自己的东西,还要感谢我的同学,他们在我遇到困难时也给了我很多帮助。参考资料1. 季民 J2EE案例开发 中国水利水电出版 2003-08 2. 美Joe Walnes Java开放源码编程 电子工业出版 2004-073郝玉龙 J2EE编程技术 北方交大出版 2003-024. 朱喜福 Java程序设计 人民邮电出版 2003-045. 王路群 Java高级程序设计 中国水利水电出版社 2009-86. 王碧波 JBuilder 10入门与提高 清华大学出版 2004-117. 吴其庆 Java程序设计实例教程 冶金工业出版社 2007-118. 柳西玲 Java语言应用开发基础 清华大学出版社 2008-29. 朱战立 Java程序设计实用教程 人民

    注意事项

    本文(JAVA课程设计 时钟的设计实现.doc)为本站会员(文库蛋蛋多)主动上传,三一办公仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知三一办公(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    备案号:宁ICP备20000045号-2

    经营许可证:宁B2-20210002

    宁公网安备 64010402000987号

    三一办公
    收起
    展开