歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java實現時鐘

Java實現時鐘

日期:2017/3/1 10:37:45   编辑:Linux編程

一、核心的表達式

因為需要動態顯示小時的指針、分鐘的指針、秒的指針的位置,所以確認三個指針的角度非常重要;

X:三個指針相交的原點的X坐標;

Y:三個指針相交的原點的Y坐標;

HOUR_LENGTH、MINUTE_LENGTH、SECOND_LENGTH表示時針、分針、秒針的長度;

hour、minute、second表示現在是幾時、幾分、幾秒;

hourLine.x2 = X+HOUR_LENGTH*Math.cos(hour*(Math.PI/6)-Math.PI/2);
hourLine.y2 = Y+HOUR_LENGTH*Math.sin(hour*(Math.PI/6)-Math.PI/2);
minLine.x2 = X+MINUTE_LENGTH*Math.cos(minute*(Math.PI/30)-Math.PI/2);
minLine.y2 = Y+MINUTE_LENGTH*Math.sin(minute*(Math.PI/30)-Math.PI/2);
secondLine.x2 = X+SECOND_LENGTH*Math.cos(second*(Math.PI/30)-Math.PI/2);
secondLine.y2 = Y+SECOND_LENGTH*Math.sin(second*(Math.PI/30)-Math.PI/2);

二、怎樣動態顯示

簡單的說就是每一秒更新一次屏幕,因此用Timer和TimerTask非常符合要求;每一秒刷新一次;

三、代碼

[java]
  1. package org.Demo00;
  2. import java.awt.Graphics;
  3. import java.awt.Graphics2D;
  4. import java.awt.geom.Ellipse2D;
  5. import java.awt.geom.Line2D;
  6. import java.util.Calendar;
  7. import java.util.GregorianCalendar;
  8. import java.util.Timer;
  9. import java.util.TimerTask;
  10. import javax.swing.JFrame;
  11. import javax.swing.JPanel;
  12. public class Test5 extends JFrame {
  13. MyPanel clockPanel;
  14. Ellipse2D.Double e;
  15. int x;
  16. int y;
  17. Line2D.Double hourLine;
  18. Line2D.Double minLine;
  19. Line2D.Double secondLine;
  20. GregorianCalendar calendar;
  21. int hour;
  22. int minute;
  23. int second;
  24. public static final int X = 60;
  25. public static final int Y = 60;
  26. public static final int X_BEGIN = 10;
  27. public static final int Y_BEGIN = 10;
  28. public static final int RADIAN = 50;
  29. public Test5() {
  30. setSize(300, 400);
  31. clockPanel = new MyPanel();
  32. add(clockPanel);
  33. Timer t = new Timer();
  34. Task task = new Task();
  35. t.schedule(task, 0, 1000);
  36. }
  37. public static void main(String[] args) {
  38. Test5 t = new Test5();
  39. t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  40. t.setVisible(true);
  41. }
  42. class MyPanel extends JPanel {
  43. public MyPanel() {
  44. e = new Ellipse2D.Double(X_BEGIN, Y_BEGIN, 100, 100);
  45. hourLine = new Line2D.Double(X, Y, X, Y);
  46. minLine = new Line2D.Double(X, Y, X, Y);
  47. secondLine = new Line2D.Double(X, Y, X, Y);
  48. }
  49. public void paintComponent(Graphics g) {
  50. super.paintComponent(g);
  51. Graphics2D g2 = (Graphics2D) g;
  52. g2.drawString("12", 55, 25);
  53. g2.drawString("6", 55, 105);
  54. g2.drawString("9", 15, 65);
  55. g2.drawString("3", 100, 65);
  56. g2.draw(e);
  57. g2.draw(hourLine);
  58. g2.draw(minLine);
  59. g2.draw(secondLine);
  60. }
  61. }
  62. class Task extends TimerTask {
  63. public void run() {
  64. calendar = new GregorianCalendar();
  65. hour = calendar.get(Calendar.HOUR);
  66. minute = calendar.get(Calendar.MINUTE);
  67. second = calendar.get(Calendar.SECOND);
  68. hourLine.x2 = X + 40 * Math.cos(hour * (Math.PI / 6) - Math.PI / 2);
  69. hourLine.y2 = Y + 40 * Math.sin(hour * (Math.PI / 6) - Math.PI / 2);
  70. minLine.x2 = X + 45
  71. * Math.cos(minute * (Math.PI / 30) - Math.PI / 2);
  72. minLine.y2 = Y + 45
  73. * Math.sin(minute * (Math.PI / 30) - Math.PI / 2);
  74. secondLine.x2 = X + 50
  75. * Math.cos(second * (Math.PI / 30) - Math.PI / 2);
  76. secondLine.y2 = Y + 50
  77. * Math.sin(second * (Math.PI / 30) - Math.PI / 2);
  78. repaint();
  79. }
  80. }
  81. }
Copyright © Linux教程網 All Rights Reserved