gpt4 book ai didi

JavaFX节点随机移动

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:19:32 24 4
gpt4 key购买 nike

我想制作一个“矩形”并在每次到达起始位置时将其坐标更改为随机位置。首先,我什至不知道是否可以这样做,如果可以的话,我想举个例子说明它是如何工作的。 JavaFX 对我来说是一个新事物,所以我不太了解它,所以我让它移动(矩形)到一个随机位置并且它是无限循环的,这很好但不是我需要的 :D。

public class Java2 extends Application {


public static final int PANEL_WIDTH = 600;
public static final int PANEL_HEIGHT = 600;




@Override
public void start(Stage primaryStage) {

Random ran = new Random();

int loc= ran.nextInt(600 - 300 + 1) + 300; //min=300 , max=600

Rectangle rekt = new Rectangle(20, 20);

Pane root = new Pane();

root.getChildren().add(rekt);

Scene scene = new Scene(root, PANEL_WIDTH, PANEL_HEIGHT);




PathTransition pathTransition = new PathTransition();
Path path = new Path();

path.getElements().add(new MoveTo(20,20));
path.getElements().add(new LineTo(loc,600));


pathTransition.setDuration(javafx.util.Duration.millis(4000));
pathTransition.setPath(path);
pathTransition.setNode(rekt);
pathTransition.setOrientation(
PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
pathTransition.setCycleCount(Timeline.INDEFINITE);
pathTransition.setAutoReverse(true);
pathTransition.play();






primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
r1.requestFocus();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

}

最佳答案

您可以使用 PathTransition 的 setOnFinished方法并在其中添加一个新路径并再次播放路径转换。

我将循环计数设置为 2。循环 1 是一个方向,但由于您启用了自动反转,循环 2 是返回原点的方向。

完成后,将设置新路径并再次播放过渡。

import javafx.animation.PathTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Java2 extends Application {

public static final int PANEL_WIDTH = 600;
public static final int PANEL_HEIGHT = 600;

Random ran = new Random();

@Override
public void start(Stage primaryStage) {

Rectangle rekt = new Rectangle(20, 20);

Pane root = new Pane();

root.getChildren().add(rekt);

Scene scene = new Scene(root, PANEL_WIDTH, PANEL_HEIGHT);

PathTransition pathTransition = new PathTransition();

pathTransition.setDuration(javafx.util.Duration.millis(500));
pathTransition.setPath(createPath());
pathTransition.setNode(rekt);
pathTransition.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
pathTransition.setCycleCount(2);
pathTransition.setAutoReverse(true);
pathTransition.setOnFinished(e -> {

pathTransition.setPath(createPath());
pathTransition.play();

});
pathTransition.play();

primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}

private Path createPath() {

int loc = ran.nextInt(600 - 300 + 1) + 300; // min=300 , max=600

Path path = new Path();

path.getElements().add(new MoveTo(20, 20));
path.getElements().add(new LineTo(loc, 600));

return path;

}

public static void main(String[] args) {
launch(args);
}

}

关于JavaFX节点随机移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30479295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com