栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

JavaFX复制相同的动画

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JavaFX复制相同的动画

使用

FlowPane
代替
StackPane
并在
FanPane
类内部声明Animation 。这是代码

public class FanWithControls extends Application {    @Override // Override the start method in the Application class    public void start(Stage primaryStage) {        FanPane fan = new FanPane();        FlowPane spane = new FlowPane();        spane.getChildren().addAll(fan);        HBox hBox = new HBox(5);        Button btPause = new Button("Pause");        Button btResume = new Button("Resume");        Button btReverse = new Button("Reverse");        hBox.setAlignment(Pos.CENTER);        hBox.getChildren().addAll(btPause, btResume, btReverse);        Slider slider = new Slider(0, 10, 3);        slider.setShowTickLabels(true);        slider.setShowTickMarks(true);        BorderPane pane = new BorderPane();        pane.setCenter(spane);        pane.setTop(hBox);        pane.setBottom(slider);        // Create a scene and place it in the stage        Scene scene = new Scene(pane, 300, 400);        primaryStage.setTitle("FanWithControls"); // Set the stage title        primaryStage.setScene(scene); // Place the scene in the stage        primaryStage.show(); // Display the stage        fan.setAnimation(fan);        Timeline animation = fan.getAnimation();        scene.widthProperty().addListener(e -> fan.setW(fan.getWidth()));        scene.heightProperty().addListener(e -> fan.setH(fan.getHeight()));        btPause.setonAction((e) -> { for (Node fans : spane.getChildren()) {     FanPane fanpane = (FanPane) fans;     fanpane.getAnimation().pause(); }// animation.pause();        });        btResume.setonAction((e) -> { for (Node fans : spane.getChildren()) {     FanPane fanpane = (FanPane) fans;     fanpane.getAnimation().play(); }// animation.play();        });        btReverse.setonAction((e) -> { for (Node fans : spane.getChildren()) {     FanPane fanpane = (FanPane) fans;     fanpane.reverse(); }// fan.reverse();        });        slider.valueProperty().addListener((ov) -> {// animation.setRate(slider.getValue()); for (Node fans : spane.getChildren()) {     FanPane fanpane = (FanPane) fans;     fanpane.getAnimation().setRate(slider.getValue()); } if (spane.getChildren().size() < (int) slider.getValue()) {     for (int i = spane.getChildren().size(); i < (int) slider.getValue(); i++) {         FanPane fanPane = new FanPane();         spane.getChildren().add(fanPane);         fanPane.setAnimation(fanPane);         fanPane.getAnimation().setRate(slider.getValue());     } } else if (spane.getChildren().size() > (int) slider.getValue()) {     for (int i = (int) slider.getValue(); i < spane.getChildren().size(); i++) {         spane.getChildren().remove(spane.getChildren().size() - 1);     } }        });    }        public static void main(String[] args) {        launch(args);    }    class FanPane extends Pane {        private double w = 200;        private double h = 200;        private double radius = Math.min(w, h) * 0.45;        private Arc arc[] = new Arc[4];        private double startAngle = 30;        private Circle circle = new Circle(w / 2, h / 2, radius);        private Timeline animation;        public FanPane() { circle.setStroke(Color.BLUE); circle.setFill(Color.WHITE); circle.setStrokeWidth(4); getChildren().add(circle); for (int i = 0; i < 4; i++) {     arc[i] = new Arc(w / 2, h / 2, radius * 0.9, radius * 0.9, startAngle + i * 90, 35);     arc[i].setFill(Color.RED); // Set fill color     arc[i].setType(ArcType.ROUND);     getChildren().addAll(arc[i]); }        }        public Timeline getAnimation() { return animation;        }        public void setAnimation(FanPane fan) { this.animation = new Timeline(new Keyframe(Duration.millis(50), e -> fan.move())); animation.setCycleCount(Timeline.INDEFINITE); animation.play();        }        private double increment = 5;        public void reverse() { increment = -increment;        }        public void move() { setStartAngle(startAngle + increment);        }        public void setStartAngle(double angle) { startAngle = angle; setValues();        }        public void setValues() { radius = Math.min(w, h) * 0.45; circle.setRadius(radius); circle.setCenterX(w / 2); circle.setCenterY(h / 2); for (int i = 0; i < 4; i++) {     arc[i].setRadiusX(radius * 0.9);     arc[i].setRadiusY(radius * 0.9);     arc[i].setCenterX(w / 2);     arc[i].setCenterY(h / 2);     arc[i].setStartAngle(startAngle + i * 90); }        }        public void setW(double w) { this.w = w; setValues();        }        public void setH(double h) { this.h = h; setValues();        }        public double getCenterX() { return circle.getCenterX();        }        public double getCenterY() { return circle.getCenterY();        }        public double getRadius() { return circle.getRadius();        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/418626.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号