




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、时钟的界面良好,简洁实用性强,实现程序与电脑的时间的同步。可以显示始终,也可以显示分针秒针,并可以在相应的位置调整时间。而且初始运行会与电脑的时间校对,一般默认为同步,但还可以自己再次调节,提高实用性。本系统共包括 1 个 java 源文件。1、Clock 源文件是本程序的主函数其作用是初始化棋盘。2、 setCurrentTime 源文件实现电脑设置时间。3、 paintHourPointer 源文件为时针.4、 paintSecondPointer 源文件实现人与电脑设置秒针.5、 paintMinuteDot 源文件人与电脑设置分针. Clock 成员变量 : 成员变量描述 变量类型 名
2、称 时针 String Hour 分针 String Minute 秒针 String Second 时间点 TextField text_1 Clock 方法 : 方法名 功能 备注 setCurrentTime 设置当前时间 构造方法 paintHourPointer 设置时针 接口方法 paintSecondPointer 设置秒针 接口方法 paintMinuteDot 设置分针 接口方法 actionPerformed 事件处理 run 程序运行 详细设计如下import java.awt.*; import java.awt.geom.Ellipse2D; import java.
3、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 priva
4、te 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
5、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 p
6、rotected 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.
7、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(
8、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);
9、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.setC
10、olor(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
11、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
12、, 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,
13、 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
14、* 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 = ne
15、w 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 = Mat
16、h.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
17、.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.set
18、Visible(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
19、- 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.drawL
20、ine(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 <
21、; 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) + "
22、:" + 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;/
23、小时,分钟,秒 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.MI
24、NUTE) / 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);
25、/定义 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("北京时间", "
26、;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(int
27、erval); 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 GregorianCal
28、endar(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
29、 * 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=MyTHour*60*60+MyTMinute*60+MyTSecond; 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() setCurren
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 观影课件教学课件
- 中国岩盐项目商业计划书
- 过敏性鼻炎诊疗规范学习手册
- 骨折术后功能锻炼康复方案
- 耳鼻喉科鼻窦炎护理操作规范
- 中国防锈涂料项目创业计划书
- 中国烯甲炔诺酮项目创业计划书
- 2025年中国内墙乳胶漆项目创业计划书
- 中国汽车塑料件用水性涂料项目商业计划书
- (正式版)DB41∕T 2898-2025 《医学影像数据人工智能分析方法评估指南》
- 公安辅警考试题库
- 高中通用技术《结构与设计》练习题(附答案解析)
- GB/T 8918-2006重要用途钢丝绳
- GB/T 6620-2009硅片翘曲度非接触式测试方法
- 注塑行业ISO9001体系品质检验控制程序
- 企业财务管理咨询
- Unit 2 Lesson 3 Running and Fitness课件-高中英语北师大版必修第一册
- 《工程伦理学》工程中的诚信与道德问题 课件
- 电影与女性主义(电影理论课程课件)
- 家庭照护员理论考试备考题库(含答案)
- 幼儿园大班综合《我们和手机》课件
评论
0/150
提交评论