package edu.dartmouth.cs.kalah; /** * @author Scot Drysdale, 8/2011 * Holds the graphical representation of the Kalah game board. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import edu.dartmouth.cs.kalah.KalahGame; public class KalahFrame extends JFrame implements ActionListener { // Needed to eliminate a compiler warning. private static final long serialVersionUID = 1L; private static final Color BACKGROUND_COLOR = new Color(245,220,175); private static final Color ACTIVE_COLOR = Color.CYAN; private static final Color INACTIVE_COLOR = Color.BLACK; private static final Color HIGHLIGHT_COLOR = Color.RED; private JButton [] buttons; // Hold the pit and kalah buttons. public int moveChosen; // Will hold the move clicked on public KalahFrame() { Container boardPane; // Pane representing Kalah board JPanel northPanel, southPanel; // Hold the two sets of pit buttons // Note that the content pane uses the Border Layout manager. boardPane = getContentPane(); buttons = new JButton[KalahGame.NUMPITS]; boardPane.setBackground(BACKGROUND_COLOR); for (int i = 0; i < KalahGame.NUMPITS; i++) { buttons[i] = new JButton(Integer.toString(KalahGame.INITSTONES)); buttons[i].setForeground(INACTIVE_COLOR); buttons[i].addActionListener(this); } buttons[0].setText("0"); buttons[KalahGame.HALFPITS].setText("0"); boardPane.setLayout(new BorderLayout()); southPanel = new JPanel(); southPanel.setBackground(BACKGROUND_COLOR); for (int i = 1; i <= KalahGame.HALFPITS; i++) southPanel.add(buttons[i]); northPanel = new JPanel(); northPanel.setBackground(BACKGROUND_COLOR); for (int i = KalahGame.NUMPITS-1; i > KalahGame.HALFPITS; i--) northPanel.add(buttons[i]); boardPane.add(northPanel, BorderLayout.NORTH); boardPane.add(southPanel, BorderLayout.SOUTH); boardPane.add(buttons[0], BorderLayout.WEST); boardPane.add(buttons[KalahGame.HALFPITS], BorderLayout.EAST); } /** * Graphically displays the board * @param state current game state */ public void display (KalahGame state) { int [] board = state.getBoard(); for (int i = 0; i < KalahGame.NUMPITS; i++) { buttons[i].setText(Integer.toString(board[i])); } buttons[0].setForeground(INACTIVE_COLOR); buttons[KalahGame.HALFPITS].setForeground(INACTIVE_COLOR); // Make one half of the board the active color. if(state.getPlayerNum() == 0) { for(int i = 1; i < KalahGame.HALFPITS; i++) buttons[i].setForeground(ACTIVE_COLOR); for(int i = KalahGame.HALFPITS+1; i < KalahGame.NUMPITS; i++) buttons[i].setForeground(INACTIVE_COLOR); } else { for(int i = 1; i < KalahGame.HALFPITS; i++) buttons[i].setForeground(INACTIVE_COLOR); for(int i = KalahGame.HALFPITS+1; i < KalahGame.NUMPITS; i++) buttons[i].setForeground(ACTIVE_COLOR); } if(state.getPitToHightlight() >= 0) buttons[state.getPitToHightlight()].setForeground(HIGHLIGHT_COLOR); repaint(); } /** * Handles clicking on the buttons to get moves * @param event the event that tiggered this listener */ public void actionPerformed(ActionEvent event) { Object source = event.getSource(); int buttonNum; // Look for button clicked for(buttonNum = 0; source != buttons[buttonNum] && buttonNum < KalahGame.NUMPITS; buttonNum++); // Only want to register clicks on the user's buttons, and only // if the game is waiting for a move. if(buttonNum < KalahGame.NUMPITS && moveChosen == -1) moveChosen = buttonNum; } }