Java clock implementation

Source: Internet
Author: User

I. Core expressions

Because the hour pointer, minute pointer, and second pointer location need to be dynamically displayed, it is very important to confirm the angle of the three pointers;

X: The X coordinate of the origin where the three pointers intersect;

Y: Y coordinate of the origin where the three pointers intersect;

Hour_length, minute_length, and second_length indicate the length of the hour, minute, and second hands;

Hour, minute, and second indicate the current time, minutes, and seconds;

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 );

Ii. How to dynamically display

In short, the screen is updated every second. Therefore, timer and timertask are used to meet the requirements and are refreshed every second;

Iii. Code

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();}}}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.