Exemple #1
0
 /**
  * the paintComponent(Graphics g) method paints the view component. It begins by painting the
  * black and white squares, and then iterates throught the board to paint all the pieces on the
  * board.
  */
 @Override
 public void paintComponent(Graphics g) {
   Graphics2D g2 = (Graphics2D) g;
   boolean isBlack = false; // keeps track of whether a square should be painted black or white
   /*
    * This for loop cycles through an 8x8 grid, alternating black and white squares
    */
   for (int i = 0; i < 8; i++) {
     isBlack = !(isBlack);
     for (int j = 0; j < 8; j++) {
       isBlack = !(isBlack);
       Rectangle rect = new Rectangle(i * 62, j * 62, 62, 62);
       if (isBlack) {
         g2.setColor(Color.darkGray);
       } else {
         g2.setColor(Color.white);
       }
       g2.fill(rect);
     }
   }
   /*
    * This for loop cycles through the board and for any board square with a piece,
    * it paints draws the piece.
    */
   for (int i = 0; i < 8; i++) {
     for (int j = 0; j < 8; j++) {
       if (board.hasPiece(i, j)) { // perform draw action if piece exists on board
         board.getSquare(i, j).draw(g2);
       }
     }
   }
   // draws selected pieces on top to ensure they are on the top layer
   for (int i = 0; i < 8; i++) {
     for (int j = 0; j < 8; j++) {
       if (board.hasPiece(i, j)) {
         if (board.getSquare(i, j).isSelected()) {
           board.getSquare(i, j).draw(g2);
         }
       }
     }
   }
 }