package visualizationElements; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; /** * Represents a chess board. * @author MSchaefer * */ public class ChessBoard extends Table { private static final int START_X_POS = 10; private static final int START_Y_POS = 10; private static final int BORDER = 20; private boolean[][] board; private int size; private int cellWidth; private int cellHeight; private String marking; /** * Creates a new ChessBoard. * @param board The 2-dimensional array which represents the chess board. * @param width Width of the whole chess board. * @param height Height of the whole chess board. */ public ChessBoard(boolean[][] board, int width, int height, String marking){ super(null, board.length, board.length); if(board.length != board[0].length){ throw new IllegalArgumentException("The array/board must be N x N!"); } this.size = board.length; this.board = board; this.marking = marking; calculateCellHeigth(width, height, board.length); } /** * Calculates the height and width of a cell dependent of the height and width of the whole chess board and the number of cells. * @param width Width of the chess board. * @param height Height of the chess board. * @param size Number of cells in one row/column */ private void calculateCellHeigth(int width, int height, int size) { if(width <= height){ height = width; } this.cellWidth = (height - BORDER)/(size + 1); this.cellHeight = (height - BORDER)/(size + 1); if(cellWidth < cellHeight){ cellHeight = cellWidth; } else{ cellWidth = cellHeight; } } @Override public void draw(Graphics g) { int xpos = START_X_POS + cellWidth/2; int ypos = START_Y_POS; g.setFont(new Font("Arial", Font.BOLD, cellHeight/3)); // draw horizontal labeling for(int i = size; i > 0; i--){ ypos = ypos + cellWidth; g.drawString(String.valueOf(i), xpos, ypos + (cellHeight/2)); } ypos = START_Y_POS + cellWidth/3; xpos = START_X_POS; // Draw vertical labeling char name = 'A'; for(int i = size; i > 0; i--){ xpos = xpos + cellHeight; g.drawString(String.valueOf(name), xpos + (cellWidth/2), ypos + cellHeight/2); name++; } xpos = START_X_POS; ypos = START_Y_POS; g.setColor(Color.WHITE); for(int i = 0; i < board.length; i++){ xpos = xpos + cellWidth; ypos = START_Y_POS; for(int j = 0; j < board[i].length; j++){ ypos = ypos + cellWidth; g.fillRect(xpos, ypos, cellWidth, cellHeight); if(g.getColor() == Color.WHITE){ g.setColor(Color.BLACK); } else{ g.setColor(Color.WHITE); } if(board[j][i] == true){ g.setFont(new Font("Arial", Font.BOLD, cellHeight/2)); g.drawString(this.marking, xpos + (cellWidth/3), ypos + (2*cellHeight)/3); } } if(board.length %2 == 0){ if(g.getColor() == Color.WHITE){ g.setColor(Color.BLACK); } else{ g.setColor(Color.WHITE); } } } } /** * @param board the board to set */ public void setBoard(boolean[][] board) { this.board = board; } /** * @return the board */ public boolean[][] getBoard() { return board; } /** * @param size the size to set */ public void setSize(int size) { this.size = size; } /** * @return the size */ public int getSize() { return size; } /** * @return the width of a cell */ public int getCellWidth() { return cellWidth; } /** * @param cellWidth */ public void setCellWidth(int cellWidth) { this.cellWidth = cellWidth; } /** * @return the height of a cell */ public int getCellHeight() { return cellHeight; } /** * @param cellHeight */ public void setCellHeight(int cellHeight) { this.cellHeight = cellHeight; } /** * @return the marking */ public String getMarking() { return marking; } /** * @param marking The marking of the chess board. */ public void setMarking(String marking) { this.marking = marking; } }