Can't add multiple of same rectangle

回眸只為那壹抹淺笑 提交于 2019-12-02 17:01:08

问题


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.



来源:https://stackoverflow.com/questions/46208756/cant-add-multiple-of-same-rectangle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!