JavaFX节点随机移动

时间:2015-05-27 10:04:51

标签: java javafx

我想做一个“矩形”&每当它到达它的起始位置时,将它的坐标改为随机位置。首先,我甚至不知道是否可以这样做,如果它是我想要一个如何工作的例子。 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);
    }

} 

1 个答案:

答案 0 :(得分:2)

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

}