Skip to content

Commit f6bb66d

Browse files
committed
commit Exercise_30_07
1 parent f037a2f commit f6bb66d

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package exercise_30_07;
2+
3+
import javafx.application.Application;
4+
import javafx.geometry.Pos;
5+
import javafx.scene.Scene;
6+
import javafx.scene.control.Button;
7+
import javafx.scene.layout.BorderPane;
8+
import javafx.scene.layout.HBox;
9+
import javafx.stage.Stage;
10+
11+
public class Exercise_30_07 extends Application {
12+
13+
@Override
14+
public void start(Stage primaryStage) {
15+
ClockPane clockPane = new ClockPane();
16+
17+
HBox hBox = new HBox(5);
18+
hBox.setAlignment(Pos.CENTER);
19+
Button stop = new Button("Stop");
20+
Button start = new Button("Start");
21+
hBox.getChildren().addAll(stop, start);
22+
23+
BorderPane pane = new BorderPane(clockPane);
24+
pane.setBottom(hBox);
25+
26+
Scene scene = new Scene(pane, 250, 270);
27+
primaryStage.setTitle("Exercise_30_07");
28+
primaryStage.setScene(scene);
29+
primaryStage.show();
30+
31+
stop.setOnAction(e -> clockPane.suspend());
32+
start.setOnAction(e -> clockPane.resume());
33+
}
34+
35+
public static void main(String[] args) {
36+
launch(args);
37+
}
38+
39+
}

0 commit comments

Comments
 (0)