【发布时间】:2014-12-23 11:06:10
【问题描述】:
我正在使用 Java 中的 Swing 制作一个以空间为主题的小型地牢爬行游戏。我正在使用 MVC 编程范例。游戏以网格的形式呈现,点击按钮可以让玩家在棋盘上移动或攻击敌人。
在游戏的模型/逻辑中,我使用 2D 数组生成游戏环境的抽象表示,方法是使用以下方法将对象随机分配到 x(列)和 y(行)坐标:
//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;
}
}
}
现在我想使用 JButton 网格在 JPanel 容器中绘制由这个 2D 对象数组支持的实际 GUI 游戏板。我想基本上调用galaxyArray中的所有对象并在ButtonGrid中的相应坐标处绘制一个带有相应图像叠加层的JButton。使用 Swing 实现这一目标的最佳方法是什么?
目前,我还为绘制 JButtonGrid 的方法编写了以下草稿,但我仍然看不到由 2D 对象数组支持它的最佳策略是什么。 此外,由于我不清楚的原因,为按钮添加图标不起作用。 (我把那部分注释掉了。)
public JPanel makeGameBoard() {
/** This method makes a first version of the game board when the object is first called */
//create a new JPanel
JPanel boardPanel = new JPanel();
// create a gridButton of JButtons defined by the width and length
JButton[][] gridButton = new JButton[this.boardWidth][this.boardLength];
// set layout
boardPanel.setLayout(new GridLayout(this.boardWidth,this.boardLength));
//for-loops to place buttons in gridButton
for(int y = 0; y < this.boardLength; y++) {
for(int x = 0; x < this.boardWidth; x++){
// creates new button (with coordinates as string); gridButton[x][y] needs to be reused as coordinate system
gridButton[x][y]=new JButton(x+":"+y);
gridButton[x][y].setActionCommand(x+":"+y);
gridButton[x][y].setText("");
gridButton[x][y].addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String com = e.getActionCommand();
System.out.println(com);
}
});
// add icon to every button
// try {
// Image img = ImageIO.read(getClass().getResource("resources/invisible.png"));
// gridButton[x][y].setIcon(new ImageIcon(img));
// }
// catch (IOException ex) {
// System.out.println("Image file for gridButton not found!");
// }
// add the gridButton to the panel
boardPanel.add(gridButton[x][y]);
}
}
//return boardPanel
return boardPanel;
}
基本上我想知道从我的makeGameBoard 方法访问存储在galaxyArray 中的对象的最佳策略是什么,以便可以使用数组中对象的变量在按钮上绘制相应的图像网格中的坐标。
/*EDIT 感谢下面友好人士的建议和链接教程,我已经成功了!以下是任何有类似问题的人的板视图代码:
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("");
}
}
}
}
【问题讨论】:
-
喜欢this?
-
在此示例中,没有更高级别的棋盘表示为与视图分离的二维对象数组。我本质上要问的是如何从我的 makeBoard 方法中最好地访问存储在galaxyArray中的对象,以便可以使用数组中对象的变量在按钮上绘制具有网格中相应坐标的图像。跨度>
-
Making a robust, resizable Swing Chess GUI 中的代码存储了一个按钮数组..
标签: java swing grid jbutton multidimensional-array