import javax.swing.*; import java.awt.*; import javax.swing.event.*; import java.awt.event.*; /** * This class provides a graphical model of a board game. * The class creates a rectangular panel of clickable squares, * of type SmartSquare. If a square is clicked by the user, a * callback method is invoked upon the corresponding SmartSquare instance. * The class is intended to be used as a basis for tile based games. * @author joe finney */ public class GameBoard extends JFrame implements MouseListener { private JPanel boardPanel = new JPanel(); private int boardHeight; private int boardWidth; private GameSquare[][] board; /** * Create a new game board of the given size. * As soon as an instance of this class is created, it is visualized * on the screen. * * @param title the name printed in the window bar. * @param width the width of the game area, in squares. * @param height the height of the game area, in squares. */ public GameBoard(String title, int width, int height) { super(); this.boardWidth = width; this.boardHeight = height; // Create game state this.board = new GameSquare[width][height]; // Set up window setTitle(title); setSize(20+width*20,20+height*20); setContentPane(boardPanel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); boardPanel.setLayout(new GridLayout(height,width)); for (int y = 0; y= boardWidth || y < 0 || y >= boardHeight) return null; return board[x][y]; } public void mouseClicked(MouseEvent e) { GameSquare b = (GameSquare)e.getSource(); if (SwingUtilities.isRightMouseButton(e)) b.rightClicked(); else b.clicked(); } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } /* public void actionPerformed(ActionEvent e) { // The button that has been pressed. GameSquare b = (GameSquare)e.getSource(); b.clicked(); } */ }