控制时钟)修改程序清单14-21以在类中加入动画,添加两个方法start()和stop()以启动和停止时钟。编写程序,让用户使用Start和Stop按钮来控制始终
时间: 2023-07-15 11:12:04 浏览: 127
以下是修改后的程序清单14-21:
```java
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.property.DoubleProperty;
import javafx.scene.Group;
import javafx.scene.effect.DropShadow;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.util.Duration;
public class ControlledClock extends Group {
private final Circle face = new Circle(80, 80, 80);
private final Text[] text = new Text[12];
private final Line[] tick = new Line[60];
private final DoubleProperty seconds;
private final DoubleProperty minutes;
private final DoubleProperty hours;
private Timeline animation;
public ControlledClock() {
// Draw clock face
face.setFill(Color.WHITE);
face.setStroke(Color.BLACK);
getChildren().add(face);
// Draw numbers
for (int i = 1; i <= 12; i++) {
text[i - 1] = new Text(i + "");
text[i - 1].setFont(Font.font(20));
double angle = i * Math.PI / 6;
double x = 80 + 60 * Math.sin(angle);
double y = 80 - 60 * Math.cos(angle);
text[i - 1].setX(x - text[i - 1].getLayoutBounds().getWidth() / 2);
text[i - 1].setY(y + text[i - 1].getLayoutBounds().getHeight() / 2);
getChildren().add(text[i - 1]);
}
// Draw ticks
for (int i = 0; i < 60; i++) {
tick[i] = new Line(0, 0, 0, 0);
tick[i].setStrokeWidth(i % 5 == 0 ? 3 : 1);
tick[i].setStroke(Color.BLACK);
tick[i].setEffect(new DropShadow(5, Color.BLACK));
double angle = i * Math.PI / 30;
double x1 = 80 + 70 * Math.sin(angle);
double y1 = 80 - 70 * Math.cos(angle);
double x2 = 80 + 80 * Math.sin(angle);
double y2 = 80 - 80 * Math.cos(angle);
tick[i].setStartX(x1);
tick[i].setStartY(y1);
tick[i].setEndX(x2);
tick[i].setEndY(y2);
getChildren().add(tick[i]);
}
// Draw hands
Line hourHand = new Line(0, 0, 0, -50);
hourHand.setStrokeWidth(5);
hourHand.setStroke(Color.BLACK);
hourHand.setEffect(new DropShadow(5, Color.BLACK));
hourHand.setTranslateX(80);
hourHand.setTranslateY(80);
getChildren().add(hourHand);
hours = hourHand.rotateProperty();
Line minuteHand = new Line(0, 0, 0, -70);
minuteHand.setStrokeWidth(3);
minuteHand.setStroke(Color.BLACK);
minuteHand.setEffect(new DropShadow(5, Color.BLACK));
minuteHand.setTranslateX(80);
minuteHand.setTranslateY(80);
getChildren().add(minuteHand);
minutes = minuteHand.rotateProperty();
Line secondHand = new Line(0, 0, 0, -80);
secondHand.setStrokeWidth(1);
secondHand.setStroke(Color.RED);
secondHand.setEffect(new DropShadow(5, Color.BLACK));
secondHand.setTranslateX(80);
secondHand.setTranslateY(80);
getChildren().add(secondHand);
seconds = secondHand.rotateProperty();
// Setup animation
KeyFrame keyFrame = new KeyFrame(Duration.seconds(1), e -> {
seconds.set(seconds.get() + 6);
minutes.set(minutes.get() + 0.1);
hours.set(hours.get() + 1.0 / 120);
});
animation = new Timeline(keyFrame);
animation.setCycleCount(Timeline.INDEFINITE);
}
public void start() {
animation.play();
}
public void stop() {
animation.pause();
}
}
```
在这个修改后的版本中,我们添加了两个方法 `start()` 和 `stop()` 以启动和停止时钟的动画。这个方法使用 JavaFX 中的 `Timeline` 类来实现动画效果。
我们可以编写以下代码来创建一个带有 "Start" 和 "Stop" 按钮的窗口,并控制时钟的开始和停止:
```java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ControlledClockDemo extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
ControlledClock clock = new ControlledClock();
Button startButton = new Button("Start");
startButton.setOnAction(e -> clock.start());
Button stopButton = new Button("Stop");
stopButton.setOnAction(e -> clock.stop());
VBox root = new VBox(clock, startButton, stopButton);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
```
这个程序创建了一个带有时钟和两个按钮的窗口。当用户点击 "Start" 按钮时,时钟动画开始播放;当用户点击 "Stop" 按钮时,时钟动画停止。
阅读全文