Advertisement
aidandeno

Checkers - GameWindow

Sep 6th, 2014
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.04 KB | None | 0 0
  1. /**
  2.  * GUI class with Checkers main method. GameWindow is an inherited class of
  3.  * JFrame.
  4.  *
  5.  * @author
  6.  */
  7.  
  8. import java.awt.*;
  9. import javax.swing.*;
  10. import javax.swing.border.*;
  11.  
  12. public class GameWindow extends JFrame
  13. {
  14.  
  15.     private final JPanel playArea = new JPanel(new BorderLayout(3, 3));
  16.     static JPanel board; //checkers board as a JPanel.
  17.     static Timer timer;
  18.     JButton newGame;
  19.     JButton resign;
  20.     JButton help;
  21. //    private final JPanel sidebar = new JPanel()
  22. //    {
  23. //        @Override
  24. //        public Dimension getPreferredSize()
  25. //        {
  26. //            return new Dimension(200, 100);
  27. //        }
  28. //    };
  29.  
  30.     public GameWindow()
  31.     {  
  32.         super(); //takes on attributes of JFrame
  33.         playArea.setBorder(new EmptyBorder(5, 5, 5, 5)); //unimportant
  34.        
  35.         //creates toolbar along top of JFrame.
  36.         JToolBar tools = new JToolBar();
  37.  
  38.         tools.setFloatable(false); //fixed toolbar
  39.         playArea.add(tools, BorderLayout.PAGE_START); //position at top
  40.         tools.add(newGame = new JButton("New Game")); //New Game button initialises board.
  41.         tools.add(resign = new JButton("Resign")); //Ends game.
  42.         tools.addSeparator();
  43.         tools.add(help = new JButton(" ? "));
  44.         tools.addSeparator(); //unimportant
  45.         tools.add(new JLabel("Game by Aidan de Nobrega - DNBAID001")); //signature
  46.         tools.add(Box.createHorizontalGlue()); //affixes components created from here to right end of toolbar
  47.  
  48.         //counter creation
  49.         JLabel clock = new JLabel("Count: 0"); //timer that starts when "New Game" is clicked
  50.         tools.add(clock);
  51.         MyListener clockEar = new MyListener(clock);
  52.         timer = new Timer(1000, clockEar);
  53.  
  54.         tools.addSeparator();
  55.         tools.addSeparator();
  56.         tools.add(new JButton("Pause"));
  57.         //end of toolbar creation.
  58.        
  59.         //creates checkers board
  60.         board = new JPanel(new GridLayout(8, 8)) //8x8 board
  61.         {
  62.             @Override
  63.             public Dimension getPreferredSize()
  64.             {
  65.                 return new Dimension(600, 600); //fixes board to square shape.
  66.             }
  67.         };
  68.         board.setBorder(new LineBorder(Color.BLACK));
  69.         playArea.add(board, BorderLayout.CENTER); //fills area below toolbar
  70.         //playArea.add(sidebar, BorderLayout.LINE_END);
  71.  
  72.         /**
  73.          * Creates boardSquares each of which is an object of JButton.
  74.          * Each square is added to boardSquares array in BoardSquare class.
  75.          * When square is instantiated, it is either red or black.
  76.          *
  77.          * @see BoardSquare class.
  78.          */
  79.         for (int i = 0; i < BoardSquare.boardSquares.length; i++)
  80.         {
  81.             for (int j = 0; j < BoardSquare.boardSquares[i].length; j++)
  82.             {  
  83.                 //red and black squares alternate
  84.                 if ((i + j) % 2 == 0)
  85.                 {
  86.                     BoardSquare square = new BoardSquare(true, j, i); //parameter - isRed
  87.                     board.add(BoardSquare.boardSquares[j][i] = square);                
  88.                 }
  89.                 else
  90.                 {  
  91.                     BoardSquare square = new BoardSquare(false, j, i);
  92.                     board.add(BoardSquare.boardSquares[j][i] = square);
  93.                 }
  94.                 BoardSquare.boardSquares[j][i].add(new JLabel((j+1)+":"+(i+1)));
  95.             }
  96.         }
  97.     }
  98.  
  99. //    public final JComponent getChessBoard()
  100. //    {
  101. //        return board;
  102. //    }
  103.  
  104.     public static void main(String[] args)
  105.     {
  106.  
  107.         GameWindow window = new GameWindow();
  108.  
  109.         JFrame frame = new JFrame("Checkers");
  110.         frame.setResizable(false); //GameWindow is fixed size and shape.
  111.         frame.add(window.playArea);
  112.         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  113.         frame.setLocationByPlatform(true);
  114.         frame.pack(); //playArea fills space in JFrame.
  115.         frame.setVisible(true);
  116.         BoardSquare.initialise(); //game initialised
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement