package fx;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Exercise16_19 extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Fan fan1 = new Fan(); //风扇对象
Fan fan2 = new Fan();
Fan fan3 = new Fan();
HBox hBox = new HBox(10, fan1, fan2, fan3);
hBox.setAlignment(Pos.CENTER);
Button start = new Button("Start All"); //启动按钮
Button stop = new Button("Stop All"); //停止按钮
HBox bottomHBox = new HBox(10, start, stop);
bottomHBox.setAlignment(Pos.CENTER);
start.setOnAction(event -> { //按钮注册动作事件
fan1.getAnimation().play();
fan2.getAnimation().play();
fan3.getAnimation().play();
});
stop.setOnAction(event -> {
fan1.getAnimation().stop();
fan2.getAnimation().stop();
fan3.getAnimation().stop();
});
BorderPane pane = new BorderPane(hBox);
pane.setBottom(bottomHBox);
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.setTitle("Exercise16_19");
primaryStage.show();
}
/** 内部类Fan */
public class Fan extends BorderPane {
private Timeline animation; //时间线
private Group group = new Group(); //组
public Fan() {
Circle circle = new Circle(70, 70, 60); //圆
circle.setStyle("-fx-stroke: black; -fx-fill: white;"); //圆设置样式
group.getChildren().add(circle);
for (int i = 0, j = 90; i < 4; i++, j += 90) { //扇叶
Arc arc = new Arc(70, 70, 50, 50,30 + j, 35);
arc.setType(ArcType.ROUND);
group.getChildren().add(arc);
}
Button pause = new Button("Pause"); //按钮
Button resume = new Button("Resume");
Button reverse = new Button("Reverse");
HBox hBox = new HBox(10, pause, resume, reverse);
hBox.setAlignment(Pos.BOTTOM_CENTER);
//关键帧
KeyFrame keyFrame1 = new KeyFrame(Duration.millis(10), event -> group.setRotate(group.getRotate()+1));
KeyFrame keyFrame2 = new KeyFrame(Duration.millis(10), event -> group.setRotate(group.getRotate()-1));
animation = new Timeline(keyFrame1);
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
pause.setOnAction(event -> animation.pause()); //按钮注册动作事件
resume.setOnAction(event -> animation.play());
reverse.setOnAction(event -> {
animation.stop();
animation.getKeyFrames().add(animation.getKeyFrames().remove(0).equals(keyFrame1) ? keyFrame2 : keyFrame1);
animation.play();
});
Slider slider = new Slider(); //滑动条
slider.valueProperty().addListener(observable -> animation.setRate(slider.getValue())); //滑动条添加监听器
this.setCenter(new BorderPane(group));
this.setStyle("-fx-border-color: black;");
this.setTop(hBox);
this.setBottom(slider);
}
public Timeline getAnimation() {
return animation;
}
}
}
JavaFX、控制一组风扇
最新推荐文章于 2023-03-14 18:39:16 发布