Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * GUI class with Checkers main method. GameWindow is an inherited class of
- * JFrame.
- *
- * @author
- */
- import java.awt.*;
- import javax.swing.*;
- import javax.swing.border.*;
- public class GameWindow extends JFrame
- {
- private final JPanel playArea = new JPanel(new BorderLayout(3, 3));
- static JPanel board; //checkers board as a JPanel.
- static Timer timer;
- JButton newGame;
- JButton resign;
- JButton help;
- // private final JPanel sidebar = new JPanel()
- // {
- // @Override
- // public Dimension getPreferredSize()
- // {
- // return new Dimension(200, 100);
- // }
- // };
- public GameWindow()
- {
- super(); //takes on attributes of JFrame
- playArea.setBorder(new EmptyBorder(5, 5, 5, 5)); //unimportant
- //creates toolbar along top of JFrame.
- JToolBar tools = new JToolBar();
- tools.setFloatable(false); //fixed toolbar
- playArea.add(tools, BorderLayout.PAGE_START); //position at top
- tools.add(newGame = new JButton("New Game")); //New Game button initialises board.
- tools.add(resign = new JButton("Resign")); //Ends game.
- tools.addSeparator();
- tools.add(help = new JButton(" ? "));
- tools.addSeparator(); //unimportant
- tools.add(new JLabel("Game by Aidan de Nobrega - DNBAID001")); //signature
- tools.add(Box.createHorizontalGlue()); //affixes components created from here to right end of toolbar
- //counter creation
- JLabel clock = new JLabel("Count: 0"); //timer that starts when "New Game" is clicked
- tools.add(clock);
- MyListener clockEar = new MyListener(clock);
- timer = new Timer(1000, clockEar);
- tools.addSeparator();
- tools.addSeparator();
- tools.add(new JButton("Pause"));
- //end of toolbar creation.
- //creates checkers board
- board = new JPanel(new GridLayout(8, 8)) //8x8 board
- {
- @Override
- public Dimension getPreferredSize()
- {
- return new Dimension(600, 600); //fixes board to square shape.
- }
- };
- board.setBorder(new LineBorder(Color.BLACK));
- playArea.add(board, BorderLayout.CENTER); //fills area below toolbar
- //playArea.add(sidebar, BorderLayout.LINE_END);
- /**
- * Creates boardSquares each of which is an object of JButton.
- * Each square is added to boardSquares array in BoardSquare class.
- * When square is instantiated, it is either red or black.
- *
- * @see BoardSquare class.
- */
- for (int i = 0; i < BoardSquare.boardSquares.length; i++)
- {
- for (int j = 0; j < BoardSquare.boardSquares[i].length; j++)
- {
- //red and black squares alternate
- if ((i + j) % 2 == 0)
- {
- BoardSquare square = new BoardSquare(true, j, i); //parameter - isRed
- board.add(BoardSquare.boardSquares[j][i] = square);
- }
- else
- {
- BoardSquare square = new BoardSquare(false, j, i);
- board.add(BoardSquare.boardSquares[j][i] = square);
- }
- BoardSquare.boardSquares[j][i].add(new JLabel((j+1)+":"+(i+1)));
- }
- }
- }
- // public final JComponent getChessBoard()
- // {
- // return board;
- // }
- public static void main(String[] args)
- {
- GameWindow window = new GameWindow();
- JFrame frame = new JFrame("Checkers");
- frame.setResizable(false); //GameWindow is fixed size and shape.
- frame.add(window.playArea);
- frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- frame.setLocationByPlatform(true);
- frame.pack(); //playArea fills space in JFrame.
- frame.setVisible(true);
- BoardSquare.initialise(); //game initialised
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement