无法添加多个相同的矩形

Can't add multiple of same rectangle

我想在我的 borderPane 上制作多个相同的矩形来为游戏制作墙。每面墙看起来都一样,大小也一样。我知道图像有效并且没有其他错误。我使用以下代码添加矩形:

public class Antz extends Application{
public BorderPane borderPane; 
public Scene scene;
public Image wallImage = new Image("/recources/images/walls.png");
public Rectangle wall = new Rectangle();
public int[][] walls = {{1,0,0,0,0,1,1,1,0,1,0,0,0,0,0,1,0},
                  {1,1,1,1,0,1,0,0,0,1,0,1,1,1,0,1,0},
                  {0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0},
                  {0,1,0,1,1,1,0,1,0,1,1,1,0,1,0,1,0},
                  {0,0,0,0,1,0,0,1,0,0,0,1,0,1,1,1,0},
                  {1,0,1,1,1,1,0,1,1,1,1,1,0,1,0,0,0},
                  {1,0,1,0,0,1,0,0,0,0,0,1,0,0,0,1,1},
                  {1,0,1,1,0,1,0,1,0,1,0,0,0,1,0,0,1},
                  {1,0,0,0,0,0,0,1,1,1,0,0,1,1,1,0,1},
                  {0,0,1,0,1,1,0,1,0,1,1,0,1,0,0,0,0},
                  {1,1,1,0,1,0,0,0,0,0,1,0,1,1,0,1,0},
                  {1,0,0,0,0,0,1,1,1,0,1,0,0,1,1,1,0},
                  {1,0,1,0,1,1,1,0,1,1,1,1,0,0,0,1,0},
                  {0,0,1,0,0,0,0,0,0,0,0,1,0,1,0,0,0},
                  {1,0,1,1,1,1,0,1,1,1,0,1,0,1,1,0,1},
                  {0,0,1,0,0,0,0,0,0,1,0,0,0,1,0,0,0}};

public static void main(String[] args){
    launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception {
    buildWindows();
    buildWalls();
    primaryStage.setScene(scene);
    primaryStage.setTitle("Formicidae");
    primaryStage.show();
    primaryStage.setMinHeight(550);
    primaryStage.setMinWidth(700);
    primaryStage.setFullScreenExitHint("");
    primaryStage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);

}

public void buildWindows() {
    borderPane = new BorderPane();
    borderPane.setStyle("-fx-background-color: #000000;");
    scene =  new Scene(borderPane, 700, 550);
}

public void buildWalls(){
    wall = new Rectangle(wallImage.getWidth(), wallImage.getHeight());
    wall.setFill(new ImagePattern(wallImage));
    for(int i = 0;i<walls.length;i++){
        for(int j = 0;j<walls[i].length;j++){
            if(walls[i][j]==1){
                wall.setX(j*20);
                wall.setY(i*20);

                borderPane.getChildren().add(wall);
            }
        }
    }
}
}

我在 运行:

时收到此错误
Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = BorderPane@4a17c25d[styleClass=root]

尝试在 Pane 对象中两次添加相同的 Node 是不正确的行为。 因此,每次在窗格对象上添加对象时都必须创建新对象。

public void buildWalls(){

    wall.setFill(new ImagePattern(wallImage));
    for(int i = 0;i<walls.length;i++){
        for(int j = 0;j<walls[i].length;j++){
            if(walls[i][j]==1){
                wall = new Rectangle(wallImage.getWidth(), wallImage.getHeight());
                wall.setX(j*20);
                wall.setY(i*20);

                borderPane.getChildren().add(wall);
            }
        }
    }
}

好吧,我用 google 中的一些图像测试了程序。

我不知道你应该用你的游戏做什么,但这是输出图像。