Java實現時鐘

來源:互聯網
上載者:User

一、核心的運算式

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

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非常符合要求;每一秒重新整理一次;

三、代碼

package org.Demo00;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.geom.Ellipse2D;import java.awt.geom.Line2D;import java.util.Calendar;import java.util.GregorianCalendar;import java.util.Timer;import java.util.TimerTask;import javax.swing.JFrame;import javax.swing.JPanel;public class Test5 extends JFrame {MyPanel clockPanel;Ellipse2D.Double e;int x;int y;Line2D.Double hourLine;Line2D.Double minLine;Line2D.Double secondLine;GregorianCalendar calendar;int hour;int minute;int second;public static final int X = 60;public static final int Y = 60;public static final int X_BEGIN = 10;public static final int Y_BEGIN = 10;public static final int RADIAN = 50;public Test5() {setSize(300, 400);clockPanel = new MyPanel();add(clockPanel);Timer t = new Timer();Task task = new Task();t.schedule(task, 0, 1000);}public static void main(String[] args) {Test5 t = new Test5();t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);t.setVisible(true);}class MyPanel extends JPanel {public MyPanel() {e = new Ellipse2D.Double(X_BEGIN, Y_BEGIN, 100, 100);hourLine = new Line2D.Double(X, Y, X, Y);minLine = new Line2D.Double(X, Y, X, Y);secondLine = new Line2D.Double(X, Y, X, Y);}public void paintComponent(Graphics g) {super.paintComponent(g);Graphics2D g2 = (Graphics2D) g;g2.drawString("12", 55, 25);g2.drawString("6", 55, 105);g2.drawString("9", 15, 65);g2.drawString("3", 100, 65);g2.draw(e);g2.draw(hourLine);g2.draw(minLine);g2.draw(secondLine);}}class Task extends TimerTask {public void run() {calendar = new GregorianCalendar();hour = calendar.get(Calendar.HOUR);minute = calendar.get(Calendar.MINUTE);second = calendar.get(Calendar.SECOND);hourLine.x2 = X + 40 * Math.cos(hour * (Math.PI / 6) - Math.PI / 2);hourLine.y2 = Y + 40 * Math.sin(hour * (Math.PI / 6) - Math.PI / 2);minLine.x2 = X + 45* Math.cos(minute * (Math.PI / 30) - Math.PI / 2);minLine.y2 = Y + 45* Math.sin(minute * (Math.PI / 30) - Math.PI / 2);secondLine.x2 = X + 50* Math.cos(second * (Math.PI / 30) - Math.PI / 2);secondLine.y2 = Y + 50* Math.sin(second * (Math.PI / 30) - Math.PI / 2);repaint();}}}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.