问题描述
我在网格布局中将图标添加到特殊元素(网格?)时遇到问题.我的网格布局包含64个砖",旨在用作棋盘.
我的网格代码如下:
国际象棋棋盘
public class SjakkBrett extends JFrame implements Config {
public ChessBoard() {
setSize(800,800);
setLayout(new GridLayout(BRICKS/ ROWS, 0) );
for (int id = 0; id < BRICKS; id++)
add(new Brick(id));
setVisible(true);
}
配置
public interface Config {
public int ROWS= 8;
public int BRICKS= 64;
}
我的问题是我似乎无法找到一种方法来向板中的特定积木(例如setIcon(new ImageIcon("pawn.png"));
)添加图标,因为我不知道如何使用自己制作的积木ID. /p>
有人可以帮助我吗?
要回答您的特定问题:
List<Brick> bricks = new ArrayList<Brick>();
public ChessBoard() {
setSize(800,800);
setLayout(new GridLayout(BRICKS/ ROWS, 0) );
for (int id = 0; id < BRICKS; id++) {
Brick brick = new Brick(id);
add(brick);
bricks.add(brick);
}
setVisible(true);
}
public void setBrick(int id, int piece) {
bricks.get(id).setPiece(piece);
}
要回答您未解决的问题,让我们考虑一下国际象棋游戏.
国际象棋棋盘已经有一个符号.典型的第一步是e4.由于未指定片段,因此表示典当.可以移至e4的唯一棋子是位于e2上的棋子.因此,e4是将棋子从e2移到e4"的简写方式.
因此,我们有一块砖(正方形)被安排成一块板.根据每块不同的规则,我们具有从一块砖移动到另一块砖的能力.我们也有捕获规则和确定谁赢的规则.
所有这些元素都必须以对象或方法的形式出现在游戏中.
所以,让我们谈谈对象.
我们有一块砖头(正方形).我们有一组称为木板的砖块.我们有件.
这些对象是相互关联的.它们的共同点是位置的概念.
一块砖位于特定的位置(e2).董事会需要知道如何将斑点(e2)转换为有意义的东西(第1行,第4列;假设第0行,第0列是左下角).作品需要知道它的位置(e2),可以合法到达的位置(e3,e4)以及可以到达的位置(e4).
这应该足以让您入门.
I have a problem adding an icon to a spesific element(grid?) in gridlayout. My gridlayout contains 64 "bricks" which is intended to work as a chessboard.
My gridcode looks like this:
ChessBoard
public class SjakkBrett extends JFrame implements Config {
public ChessBoard() {
setSize(800,800);
setLayout(new GridLayout(BRICKS/ ROWS, 0) );
for (int id = 0; id < BRICKS; id++)
add(new Brick(id));
setVisible(true);
}
Config
public interface Config {
public int ROWS= 8;
public int BRICKS= 64;
}
My problem is that I can't seem to find a way to add icons to a specific brick in the board, such as setIcon(new ImageIcon("pawn.png"));
because I don't know how to use the brick ID's I'm making.
Anyone who could help me out here?
To answer your specific question:
List<Brick> bricks = new ArrayList<Brick>();
public ChessBoard() {
setSize(800,800);
setLayout(new GridLayout(BRICKS/ ROWS, 0) );
for (int id = 0; id < BRICKS; id++) {
Brick brick = new Brick(id);
add(brick);
bricks.add(brick);
}
setVisible(true);
}
public void setBrick(int id, int piece) {
bricks.get(id).setPiece(piece);
}
To answer your unasked questions, let's think about a game of chess for a bit.
A chess board already has a notation. A typical first move is e4. Since a piece is not specified, that means a pawn. The only pawn that can move to e4 is the pawn sitting on e2. So, e4 is a shorthand way of saying "move the pawn from e2 to e4".
So, we have bricks (squares) that are arranged to make a board. We have pieces with the ability to move from one brick to another, according to rules that are different for each piece. We also have capture rules and rules for determining who wins.
All of these elements have to be present in the game as either objects or methods.
So, let's talk about objects.
We have a brick (square).We have a collection of bricks called a board.We have pieces.
These objects are interrelated. What they all have in common is the idea of location.
A brick is located in a specific spot (e2).A board needs to know how to translate a spot (e2) into something meaningful (row 1, column 4; assuming row 0, column 0 is the lower left corner).A piece needs to know where it's located (e2), where it can legally go (e3, e4), and where it will go (e4).
This should be enough to get you started.
这篇关于如何使用GridLayout中的特定元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!