当前位置:文档之家› JAVA课程设计 时钟

JAVA课程设计 时钟

辽宁工业大学JA V A程序设计课程设计(论文)题目:时钟院(系):软件学院专业班级:软件技术班学号:学生姓名:指导教师:***教师职称:助教起止时间: 2009.12.1至2009.12.16程序设计专题(报告)任务及评语目录第1章课程设计的目的与要求 (1)1.1 课程设计目的 (1)1.2 课程设计的实验环境 (1)1.3 课程设计的预备知识 (1)1.4 课程设计要求 (1)第2章课程设计内容 (2)2.1课程设计主要内容 (2)2.2概要设计 (2)2.2.1自定义类说明 (2)2.3详细设计 (3)2.4测试分析 (16)2.4.1程序运行情况 (16)2.4.2程序异常处理 (16)第3章课程设计总结 (17)参考资料18第1章课程设计的目的与要求1.1 课程设计目的《JA V A程序设计》是计算机相关专业的必修专业基础课程,其实践性、应用性很强。

实践教学环节是必不可少的一个重要环节。

本课程的程序设计专题实际是计算机相关专业学生学习完《JAVA程序设计》课程后,进行的一次全面的综合训练,JA V A程序设计的设计目的是加深对理论教学内容的理解和掌握,使学生较系统地掌握程序设计及其在网络开发中的广泛应用,基本方法及技巧,为学生综合运用所学知识,利用软件工程为基础进行软件开发、并在实践应用方面打下一定基础。

1.2 课程设计的实验环境硬件要求能运行Windows 9.X操作系统的微机系统。

JAVA程序设计语言及相应的集成开发环境,J2SDK和ECLIPSE开发工具。

1.3 课程设计的预备知识熟悉JAVA语言及ECLIPSE开发工具。

1.4 课程设计要求按课程设计指导书提供的课题,要求学生在自行完成各个操作环节,并能实现且达到举一反三的目的,完成一个项目解决一类问题。

要求学生能够全面、深入理解和熟练掌握所学内容,并能够用其分析、设计和解答类似问题;对此能够较好地理解和掌握,能够进行简单分析和判断;能编写出具有良好风格的程序;掌握JA V A程序设计的基本技能和面向对象的概念和方法;了解多线程、安全和网络等编程技术。

同时培养学生进行分析问题、解决问题的能力;培养学生进行设计分析、设计方法、设计操作与测试、设计过程的观察、理解和归纳能力的提高。

第2章课程设计内容2.1课程设计主要内容我设计的时钟有的界面良好,比较简洁美观,程序有很强的实用性,实现程序与电脑的时间的同步。

可以显示时钟,也可以显示分针秒针,并可以在相应位置调整时间。

而且初始运行会自动与电脑的时间校对,一般默认为同步,但还可以自己再次调节,提高了实用性。

本系统共包括1个java源文件。

1、Clock源文件是本程序的主函数其作用是初始化棋盘。

2、setCurrentTime源文件实现电脑设置时间。

3、paintHourPointer源文件为时针.4、paintSecondPointer源文件实现人与电脑设置秒针.5、paintMinuteDot源文件人与电脑设置分针.2.2概要设计2.2.1自定义类说明*********************************************************** 类名: Clock ** 作用: 自定义主类,对鼠标拖拽的初始界面进行声明** 继承的父类: JComponent类** 实现的接口: 没有***********************************************************表1-成员变量表表2-方法表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.V ALUE_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, pos[0], pos[1], 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(pos[0], pos[1], pos1[0], pos1[1]), true);double[] pos2 = calcPos(r3, angle + 180);gp.lineTo((float)pos2[0], (float)pos2[1]);double[] pos3 = calcPos(r2, angle + 270);gp.lineTo((float)pos3[0], (float)pos3[1]);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(pos1[0], pos1[1], pos[0], pos[1]);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) {}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 //clockcanvasimplements 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 MyTimerimplements 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程序运行情况当程序正常运行的时候,它能清晰的显示时钟界面。

相关主题