如何在AnchorPane上随机地在javafx中放置多个矩形? 此外,他们不得重叠。
有什么想法吗?你的
答案 0 :(得分:1)
试试这个。
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class RectangleDemo extends Application {
@Override
public void start(Stage stage) {
AnchorPane root = new AnchorPane();
Scene scene = new Scene(root);
stage.setScene(scene);
int columns = 20, rows = 10, horizontal = 50, vertical = 20;
Rectangle rect = null;
for (int i = 0; i < columns; ++i) {
for (int j = 0; j < rows; ++j) {
rect = new Rectangle(horizontal * j, vertical * i, horizontal, vertical);
rect.setStroke(Color.RED);
root.getChildren().add(rect);
}
}
scene.setRoot(root);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}