3
我想製作一個「矩形」&每次涉及它的起始位置時,將它的座標更改爲隨機位置。首先,我甚至不知道是否可以這樣做,如果是這樣的話,我想要一個它如何工作的例子。 JavaFX對我來說是一件新事物,所以我對它不瞭解很多,所以我將它移動到了一個隨機位置(矩形),它無限循環,這很好,但不是我所需要的:D。JavaFX節點隨機移動
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);
}
}
它的工作,這正是我需要的。感謝您的光臨。 –