@@ -44,120 +44,119 @@ public void start(Stage primaryStage) {
4444 }
4545
4646
47- public static void main (String [] args ) {
48- launch (args );
49- }
50- }
51- 52- class ClockPane extends Pane {
53- private int hour ;
54- private int minute ;
55- private int second ;
56- 57- private double w = 250 , h = 250 ;
58- 5947 /**
60- * Construct a default clock with the current time
48+ * ClockPane is a Pane that displays a clock.
6149 */
62- public ClockPane () {
63- setCurrentTime ();
64- }
65- 66- public ClockPane (int hour , int minute , int second ) {
67- this .hour = hour ;
68- this .minute = minute ;
69- this .second = second ;
70- paintClock ();
71- }
72- 73- public int getHour () {
74- return hour ;
75- }
76- 77- public void setHour (int hour ) {
78- this .hour = hour ;
79- paintClock ();
80- }
81- 82- public int getMinute () {
83- return minute ;
84- }
85- 86- public void setMinute (int minute ) {
87- this .minute = minute ;
88- paintClock ();
89- }
90- 91- public int getSecond () {
92- return second ;
93- }
94- 95- public void setSecond (int second ) {
96- this .second = second ;
97- paintClock ();
98- }
99- 100- public double getW () {
101- return w ;
102- }
103- 104- public void setW (double w ) {
105- this .w = w ;
106- paintClock ();
107- }
108- 109- public double getH () {
110- return h ;
111- }
112- 113- public void setH (double h ) {
114- this .h = h ;
115- paintClock ();
116- }
117- 118- public void setCurrentTime () {
119- Calendar calendar = new GregorianCalendar ();
120- this .hour = calendar .get (Calendar .HOUR_OF_DAY );
121- this .minute = calendar .get (Calendar .MINUTE );
122- this .second = calendar .get (Calendar .SECOND );
123- paintClock (); // Repaint the clock
124- }
125- 126- protected void paintClock () {
127- double clockRadius = Math .min (w , h ) * 0.8 * 0.5 ;
128- double centerX = w / 2 ;
129- double centerY = h / 2 ;
130- 131- Circle circle = new Circle (centerX , centerY , clockRadius );
132- circle .setFill (Color .WHITE );
133- circle .setStroke (Color .BLACK );
134- Text t1 = new Text (centerX - 5 , centerY - clockRadius + 12 , "12" );
135- Text t2 = new Text (centerX - clockRadius + 3 , centerY + 5 , "9" );
136- Text t3 = new Text (centerX + clockRadius - 10 , centerY + 3 , "3" );
137- Text t4 = new Text (centerX - 3 , centerY + clockRadius - 3 , "6" );
138- double sLength = clockRadius * 0.8 ;
139- double secondX = centerX + sLength *
140- Math .sin (second * (2 * Math .PI / 60 ));
141- double secondY = centerY - sLength *
142- Math .cos (second * (2 * Math .PI / 60 ));
143- Line sLine = new Line (centerX , centerY , secondX , secondY );
144- sLine .setStroke (Color .RED );
145- double mLength = clockRadius * 0.65 ;
146- double xMinute = centerX + mLength *
147- Math .sin (minute * (2 * Math .PI / 60 ));
148- double minuteY = centerY - mLength *
149- Math .cos (minute * (2 * Math .PI / 60 ));
150- Line mLine = new Line (centerX , centerY , xMinute , minuteY );
151- mLine .setStroke (Color .BLUE );
152- 153- double hLength = clockRadius * 0.5 ;
154- double hourX = centerX + hLength *
155- Math .sin ((hour % 12 + minute / 60.0 ) * (2 * Math .PI / 12 ));
156- double hourY = centerY - hLength *
157- Math .cos ((hour % 12 + minute / 60.0 ) * (2 * Math .PI / 12 ));
158- Line hLine = new Line (centerX , centerY , hourX , hourY );
159- hLine .setStroke (Color .GREEN );
160- getChildren ().clear ();
161- getChildren ().addAll (circle , t1 , t2 , t3 , t4 , sLine , mLine , hLine );
50+ class ClockPane extends Pane {
51+ private int hour ;
52+ private int minute ;
53+ private int second ;
54+ 55+ private double w = 250 , h = 250 ;
56+ 57+ /**
58+ * Construct a default clock with the current time
59+ */
60+ public ClockPane () {
61+ setCurrentTime ();
62+ }
63+ 64+ public ClockPane (int hour , int minute , int second ) {
65+ this .hour = hour ;
66+ this .minute = minute ;
67+ this .second = second ;
68+ paintClock ();
69+ }
70+ 71+ public int getHour () {
72+ return hour ;
73+ }
74+ 75+ public void setHour (int hour ) {
76+ this .hour = hour ;
77+ paintClock ();
78+ }
79+ 80+ public int getMinute () {
81+ return minute ;
82+ }
83+ 84+ public void setMinute (int minute ) {
85+ this .minute = minute ;
86+ paintClock ();
87+ }
88+ 89+ public int getSecond () {
90+ return second ;
91+ }
92+ 93+ public void setSecond (int second ) {
94+ this .second = second ;
95+ paintClock ();
96+ }
97+ 98+ public double getW () {
99+ return w ;
100+ }
101+ 102+ public void setW (double w ) {
103+ this .w = w ;
104+ paintClock ();
105+ }
106+ 107+ public double getH () {
108+ return h ;
109+ }
110+ 111+ public void setH (double h ) {
112+ this .h = h ;
113+ paintClock ();
114+ }
115+ 116+ public void setCurrentTime () {
117+ Calendar calendar = new GregorianCalendar ();
118+ this .hour = calendar .get (Calendar .HOUR_OF_DAY );
119+ this .minute = calendar .get (Calendar .MINUTE );
120+ this .second = calendar .get (Calendar .SECOND );
121+ paintClock (); // Repaint the clock
122+ }
123+ 124+ protected void paintClock () {
125+ double clockRadius = Math .min (w , h ) * 0.8 * 0.5 ;
126+ double centerX = w / 2 ;
127+ double centerY = h / 2 ;
128+ 129+ Circle circle = new Circle (centerX , centerY , clockRadius );
130+ circle .setFill (Color .WHITE );
131+ circle .setStroke (Color .BLACK );
132+ Text t1 = new Text (centerX - 5 , centerY - clockRadius + 12 , "12" );
133+ Text t2 = new Text (centerX - clockRadius + 3 , centerY + 5 , "9" );
134+ Text t3 = new Text (centerX + clockRadius - 10 , centerY + 3 , "3" );
135+ Text t4 = new Text (centerX - 3 , centerY + clockRadius - 3 , "6" );
136+ double sLength = clockRadius * 0.8 ;
137+ double secondX = centerX + sLength *
138+ Math .sin (second * (2 * Math .PI / 60 ));
139+ double secondY = centerY - sLength *
140+ Math .cos (second * (2 * Math .PI / 60 ));
141+ Line sLine = new Line (centerX , centerY , secondX , secondY );
142+ sLine .setStroke (Color .RED );
143+ double mLength = clockRadius * 0.65 ;
144+ double xMinute = centerX + mLength *
145+ Math .sin (minute * (2 * Math .PI / 60 ));
146+ double minuteY = centerY - mLength *
147+ Math .cos (minute * (2 * Math .PI / 60 ));
148+ Line mLine = new Line (centerX , centerY , xMinute , minuteY );
149+ mLine .setStroke (Color .BLUE );
150+ 151+ double hLength = clockRadius * 0.5 ;
152+ double hourX = centerX + hLength *
153+ Math .sin ((hour % 12 + minute / 60.0 ) * (2 * Math .PI / 12 ));
154+ double hourY = centerY - hLength *
155+ Math .cos ((hour % 12 + minute / 60.0 ) * (2 * Math .PI / 12 ));
156+ Line hLine = new Line (centerX , centerY , hourX , hourY );
157+ hLine .setStroke (Color .GREEN );
158+ getChildren ().clear ();
159+ getChildren ().addAll (circle , t1 , t2 , t3 , t4 , sLine , mLine , hLine );
160+ }
162161 }
163- }
162+ }
0 commit comments