Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaSwing:将游戏板绘制为一个由2D对象数组支持的JButton网格_Java_Swing_Grid_Jbutton_Multidimensional Array - Fatal编程技术网

JavaSwing:将游戏板绘制为一个由2D对象数组支持的JButton网格

JavaSwing:将游戏板绘制为一个由2D对象数组支持的JButton网格,java,swing,grid,jbutton,multidimensional-array,Java,Swing,Grid,Jbutton,Multidimensional Array,我正在用Java中Swing制作的GUI编程一个小空间主题的地下城爬虫游戏。我使用的是MVC编程范例。游戏被表示为一个网格,在网格上可以点击按钮将玩家移动到棋盘上方或攻击敌人 在游戏的模型/逻辑中,我通过使用以下方法将对象随机分配到x(列)和y(行)坐标,使用2D数组生成游戏环境的抽象表示: //methods public void setGalaxy() { galaxyArray = new GalacticObject[this.row][this.col];

我正在用Java中Swing制作的GUI编程一个小空间主题的地下城爬虫游戏。我使用的是MVC编程范例。游戏被表示为一个网格,在网格上可以点击按钮将玩家移动到棋盘上方或攻击敌人

在游戏的模型/逻辑中,我通过使用以下方法将对象随机分配到x(列)和y(行)坐标,使用2D数组生成游戏环境的抽象表示:

    //methods
public void setGalaxy() {
    galaxyArray = new GalacticObject[this.row][this.col];

    //construct the array
    Random rand = new Random();
    int random;
    for (int i = 0; i < this.row; i++) {
        for (int j = 0; j < this.col; j++) {
            GalacticObject obj = new GalacticObject(i,j);
            random = rand.nextInt(100);
            if (random < 5) {
                //asteroid (5 percent chance)
                obj.makeAsteroid();
            } else if (random < 9) {
                //blackhole (4 percent chance)
                obj.makeBlackHole();
            } else {
                //rest is open (= empty)
                obj.makeOpen();
            }
            galaxyArray[i][j] = obj;
        }
    }
}

具体细节取决于您如何实现。接下来,所引用的示例只允许主视图保留对名为
board
的数组的私有引用,该数组由游戏模型维护。由于
RCView
实现了接口,因此视图的
update()
实现只需迭代
,并更新其
平铺数组作为响应。请注意,
update()
方法签名包含对请求更新的方法的引用。以你为例,

public void update(Observable model, Object arg) {
    GalacticObject galaxyArray = ((GameModel) model).getGalaxyArray();
    //loop though model, updating view components
    this.repaint();
}

像?在本例中,没有更高级别的电路板表示为与视图分离的二维对象阵列。我要问的是如何从makeBoard方法中最好地访问galaxyArray中存储的对象,以便使用数组中对象的变量在按钮上绘制一个图像,并在网格中使用相应的坐标。代码中存储了一个按钮数组。。
public void updateGameBoard(Galaxy board) {
/** This method initializes the board when the galaxy is first created in the game logic and updates it throughout the game */
    Galaxy galaxyArray = board;
    for (int r = 0; r < this.boardWidth; r++) {
        for (int c = 0; c < this.boardLength; c++) {
            //make galactic object and extract info from Galaxy
            GalacticObject temporary = new GalacticObject(r,c);
            temporary = galaxyArray.getGalacticObject(r,c);

            //check the object and set the corresponding tile image 
            if (temporary.isAsteroid()) {
                gridButton[r][c].setText("A");
            } else if (temporary.isAsteroidField()) {
                gridButton[r][c].setText("AF");
            } else if (temporary.isBlackHole()) {
                gridButton[r][c].setText("BH");
            } else if (temporary.isOpen()) {
                gridButton[r][c].setText("");
            }
        }
    }   
}
public void update(Observable model, Object arg) {
    GalacticObject galaxyArray = ((GameModel) model).getGalaxyArray();
    //loop though model, updating view components
    this.repaint();
}