JavaFX node random move

I want to create a "rectangle" and change its coordinates at a random location every time it comes to its starting position. First, I don't even know if it is possible to do this, if it is, I would like to give an example of how it works. JavaFX is new to me, so I don't know much about it, so I made it move (rectangle) to a random location and it loops endlessly, which is nice, but not what I need: 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);
    }

} 

      

+3


source to share


1 answer


You can use the PathTransition setOnFinished method and add a new path there and start the path transition again.

I set the loop count to 2. Loop 1 is one direction, but since you have auto reverse enabled, loop 2 is the direction back to the origin.



When this is complete, a new path will be set and the transition will play again.

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

}

      

+2


source







All Articles