使用javafx编写一个程序,显示一个转动的风扇,Pause、Resume 和Reverse按钮用于暂停、继续和反转风扇的转动且位于面板的顶部,并且增加一个滑动条控制风扇的速度
时间: 2024-03-05 17:55:13 浏览: 84
javaFX实验,实现风扇动画
好的,下面是一个简单的JavaFX程序,实现了您所需的功能。您可以根据需求进行修改和完善。
```java
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.stage.Stage;
import javafx.util.Duration;
public class FanDemo extends Application {
private Arc[] blades = new Arc[4];
private Timeline animation;
private double speed = 1.0;
@Override
public void start(Stage primaryStage) {
// 创建风扇面板
StackPane fanPane = new StackPane();
fanPane.setPadding(new Insets(20));
fanPane.setStyle("-fx-background-color: white;");
// 创建风扇叶片
for (int i = 0; i < blades.length; i++) {
blades[i] = new Arc(0, 0, 80, 80, i * 90 + 30, 35);
blades[i].setType(ArcType.ROUND);
blades[i].setFill(Color.RED);
}
fanPane.getChildren().addAll(blades);
// 创建控制面板
BorderPane controlPane = new BorderPane();
controlPane.setPadding(new Insets(10));
// 创建按钮
Button pauseBtn = new Button("Pause");
Button resumeBtn = new Button("Resume");
Button reverseBtn = new Button("Reverse");
pauseBtn.setOnAction(e -> animation.pause());
resumeBtn.setOnAction(e -> animation.play());
reverseBtn.setOnAction(e -> {
for (Arc blade : blades) {
blade.setStartAngle(blade.getStartAngle() + 180);
}
});
// 创建滑动条
Slider speedSlider = new Slider();
speedSlider.setMin(0.1);
speedSlider.setMax(2.0);
speedSlider.setValue(speed);
speedSlider.setShowTickLabels(true);
speedSlider.setShowTickMarks(true);
speedSlider.setMajorTickUnit(0.5);
speedSlider.setMinorTickCount(4);
speedSlider.setBlockIncrement(0.1);
speedSlider.valueProperty().addListener(new ChangeListener<Number>() {
public void changed(ObservableValue<? extends Number> ov, Number oldVal, Number newVal) {
speed = newVal.doubleValue();
animation.setRate(speed);
}
});
// 将按钮和滑动条添加到控制面板
controlPane.setLeft(pauseBtn);
controlPane.setCenter(resumeBtn);
controlPane.setRight(reverseBtn);
controlPane.setBottom(speedSlider);
// 创建动画
animation = new Timeline(new KeyFrame(Duration.millis(10), e -> {
for (Arc blade : blades) {
blade.setStartAngle(blade.getStartAngle() + 2);
}
}));
animation.setCycleCount(Animation.INDEFINITE);
animation.setRate(speed);
animation.play();
// 创建主场景
BorderPane mainPane = new BorderPane();
mainPane.setCenter(fanPane);
mainPane.setTop(controlPane);
Scene scene = new Scene(mainPane, 400, 400);
primaryStage.setTitle("Fan Demo");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
```
运行程序后,您将会看到一个带有风扇和控制按钮的窗口。您可以点击Pause按钮暂停风扇转动,点击Resume按钮继续风扇转动,点击Reverse按钮反转风扇转动方向。同时,您可以通过滑动条控制风扇的转动速度。
阅读全文