132 lines
3.3 KiB
Java
132 lines
3.3 KiB
Java
package dev.kske.chess.ui;
|
|
|
|
import java.awt.Color;
|
|
import java.awt.Dimension;
|
|
import java.awt.Graphics;
|
|
import java.awt.event.ComponentAdapter;
|
|
import java.awt.event.ComponentEvent;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import javax.swing.JPanel;
|
|
|
|
import dev.kske.chess.board.Board;
|
|
import dev.kske.chess.board.Move;
|
|
|
|
/**
|
|
* Project: <strong>Chess</strong><br>
|
|
* File: <strong>BoardPanel.java</strong><br>
|
|
* Created: <strong>01.07.2019</strong><br>
|
|
* Author: <strong>Kai S. K. Engelbart</strong><br>
|
|
* <br>
|
|
* A square panel for rendering the chess board. To work correctly,
|
|
* this must be added to a parent component that allows the child to decide the
|
|
* size.
|
|
*/
|
|
public class BoardPanel extends JPanel {
|
|
|
|
private static final long serialVersionUID = 6771148331334310216L;
|
|
|
|
private int tileSize;
|
|
private Board board;
|
|
|
|
private List<Move> displayMoves;
|
|
|
|
public BoardPanel(Board board) {
|
|
this();
|
|
setBoard(board);
|
|
}
|
|
|
|
public BoardPanel() {
|
|
displayMoves = new ArrayList<>();
|
|
|
|
/*
|
|
* Add a component listener for adjusting the tile size on resizing.
|
|
* The size of the board is assumed to be 8x8, as well as the both the board and
|
|
* the tiles being square.
|
|
*/
|
|
addComponentListener(new ComponentAdapter() {
|
|
|
|
@Override
|
|
public void componentResized(ComponentEvent e) {
|
|
tileSize = getWidth() / 8;
|
|
TextureUtil.scalePieceTextures(tileSize);
|
|
}
|
|
});
|
|
|
|
setSize(getPreferredSize());
|
|
}
|
|
|
|
@Override
|
|
protected void paintComponent(Graphics g) {
|
|
super.paintComponent(g);
|
|
|
|
// Draw the board
|
|
g.setColor(Color.white);
|
|
for (int i = 0; i < 8; i++)
|
|
for (int j = 0; j < 8; j++) {
|
|
if (j > 0) g.setColor(g.getColor().equals(Color.white) ? Color.lightGray : Color.white);
|
|
g.fillRect(tileSize * i, tileSize * j, tileSize, tileSize);
|
|
}
|
|
|
|
// Draw the pieces if a board is present
|
|
if (board != null) for (int i = 0; i < 8; i++)
|
|
for (int j = 0; j < 8; j++)
|
|
if (board.getBoardArr()[i][j] != null) g.drawImage(TextureUtil
|
|
.getPieceTexture(board.getBoardArr()[i][j]), i * tileSize, j * tileSize, this);
|
|
|
|
// Draw possible moves if a piece was selected
|
|
if (!displayMoves.isEmpty()) {
|
|
g.setColor(Color.green);
|
|
int radius = tileSize / 4;
|
|
for (Move move : displayMoves)
|
|
g.fillOval(move.dest.x * tileSize + tileSize / 2 - radius / 2,
|
|
move.dest.y * tileSize + tileSize / 2 - radius / 2,
|
|
radius,
|
|
radius);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Displays move destinations on the board.
|
|
*
|
|
* @param moves The moves to display
|
|
*/
|
|
public void displayMoves(List<Move> moves) {
|
|
displayMoves.clear();
|
|
displayMoves.addAll(moves);
|
|
repaint();
|
|
}
|
|
|
|
/**
|
|
* Clears all display moves.
|
|
*/
|
|
public void clearDisplayMoves() {
|
|
displayMoves.clear();
|
|
repaint();
|
|
}
|
|
|
|
/**
|
|
* Reverts the board to its initial state.
|
|
*/
|
|
public void reset() {
|
|
board.initializeDefaultPositions();
|
|
repaint();
|
|
}
|
|
|
|
@Override
|
|
public Dimension getMinimumSize() { return getPreferredSize(); }
|
|
|
|
@Override
|
|
public Dimension getMaximumSize() { return getPreferredSize(); }
|
|
|
|
@Override
|
|
public Dimension getPreferredSize() { return new Dimension(480, 480); }
|
|
|
|
public int getTileSize() { return tileSize; }
|
|
|
|
public Board getBoard() { return board; }
|
|
|
|
public void setBoard(Board board) { this.board = board; }
|
|
}
|