Reputation: 85
I want to make a "rectangle" & change it's coordinates to random location each time it comes to it's starting position . First at all I don't even know if it's possible to do this way,if it is I would like an example of how it works. JavaFX is a new thing to me so I don't know much about it, so I made it move (rectangle) to a random location and it's infinitely looping 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);
}
}
Upvotes: 3
Views: 3000
Reputation: 18415
You can use the PathTransition's setOnFinished method and add a new path in there and play the path transition again.
I set the cycle count to 2. Cycle 1 is one direction, but since you have autoreverse enabled, cycle 2 is the direction back to the origin.
When that is finished, a new path is set and the transition is played 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);
}
}
Upvotes: 2