Java Implementation Clock __java

Source: Internet
Author: User

One, the core of the expression

It is important to confirm the angle of the three pointers because you need to animate the pointer to the hour, the minute pointer, and the pointer position of the second.

X: The x-coordinate of the origin at which three pointers intersect;

Y: the y-coordinate of the origin at which three pointers intersect;

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

Hour, minute, second say now is the time, a few minutes, a few 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);

Second, how to dynamically display

The simple thing is to update the screen every second, so the timer and the timertask are very much in line with the requirements; refresh once every second;

Third, the 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 + * Math.Cos (Hour * (MATH.PI/6)-MATH.PI/2);
			Hourline.y2 = Y + * Math.sin (Hour * (MATH.PI/6)-MATH.PI/2); MINLINE.X2 = X +* Math.Cos (Minute * (MATH.PI/30)-MATH.PI/2);
			Minline.y2 = Y + * Math.sin (minute * (MATH.PI/30)-MATH.PI/2);
			secondline.x2 = X + M Math.Cos (second * (MATH.PI/30)-MATH.PI/2);
			Secondline.y2 = Y + M 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.