//Extends the chessBoard with drawing the coins on the board, setting up the coin images and handling mouse events import java.awt.*; import java.awt.event.*; public class draughtsCanvas extends draughtsChessBoard implements MouseListener { private Image[] imgCoin = new Image[4]; //0 = red, 1 = blue, 2 = redKing, 3 = blueKing private draughtsMoveCheck moveCheck = new draughtsMoveCheck(); public draughtsCanvas () { this.addMouseListener(this); } public void resetBoard () { moveCheck.reset(); repaint(); } public void setupCoins (Image coinRed, Image coinBlue, Image coinRedKing, Image coinBlueKing) { imgCoin[0] = coinRed; imgCoin[1] = coinBlue; imgCoin[2] = coinRedKing; imgCoin[3] = coinBlueKing; moveCheck.reset(); } protected void drawExtra (Graphics g) //Extended from chessBoard to draw extra features like player pieces { if (moveCheck.isTileSelected()) { g.setColor(new Color(31,200,75)); //Bright green g.drawRect((moveCheck.getSelectedColumn() + 1) * 50, (moveCheck.getSelectedRow() + 1) * 50, 50, 50); } drawCoinMatrix(g); g.setColor(new Color(0,0,0)); g.drawString(moveCheck.getStatusMsg(), 50, 475); //Write the msg on the south border } private void drawCoinMatrix (Graphics g) //Draws all the coins on the board { int cellContents = 0; for (int row = 0; row < 8; row++) { for (int column = 0; column < 8; column++) { cellContents = moveCheck.getCell(row, column); if (cellContents != 0 && cellContents != 5) //If cell is not empty or an ilegal cell (can't move on) { drawCoin(g,((column + 1) * 50),((row + 1) * 50), cellContents); } } } } private void drawCoin (Graphics g, int startX, int startY, int currentPlayer) //Draw each indvidual coin { g.drawImage(imgCoin[currentPlayer - 1], startX, startY, this); } private int findWhichTileSelected (int coor) { for (int i = 0; i < 8; i++) { if (coor > (i + 1) * 50 && coor < (i + 2) * 50) { return i; } } return -1; } public void mouseClicked (MouseEvent e) { } public void mouseEntered (MouseEvent e) { } public void mouseExited (MouseEvent e) { } public void mousePressed (MouseEvent e) { if (!moveCheck.isWinner()) //If the game hasn't been won { int row = findWhichTileSelected(e.getY()); int column = findWhichTileSelected(e.getX()); if (row != -1 && column != -1) { moveCheck.processMouseClick (row, column); repaint(); } } } public void mouseReleased (MouseEvent e) { } }