Example #1
0
  private void drawCapturedPieces(Graphics g) {
    int offset = squareSize * 4 / 5;
    BufferedImage[] blackCaptured = imageMap.getCapturedImages(LocationImageMap.BLACK);
    int size = blackCaptured.length;

    int lineSize = 8;
    if (size <= lineSize) lineSize = size;
    for (int i = 0; i < lineSize; i++)
      g.drawImage(
          blackCaptured[i],
          boardSize + playerScreenMargin + offset * i,
          boardSize / 4 + playerScreenMargin,
          pieceSize,
          pieceSize,
          this);
    if (lineSize == 8) {
      int i = 0;
      for (int k = 8; k < size; k++) {
        g.drawImage(
            blackCaptured[k],
            boardSize + playerScreenMargin + offset * i,
            boardSize / 4 + playerScreenMargin + pieceSize,
            pieceSize,
            pieceSize,
            this);
        i++;
      }
    }

    // g.drawImage(starMark[0], boardSize + playerScreenMargin, boardSize / 2 + playerScreenMargin,
    // starMarkSize, starMarkSize, this);
    BufferedImage[] whiteCaptured = imageMap.getCapturedImages(LocationImageMap.WHITE);
    size = whiteCaptured.length;
    lineSize = 8;
    if (size <= lineSize) lineSize = size;
    for (int i = 0; i < lineSize; i++)
      g.drawImage(
          whiteCaptured[i],
          boardSize + playerScreenMargin + offset * i,
          boardSize * 3 / 4 + playerScreenMargin,
          pieceSize,
          pieceSize,
          this);
    if (lineSize == 8) {
      int i = 0;
      for (int k = 8; k < size; k++) {
        g.drawImage(
            whiteCaptured[k],
            boardSize + playerScreenMargin + offset * i,
            boardSize * 3 / 4 + playerScreenMargin + pieceSize,
            pieceSize,
            pieceSize,
            this);
        i++;
      }
    }
  }
Example #2
0
  /**
   * This function initiates a chess piece index to an image mapping. Each chess piece's image is
   * associated with an index with which the actual image could be obtained.
   *
   * @param matchScore an int array recording scores of the match. The WHITE player's score is in
   *     the index 0, the BLACK player's score is in the index 1.
   * @param isCustomGame true if the players are playing chess in custom mode, false if standard
   *     mode.
   */
  public void initImageMap(int[] matchScore, boolean isCustomGame) {
    turn = true;
    selected[0] = NONE;
    selected[1] = NONE;
    prevSelected[0] = NONE;
    prevSelected[1] = NONE;

    score = matchScore;
    imageMap.reset();
    imageMap.initLocations(isCustomGame);
  }
Example #3
0
  /**
   * This is the default constructor, which defines a panel used for a ChessFrame. It includes a
   * chess board and two player screens. The default size of each square of the chess board is 50
   * px. Both width and height of the piece's image is set to 80% of the size of the square. Each
   * piece placed on a square with a margin (10% of the the size of the square) to the top and to
   * the left. Each player will have his or her screen to the right of the board, the screeen for
   * player 1 (WHITE) placed at the top and the screen for player 2 (BLACK) placed at the bottom.
   * They both have width of 350 px by default and height matching the half of the size of the
   * board. The margin for the player screen is 20 px by default and font-size is 30.
   */
  public ChessPanel() {
    imageMap = new LocationImageMap();
    imageMap.initLocations(false);
    selected = new int[2];
    selected[0] = NONE;
    selected[1] = NONE;
    prevSelected = new int[2];
    prevSelected[0] = NONE;
    prevSelected[1] = NONE;
    usernameWhite = "";
    usernameBlack = "";
    score = new int[2];
    score[BLACK] = 0;
    score[WHITE] = 0;
    turn = true;

    squareSize = 50;
    pieceSize = squareSize * 4 / 5;
    pieceMargin = squareSize / 10;
    // A Chess board is 8 by 8.
    boardSize = 50 * 8;
    playerScreenWidth = 450;
    playerScreenMargin = 10;
    fontSize = 24;
    starMarkSize = fontSize * 5 / 4;
  }
Example #4
0
 public void selectAndMove(int[] coords) {
   prevSelected[0] = NONE;
   prevSelected[1] = NONE;
   selected[0] = NONE;
   selected[1] = NONE;
   imageMap.changeLocations(coords[0], coords[1], coords[2], coords[3]);
   turn = !turn;
 }
Example #5
0
  private void drawPieces(Graphics g) {

    // int[][] locations = imageMap.getLocations();
    for (int x = 0; x < 8; x++) {
      for (int y = 0; y < 8; y++) {
        BufferedImage pieceImage = imageMap.getBufferedImage(x, y);

        if (pieceImage != null)
          g.drawImage(
              pieceImage,
              squareSize * x + pieceMargin,
              squareSize * (7 - y) + pieceMargin,
              pieceSize,
              pieceSize,
              this);
      }
    }
  }
Example #6
0
 /**
  * This function changes the location of the image of a chess piece when there is a player input
  * for an undo.
  */
 public void undo(int fromX, int fromY, int toX, int toY) {
   imageMap.retrieveImageShift(fromX, fromY, toX, toY);
   turn = !turn;
 }
Example #7
0
 /**
  * This function changes the location of the image of a chess piece when there is a player input
  * for a move.
  */
 public void selectAndMove() {
   imageMap.changeLocations(prevSelected[0], 7 - prevSelected[1], selected[0], 7 - selected[1]);
   turn = !turn;
 }