当前位置: 首页 > 面试题库 >

JavaFX复制相同的动画

蔚楷
2023-03-14
问题内容

我正在尝试扩大以前的任务。它是画一个风扇,并有一个滑块来控制播放速度,并带有播放,暂停和反转风扇的按钮。这是我的作业代码:

public class FanWithControls extends Application {
    @Override // Override the start method in the Application class
    public void start(Stage primaryStage) {
    FanPane fan = new FanPane();
    StackPane spane = new StackPane();
    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

    Timeline animation = new Timeline(
                                      new KeyFrame(Duration.millis(50), e ->      fan.move()));
    animation.setCycleCount(Timeline.INDEFINITE);
    animation.play(); // Start animation

    scene.widthProperty().addListener(e -> fan.setW(fan.getWidth()));
    scene.heightProperty().addListener(e -> fan.setH(fan.getHeight()));

    btPause.setOnAction(e -> animation.pause());
    btResume.setOnAction(e -> animation.play());
    btReverse.setOnAction(e -> fan.reverse());

    slider.valueProperty().addListener(ov -> animation.setRate(slider.getValue()));

}

/**
 * The main method is only needed for the IDE with limited
 * JavaFX support. Not needed for running from the command line.
 */
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);


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

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

我想知道是否有人可以帮助我。我很难让它工作。香港专业教育学院试图制作一个包含多个扇形的hbox,但是它们的尺寸会缩小,而当我调整框的大小时却不会增长。

基本上,我正在努力做到这一点,因此您可以使用一个滑块,最多允许5个扇形面板。增加它会增加更多的扇形,减小它会带走它们,等等。

任何提示或帮助将不胜感激,谢谢!


问题答案:

使用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);
                }
            }
        });

    }

    /**
     * The main method is only needed for the IDE with limited JavaFX support.
     * Not needed for running from the command line.
     */
    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();
        }

    }
}


 类似资料:
  • 问题内容: 在JavaFx中,我使用以下代码创建一个StackedBarChart: 结果是从1到8的系列具有不同的颜色。9-10系列与1-2系列具有相同的颜色。我试图在style.css文件中为条形图指定默认颜色,但似乎9-10系列实际上使用了颜色0和1。我认为这是一个错误。有谁知道解决方法? 问题答案: 颜色在8个系列之后被回收(原因是必须对定义的颜色数量进行一定的硬编码限制:JavaFX C

  • 下面是一个说明问题的小应用程序: ButtonPanel.fxml 按钮PanelController。Java语言 TestApp。Java语言 基本上,我有一个FXML,用于两个单独的场景,它们可能在屏幕上同时处于活动状态,也可能不处于活动状态。这方面的一个实际例子是,将内容固定到一个侧面板上,再加上一个按钮,该按钮可以在一个可以拖动/调整大小的单独窗口中打开相同的内容。 我试图实现的目标是保

  • 我有两个名为Site和AppSite的对象,两个对象都有如下相同的字段。是否有任何util类将所有字段从AppSite复制到站点,如BeanUtils。copyProperties。 如果你看到上面的两个pojo,我有两个对象字段列表。两个对象也一样,只有Site和AmsSite对象。有相同的字段名,但不同的类名。 BeanUtils.copy属性是将所有文字字段值从AppSite正确复制到Sit

  • 我正在使用以下Robocopy命令: 问题是它一直在复制*EXTRA file下列出的某些文件,但该文件已在目标中。 我只希望robocopy复制一个更新版本的文件(创建日期不同) 仅当测试文件。zip的创建日期较新。 为了解决这个问题,我尝试使用/xo表示排除旧文件。没有运气。每当我每分钟执行一次脚本时,都会发生这种情况。

  • 使用以下逻辑使用输入/输出流复制文件。由于使用相同大小的字节缓冲区,使用Bufferred流真的有好处吗?

  • 93.背景 HBase 中的当前复制异步。因此,如果主集群崩溃,则从集群可能没有最新数据。如果用户想要强一致性,那么他们就无法切换到从属群集。 94.设计 请参阅 HBASE-19064 上的设计文档 95.运行和维护 Case.1 设置两个同步复制集群 在源集群和对等集群中添加同步对等体。 对于源群集: hbase> add_peer '1', CLUSTER_KEY => 'lg-hadoo