Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import javax.swing.*;
- /**
- *
- * Square class is an inherited class of JButton representing squares on the
- * Checkers board. Each square is a JButton allowing the user to click and move
- * Checkers.
- *
- * @author
- * Date: 07/09/2014
- */
- public class BoardSquare extends JButton
- {
- static BoardSquare[][] boardSquares = new BoardSquare[8][8]; //array of boardSquares each of JButton type.
- Checker checkerOnSquare; //contains information about the checker on this square. null if no checker.
- private final boolean isRedSquare; //colour of square
- //images of red and black checkers, both kinged and unkinged
- private static final ImageIcon redKing = new ImageIcon("Images/redKingImage.png");
- private static final ImageIcon blackKing = new ImageIcon("Images/blackKingImage.png");
- private static final ImageIcon redChecker = new ImageIcon("Images/redCheckerImage.png");
- private static final ImageIcon blackChecker = new ImageIcon("Images/blackCheckerImage.png");
- /**
- * Single constructor for Square objects.
- *
- * @param isRed a boolean parameter determining attributes such as colour and direction of movement.
- * If isRed is false, square is assumed to be black.
- */
- public BoardSquare(boolean isRed, int row , int col)
- {
- super(); //takes on JButton attributes
- this.setOpaque(true); //minor detail
- this.setMargin(new Insets(0, 0, 0, 0)); //inset margin
- this.isRedSquare = isRed;
- if (isRed){
- this.setBackground(new Color(220, 43, 43)); //sets background of red squares
- }
- else{
- this.setBackground(new Color(40, 5, 5)); //sets background of black squares
- }
- }
- /**
- * Sets the board up at the start of the game.
- * Makes use of refresh() method.
- */
- public static void initialise()
- {
- for (int i = 0; i < boardSquares.length; i++)
- {
- for (int j = 0; j < boardSquares[i].length; j++)
- {
- if (i<=2 && (i+j)%2 == 0) //sets up checkers at far end of board
- {
- //Checker instantiation: first param - isRed, second param - isKing
- //Checker is obviously not king at start of game.
- boardSquares[j][i].checkerOnSquare = new Checker(true, false);
- }
- else if (i>=5 && (i+j)%2 == 0) //checkers at near end of board.
- {
- boardSquares[j][i].checkerOnSquare = new Checker(false, false);
- }
- }
- }
- GameWindow.timer.start(); //clock starts couting.
- refresh(); //see doc for refresh() method.
- }
- /**
- * Refreshes the board by checking what Checker object (can be null) is
- * contained by each boardSquare and appropriately sets that square's icon
- * appropriately. Note that each square is an object of JButton.
- */
- private static void refresh()
- {
- for (int i = 0; i < boardSquares.length; i++)
- {
- for (int j = 0; j < boardSquares[i].length; j++)
- {
- if(boardSquares[j][i].checkerOnSquare != null){ //this means there is no checker on the square.
- if (boardSquares[j][i].checkerOnSquare.isRed){
- if (boardSquares[j][i].checkerOnSquare.isKing){
- boardSquares[j][i].setIcon(resizeIcon(64, 64, redKing)); //red and king
- }
- else{
- boardSquares[j][i].setIcon(resizeIcon(64, 64, redChecker)); //red and not king.
- }
- }
- else if(!boardSquares[j][i].checkerOnSquare.isRed){
- if(boardSquares[j][i].checkerOnSquare.isKing){
- boardSquares[j][i].setIcon(resizeIcon(64, 64, blackKing)); //black and king.
- }
- else{
- boardSquares[j][i].setIcon(resizeIcon(64, 64, blackChecker)); //black and not king.
- }
- }
- }
- }
- }
- }
- /**
- * Helper method to refresh that resizes the checkers icons so that they fit
- * snugly within the board squares.
- *
- * @param width is the width of the icon.
- * @param height is the height of the icon.
- * @param icon is the icon to be resized to new width and height.
- * @return an ImageIcon similar to the parameter imageIcon, but resized.
- */
- private static ImageIcon resizeIcon(int width, int height, ImageIcon icon)
- {
- Image img = icon.getImage();
- Image newimg = img.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);
- icon = new ImageIcon(newimg);
- return icon;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement