本文介绍了无法添加相同矩形的多个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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]


推荐答案

尝试在 Pane 对象中两次添加相同的 Node 是不正确的行为。
因此,您每次创建对象时都要创建一个新对象。

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

,我用Google的某些图片测试了该程序。

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.

这篇关于无法添加相同矩形的多个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 15:41