Can't add multiple of same rectangle

2020-05-05 17:50发布

问题:

I want to make multiple of the same rectangle on my borderPane to make walls for the game. Each wall is going to look the same and be the same size. I know the image works and that there are no other errors. I use the following code to add the rectangles:

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

I get this error when it is ran:

Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = BorderPane@4a17c25d[styleClass=root]

回答1:

Trying to add the same Node twice in the Pane object is not a correct behavior. So, you have to create new one every time adding object on a pane object.

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

Well, I tested the program with some image from google.

I don't know what you are supposed to do with your game but here is the output image.