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