不能添加多个相同的矩形 [英] Can't add multiple of same rectangle
问题描述
我想在我的 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);
}
}
}
}
}
运行时出现此错误:
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);
}
}
}
}
嗯,我用谷歌的一些图片测试了这个程序.
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.
这篇关于不能添加多个相同的矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!