Advertisement
aidandeno

Checkers - BoardSquare

Sep 8th, 2014
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.91 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3.  
  4. /**
  5. *
  6. * Square class is an inherited class of JButton representing squares on the
  7. * Checkers board. Each square is a JButton allowing the user to click and move
  8. * Checkers.
  9. *
  10. * @author
  11. * Date: 07/09/2014
  12. */
  13.  
  14. public class BoardSquare extends JButton
  15. {
  16.     static BoardSquare[][] boardSquares = new BoardSquare[8][8]; //array of boardSquares each of JButton type.
  17.     Checker checkerOnSquare; //contains information about the checker on this square. null if no checker.
  18.     private final boolean isRedSquare; //colour of square
  19.    
  20.     //images of red and black checkers, both kinged and unkinged
  21.     private static final ImageIcon redKing = new ImageIcon("Images/redKingImage.png");
  22.     private static final ImageIcon blackKing = new ImageIcon("Images/blackKingImage.png");
  23.     private static final ImageIcon redChecker = new ImageIcon("Images/redCheckerImage.png");
  24.     private static final ImageIcon blackChecker = new ImageIcon("Images/blackCheckerImage.png");
  25.    
  26.     /**
  27.      * Single constructor for Square objects.
  28.      *
  29.      * @param isRed a boolean parameter determining attributes such as colour and direction of movement.
  30.      * If isRed is false, square is assumed to be black.
  31.      */
  32.     public BoardSquare(boolean isRed, int row , int col)
  33.     {  
  34.         super(); //takes on JButton attributes
  35.         this.setOpaque(true); //minor detail
  36.         this.setMargin(new Insets(0, 0, 0, 0)); //inset margin
  37.         this.isRedSquare = isRed;
  38.         if (isRed){
  39.             this.setBackground(new Color(220, 43, 43)); //sets background of red squares
  40.         }
  41.         else{
  42.             this.setBackground(new Color(40, 5, 5)); //sets background of black squares
  43.         }
  44.     }
  45.    
  46.     /**
  47.      * Sets the board up at the start of the game.
  48.      * Makes use of refresh() method.
  49.      */
  50.     public static void initialise()
  51.     {
  52.         for (int i = 0; i < boardSquares.length; i++)
  53.         {
  54.             for (int j = 0; j < boardSquares[i].length; j++)
  55.             {
  56.                 if (i<=2 && (i+j)%2 == 0) //sets up checkers at far end of board
  57.                 {  
  58.                     //Checker instantiation: first param - isRed, second param - isKing
  59.                     //Checker is obviously not king at start of game.
  60.                     boardSquares[j][i].checkerOnSquare = new Checker(true, false);
  61.                 }
  62.                 else if (i>=5 && (i+j)%2 == 0) //checkers at near end of board.
  63.                 {
  64.                     boardSquares[j][i].checkerOnSquare = new Checker(false, false);
  65.                 }
  66.             }
  67.         }
  68.         GameWindow.timer.start(); //clock starts couting.
  69.         refresh(); //see doc for refresh() method.
  70.     }
  71.    
  72.     /**
  73.      * Refreshes the board by checking what Checker object (can be null) is
  74.      * contained by each boardSquare and appropriately sets that square's icon
  75.      * appropriately. Note that each square is an object of JButton.
  76.      */
  77.     private static void refresh()
  78.     {
  79.         for (int i = 0; i < boardSquares.length; i++)
  80.         {
  81.             for (int j = 0; j < boardSquares[i].length; j++)
  82.             {  
  83.                 if(boardSquares[j][i].checkerOnSquare != null){ //this means there is no checker on the square.
  84.                     if (boardSquares[j][i].checkerOnSquare.isRed){
  85.                         if (boardSquares[j][i].checkerOnSquare.isKing){
  86.                             boardSquares[j][i].setIcon(resizeIcon(64, 64, redKing)); //red and king
  87.                         }
  88.                         else{
  89.                         boardSquares[j][i].setIcon(resizeIcon(64, 64, redChecker)); //red and not king.
  90.                         }
  91.                     }
  92.                     else if(!boardSquares[j][i].checkerOnSquare.isRed){
  93.                         if(boardSquares[j][i].checkerOnSquare.isKing){
  94.                             boardSquares[j][i].setIcon(resizeIcon(64, 64, blackKing)); //black and king.
  95.                         }
  96.                         else{
  97.                             boardSquares[j][i].setIcon(resizeIcon(64, 64, blackChecker)); //black and not king.
  98.                         }
  99.                     }
  100.                 }
  101.             }
  102.         }
  103.     }
  104.    
  105.     /**
  106.      * Helper method to refresh that resizes the checkers icons so that they fit
  107.      * snugly within the board squares.
  108.      *
  109.      * @param width is the width of the icon.
  110.      * @param height is the height of the icon.
  111.      * @param icon is the icon to be resized to new width and height.
  112.      * @return an ImageIcon similar to the parameter imageIcon, but resized.
  113.      */
  114.     private static ImageIcon resizeIcon(int width, int height, ImageIcon icon)
  115.     {
  116.         Image img = icon.getImage();
  117.         Image newimg = img.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);
  118.         icon = new ImageIcon(newimg);  
  119.         return icon;
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement