1 import java.util.Calendar;
2 import java.util.GregorianCalendar;
3 import javafx.scene.layout.Pane;
4 import javafx.scene.paint.Color;
5 import javafx.scene.shape.Circle;
6 import javafx.scene.shape.Line;
7 import javafx.scene.text.Text;
8
9 public class ClockPane extends Pane {
10 private int hour;
11 private int minute;
12 private int second;
13
14
15 public ClockPane() {
16 setCurrentTime();
17 }
18
19
20 public ClockPane(int hour, int minute, int second) {
21 this.hour = hour;
22 this.minute = minute;
23 this.second = second;
24 }
25
26
27 public int getHour() {
28 return hour;
29 }
30
31
32 public void setHour(int hour) {
33 this.hour = hour;
34 paintClock();
35 }
36
37
38 public int getMinute() {
39 return minute;
40 }
41
42
43 public void setMinute(int minute) {
44 this.minute = minute;
45 paintClock();
46 }
47
48
49 public int getSecond() {
50 return second;
51 }
52
53
54 public void setSecond(int second) {
55 this.second = second;
56 paintClock();
57 }
58
59
60 public void setCurrentTime() {
61
62 Calendar calendar = new GregorianCalendar();
63
64
65 this.hour = calendar.get(Calendar.HOUR_OF_DAY);
66 this.minute = calendar.get(Calendar.MINUTE);
67 this.second = calendar.get(Calendar.SECOND);
68
69 paintClock();
70 }
71
72
73 private void paintClock() {
74
75 double clockRadius =
76 Math.min(getWidth(), getHeight()) * 0.8 * 0.5;
77 double centerX = getWidth() / 2;
78 double centerY = getHeight() / 2;
79
80
81 Circle circle = new Circle(centerX, centerY, clockRadius);
82 circle.setFill(Color.WHITE);
83 circle.setStroke(Color.BLACK);
84 Text t1 = new Text(centerX - 5, centerY - clockRadius + 12, "12");
85 Text t2 = new Text(centerX - clockRadius + 3, centerY + 5, "9");
86 Text t3 = new Text(centerX + clockRadius - 10, centerY + 3, "3");
87 Text t4 = new Text(centerX - 3, centerY + clockRadius - 3, "6");
88
89
90 double sLength = clockRadius * 0.8;
91 double secondX = centerX + sLength *
92 Math.sin(second * (2 * Math.PI / 60));
93 double secondY = centerY - sLength *
94 Math.cos(second * (2 * Math.PI / 60));
95 Line sLine = new Line(centerX, centerY, secondX, secondY);
96 sLine.setStroke(Color.RED);
97
98
99 double mLength = clockRadius * 0.65;
100 double xMinute = centerX + mLength *
101 Math.sin(minute * (2 * Math.PI / 60));
102 double minuteY = centerY - mLength *
103 Math.cos(minute * (2 * Math.PI / 60));
104 Line mLine = new Line(centerX, centerY, xMinute, minuteY);
105 mLine.setStroke(Color.BLUE);
106
107
108 double hLength = clockRadius * 0.5;
109 double hourX = centerX + hLength *
110 Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12));
111 double hourY = centerY - hLength *
112 Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12));
113 Line hLine = new Line(centerX, centerY, hourX, hourY);
114 hLine.setStroke(Color.GREEN);
115
116 getChildren().clear();
117 getChildren().addAll(circle, t1, t2, t3, t4, sLine, mLine, hLine);
118 }
119
120 @Override
121 public void setWidth(double width) {
122 super.setWidth(width);
123 paintClock();
124 }
125
126 @Override
127 public void setHeight(double height) {
128 super.setHeight(height);
129 paintClock();
130 }
131 }