1+ package exercise_30_07 ;
2+
3+ import java .util .Calendar ;
4+ import java .util .GregorianCalendar ;
5+ import javafx .application .Platform ;
6+ import javafx .scene .layout .Pane ;
7+ import javafx .scene .paint .Color ;
8+ import javafx .scene .shape .Circle ;
9+ import javafx .scene .shape .Line ;
10+ import javafx .scene .text .Text ;
11+
12+ public class ClockPane extends Pane {
13+ private int hour ;
14+ private int minute ;
15+ private int second ;
16+ private double w = 250 , h = 250 ;
17+ private int sleepTime = 10 ;
18+ private Thread thread = new Thread (() -> {
19+ try {
20+ while (true ) {
21+ Platform .runLater (() -> setCurrentTime ());
22+ Thread .sleep (sleepTime );
23+ }
24+ }
25+ catch (InterruptedException e ) {
26+ }
27+ });
28+
29+ public ClockPane () {
30+ setCurrentTime ();
31+ thread .start ();
32+ }
33+
34+ public ClockPane (int hour , int minute , int second ) {
35+ this .hour = hour ;
36+ this .minute = minute ;
37+ this .second = second ;
38+ paintClock ();
39+ thread .start ();
40+ }
41+
42+ public void setCurrentTime () {
43+ Calendar calendar = new GregorianCalendar ();
44+ this .hour = calendar .get (Calendar .HOUR_OF_DAY );
45+ this .minute = calendar .get (Calendar .MINUTE );
46+ this .second = calendar .get (Calendar .SECOND );
47+ paintClock ();
48+ }
49+
50+ protected void paintClock () {
51+ double clockRadius = Math .min (w , h ) * 0.8 * 0.5 ;
52+ double centerX = w / 2 ;
53+ double centerY = h / 2 ;
54+
55+ Circle circle = new Circle (centerX , centerY , clockRadius );
56+ circle .setFill (Color .WHITE );
57+ circle .setStroke (Color .BLACK );
58+ Text t1 = new Text (centerX - 5 , centerY - clockRadius + 12 , "12" );
59+ Text t2 = new Text (centerX - clockRadius + 3 , centerY + 5 , "9" );
60+ Text t3 = new Text (centerX + clockRadius - 10 , centerY + 3 , "3" );
61+ Text t4 = new Text (centerX - 3 , centerY + clockRadius - 3 , "6" );
62+
63+ double sLength = clockRadius * 0.8 ;
64+ double secondX = centerX + sLength * Math .sin (second * (2 * Math .PI / 60 ));
65+ double secondY = centerY - sLength * Math .cos (second * (2 * Math .PI / 60 ));
66+ Line sLine = new Line (centerX , centerY , secondX , secondY );
67+ sLine .setStroke (Color .RED );
68+
69+ double mLength = clockRadius * 0.65 ;
70+ double minuteX = centerX + mLength * Math .sin (minute * (2 * Math .PI / 60 ));
71+ double minuteY = centerY - mLength * Math .cos (minute * (2 * Math .PI / 60 ));
72+ Line mLine = new Line (centerX , centerY , minuteX , minuteY );
73+ mLine .setStroke (Color .BLUE );
74+
75+ double hLength = clockRadius * 0.5 ;
76+ double hourX = centerX + hLength * Math .sin ((hour % 12 + minute / 60.0 ) * (2 * Math .PI / 12 ));
77+ double hourY = centerY - hLength * Math .cos ((hour % 12 + minute / 60.0 ) * (2 * Math .PI / 12 ));
78+ Line hLine = new Line (centerX , centerY , hourX , hourY );
79+ hLine .setStroke (Color .GREEN );
80+
81+ getChildren ().clear ();
82+ getChildren ().addAll (circle , t1 , t2 , t3 , t4 , sLine , mLine , hLine );
83+ }
84+
85+ public void moveClock () {
86+ setCurrentTime ();
87+ }
88+
89+ public void resume () {
90+ thread .resume ();
91+ }
92+
93+ public void suspend () {
94+ thread .suspend ();
95+ }
96+ }
0 commit comments