I am writing a chess program, and I am in the step of creating the GUI part of the chess.
To do this
I made a BoardGUI class and a TileGUI class.
The BoardGUI initializes a GridLayout -> loops through the entire grid -> initializes a TileGUI object on each grid.
The TileGUI basically gets the information of the gameBoard -> paints the tile based on its position (either dark or light color) -> draws the Piece as and add it as a JButton object.
This basically initialized my chessboard GUI (Code written below).
My question is, when I try to highlight the movable tiles, I am trying to do :
Get the board information and the x, y position of the selected Piece as parameters -> loop through the entire board -> check if the current tile position is movable -> setBackground(color)
But When I do this, it only colors the tile of the current position of the Piece, not the tile of the movable position.
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();
}
}
And 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);
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);
}
public ArrayList<Piece> getMovablePositions(Piece[][] piece, int x, int y) {
ArrayList<Piece> movableList = new ArrayList<>();
for(int row = 0; row < NUM_OF_ROWS; row++) {
for(int col = 0; col < NUM_OF_COLS; col++) {
if(piece[x][y].canMove(piece, x, y, row, col)) {
movableList.add(piece[row][col]);
}
}
}
return movableList;
}
public void highlightValidMovement(ArrayList<Piece> movablePos) {
for(Piece pos : movablePos) {
TileGUI tilePanel = new TileGUI(boardImage, new Point(pos.getX(), pos.getX()));
setBackground(highlightColor);
add(tilePanel);
}
}
}
Codes that are not important is hidden.
This results in
instead of