我正在写国际象棋程序,我正在创建国际象棋的GUI部分。
要做到这一点
我制作了一个BoardGUI类和一个TileGUI类。
BoardGUI初始化GridLayout - >循环遍及整个网格 - >在每个网格上初始化TileGUI对象。
TileGUI基本上获取gameBoard的信息 - >根据瓷砖的位置(深色或浅色)涂抹瓷砖 - >将Piece绘制为并将其添加为JLabel对象。
这基本上初始化了我的棋盘GUI(下面的代码)。
在我的JLabel上,我正在做一个小测试,只要点击它(MouseListener)就将它移动到硬编码的位置。
它没有给我一个错误,这意味着它已成功内部移动到该位置(使用我的后端片段移动功能)
但我不知道如何重新绘制电路板以更新电路板状态。
我是否必须重复我的boardPanel构造函数的过程(只是重复使用更新的chessBoard初始化电路板的步骤)?
private class BoardGUI extends JPanel {
BoardGUI() {
super(new GridLayout(NUM_OF_ROWS, NUM_OF_COLS)); //Place tile on each
for(int row = 0; row < NUM_OF_ROWS; row++) {
for(int col = 0; col < NUM_OF_COLS; col++) {
TileGUI tilePanel = new TileGUI(this, new Point(row, col));
add(tilePanel);
}
}
// Invoked when container's subcomponents are modified (added or removed from the container)
validate();
}
}
和TileGUI
private class TileGUI extends JPanel {
private Point tileCoord;
TileGUI(final BoardGUI boardPanel, final Point position) {
super(new GridLayout());
this.tileCoord = position;
paintTile();
placePieceGraphic(boardSetting);
addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
//JOptionPane.showMessageDialog(null, boardSetting[position.x][position.y].getClass().getSimpleName());
board.movePieceTo(boardSetting, 4, 4, boardSetting[position.x][position.y]);
validate();
}
validate();
}
private void placePieceGraphic(Piece[][] boardSetting) {
// Adds an empty JButton when the tile is empty
if( boardSetting[tileCoord.x][tileCoord.y] == null) {
JButton pieceButton = new JButton();
pieceButton.setOpaque(false);
pieceButton.setContentAreaFilled(false);
pieceButton.setBorderPainted(false);
add(pieceButton);
pieceButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "empty tile");
}
});
}
if( boardSetting[tileCoord.x][tileCoord.y] != null) {
// Retrieves the first letter of the class name
// Retrieves the team name: UP or DOWN
try {
BufferedImage image = ImageIO.read(filename + ".png"));
JButton pieceButton = new JButton(new ImageIcon(image));
add(pieceButton);
pieceButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
highlightValidMovement(getMovablePositions(boardSetting, tileCoord.x, tileCoord.y)); // This tries to highlight the movable tiles
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void paintTile() {
boolean isBrightTile = (this.tileCoord.x % 2) == (this.tileCoord.y % 2);
boolean isDarkTile = (this.tileCoord.x % 2) != (this.tileCoord.y % 2);
if(isBrightTile) setBackground(darkColor);
if(isDarkTile) setBackground(brightColor);
}
}
隐藏不重要的代码。
mouseClicked是处理Piece移动的事件。
编辑:
private class TileGUI extends JPanel {
private Point tileCoord;
TileGUI(final BoardGUI boardPanel, final Point position) {
super(new GridLayout());
this.tileCoord = position;
paintTile();
placePieceGraphic(boardSetting);
addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, boardSetting[position.x][position.y].getClass().getSimpleName());
board.movePieceTo(boardSetting, 4, 4, boardSetting[position.x][position.y]);
placePieceGraphic(boardSetting);
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
});
// Invoked when container's subcomponents are modified (added or removed from the container)
validate();
}
placePieceGraphic:
private void placePieceGraphic(Piece[][] boardSetting) {
removeAll();
// Adds an empty JButton when the tile is empty
if( boardSetting[tileCoord.x][tileCoord.y] == null) {
JLabel pieceLabel = new JLabel();
pieceLabel.setLocation(tileCoord.x, tileCoord.y);
add(pieceLabel);
}
if( boardSetting[tileCoord.x][tileCoord.y] != null) {
// Retrieves the first letter of the class name
String pieceAbbrevation = boardSetting[tileCoord.x][tileCoord.y].getClass().getSimpleName().substring(0, 1);
// Retrieves the team name: UP or DOWN
String teamAbbreviation = boardSetting[tileCoord.x][tileCoord.y].getTeam().name();
// Short name is Knight is actually N, but the String above will retrieve K instead.
if(boardSetting[tileCoord.x][tileCoord.y].getClass().getSimpleName().equalsIgnoreCase("Knight")) {
pieceAbbrevation = "N";
}
try {
BufferedImage image = ImageIO.read(new File(pieceImagesPath + pieceAbbrevation + teamAbbreviation + ".png"));
JLabel pieceLabel = new JLabel(new ImageIcon(image));
//pieceButton.setBackground(highlightColor);
pieceLabel.setLocation(tileCoord.x, tileCoord.y);
add(pieceLabel);
} catch (IOException e) {
e.printStackTrace();
}
}
repaint();
revalidate();
}