无法添加多个相同的矩形

时间:2017-09-14 00:33:25

标签: java image javafx 2d rectangles

我想在我的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]

1 个答案:

答案 0 :(得分:0)

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

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

好吧,我用谷歌的一些图片测试了这个程序。

enter image description here

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

enter image description here