Java实现时钟


一、核心的表达式

因为需要动态显示小时的指针、分钟的指针、秒的指针的位置,所以确认三个指针的角度非常重要; 

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

相关内容