JavaFX、控制一组风扇

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;
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值