import java.awt.event.*; import javax.swing.*; import java.awt.*; /** * The GameGui will implement controls the operations from the DiceGame * class with a graphical user interface * * @author Matt Brickner * @version Feb 11, 2003 */ public class GameGui { DiceGame game; ButtonHandler buttonHandler; NewGameMenuHandler newGameMenuHandler; RulesMenuHandler rulesMenuHandler; AboutMenuHandler aboutMenuHandler; ExitHandler exitHandler; JLabel status, score, playerOne, playerTwo; Container main; JFrame topContainer; private JButton button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, roll, removeChips, endTurn; /** * The GameGui constructor initializes a top-level container and adds a menu * and other Swing components. Also initalizes handlers for components and * creates a DiceGame object. */ public GameGui() { game = new DiceGame(); buttonHandler = new ButtonHandler(); newGameMenuHandler = new NewGameMenuHandler(); rulesMenuHandler = new RulesMenuHandler(); aboutMenuHandler = new AboutMenuHandler(); exitHandler = new ExitHandler(); topContainer = new JFrame ("High Roller"); topContainer.setDefaultCloseOperation (WindowConstants.DISPOSE_ON_CLOSE); makeMenu(topContainer); main = new Container(); main = topContainer.getContentPane(); makeComponent(main); topContainer.pack(); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); Point center = ge.getCenterPoint(); center.translate (-topContainer.getWidth()/2, -topContainer.getHeight()/2); topContainer.setLocation (center); topContainer.setVisible (true); } /** * This method initializes menubar, menus, and menu items. * @param topContainer = top-level container */ private void makeMenu(JFrame topContainer) { JMenuBar menuBar; JMenu menu; JMenuItem menuItem; menuBar = new JMenuBar(); topContainer.setJMenuBar (menuBar); menu = new JMenu ("Session"); menu.setMnemonic ('s'); menuBar.add(menu); menuItem = new JMenuItem ("New Game"); menuItem.setMnemonic ('n'); menuItem.addActionListener (newGameMenuHandler); menu.add(menuItem); menuItem = new JMenuItem ("Exit"); /* menu item */ menuItem.setMnemonic ('e'); menuItem.addActionListener (exitHandler); menu.add (menuItem); menu = new JMenu ("Help"); menu.setMnemonic ('h'); menuBar.add(menu); menuItem = new JMenuItem ("Rules of Play"); menuItem.setMnemonic ('r'); menuItem.addActionListener (rulesMenuHandler); menu.add(menuItem); menuItem = new JMenuItem ("About"); menuItem.setMnemonic ('a'); menuItem.addActionListener (aboutMenuHandler); menu.add(menuItem); } /** * This method creates the graphical components and adds them to a container * @param c the container into which the components should be added */ private void makeComponent(Container c) { // Containment hierarchy: // JFrame // (content pane) // JPanel {BoxLayout()} // | JButton (button1) // | JButton (button2) // | JButton (button3) // | JButton (button4) // | JButton (button5) // JPanel {BoxLayout()} // | JButton (button6) // | JButton (button7) // | JButton (button8) // | JButton (button9) // | JButton (button10) // JPanel // | Jpanel // | | Dice (game.getFirstDie()) // | | Dice (game.getSecondDie()) // | Jpanel {BoxLayout()} // | | JButton (rollDice) // | | JButton (removeChips) // | | JButton (endTurn) // | Jpanel {BoxLayout()} // | | JLabel (game.getPlayerOneScore()) // | | JLabel (game.getPlayerTwoScore()) // | | JLabel (Current Score) // JLabel (game.getStatus()) // c.setLayout (new BoxLayout (c, BoxLayout.Y_AXIS)); JPanel panelOne = new JPanel(); panelOne.setAlignmentX (Component.LEFT_ALIGNMENT); c.add (panelOne); button1 = new JButton (new ImageIcon(getClass().getResource("images/1.gif"))); button1.addActionListener (buttonHandler); panelOne.add (button1); button2 = new JButton (new ImageIcon(getClass().getResource("images/2.gif"))); button2.addActionListener (buttonHandler); panelOne.add (button2); button3 = new JButton (new ImageIcon(getClass().getResource("images/3.gif"))); button3.addActionListener (buttonHandler); panelOne.add (button3); button4 = new JButton (new ImageIcon(getClass().getResource("images/4.gif"))); button4.addActionListener (buttonHandler); panelOne.add (button4); button5 = new JButton (new ImageIcon(getClass().getResource("images/5.gif"))); button5.addActionListener (buttonHandler); panelOne.add (button5); JPanel panelTwo = new JPanel(); panelTwo.setAlignmentX (Component.LEFT_ALIGNMENT); c.add (panelTwo); panelTwo.setAlignmentX (Component.LEFT_ALIGNMENT); button6 = new JButton (new ImageIcon(getClass().getResource("images/6.gif"))); button6.addActionListener (buttonHandler); panelTwo.add (button6); button7 = new JButton (new ImageIcon(getClass().getResource("images/7.gif"))); button7.addActionListener (buttonHandler); panelTwo.add (button7); button8 = new JButton (new ImageIcon(getClass().getResource("images/8.gif"))); button8.addActionListener (buttonHandler); panelTwo.add (button8); button9 = new JButton (new ImageIcon(getClass().getResource("images/9.gif"))); button9.addActionListener (buttonHandler); panelTwo.add (button9); button10 = new JButton (new ImageIcon(getClass().getResource("images/10.gif"))); button10.addActionListener (buttonHandler); panelTwo.add (button10); JPanel panelThree = new JPanel(); panelThree.setAlignmentX (Component.LEFT_ALIGNMENT); c.add (panelThree); panelThree.setLayout (new FlowLayout ()); JPanel left = new JPanel(); panelThree.add(left); panelThree.add (Box.createRigidArea (new Dimension(2,0))); left.add(game.getFirstDie()); left.add(game.getSecondDie()); JPanel middle = new JPanel(); middle.setLayout (new BoxLayout (middle, BoxLayout.Y_AXIS)); panelThree.add(middle); panelThree.add (Box.createRigidArea (new Dimension(7,0))); roll = new JButton (new ImageIcon(getClass().getResource("images/rolldice.gif"))); roll.addActionListener (buttonHandler); middle.add(roll); removeChips = new JButton (new ImageIcon(getClass().getResource("images/removechips.gif"))); removeChips.addActionListener (buttonHandler); middle.add(removeChips); endTurn = new JButton (new ImageIcon(getClass().getResource("images/turn.gif"))); endTurn.addActionListener (buttonHandler); middle.add(endTurn); JPanel right = new JPanel(); right.setLayout (new BoxLayout (right, BoxLayout.Y_AXIS)); panelThree.add(right); playerOne = new JLabel (game.getPlayerOneScore()); right.add (playerOne); playerTwo = new JLabel (game.getPlayerTwoScore()); right.add (playerTwo); score = new JLabel(game.getScoreString()); score.setFont (new Font ("TimesRoman", Font.BOLD, 43)); right.add(score); JLabel scoreLabel = new JLabel("Current Score"); right.add(scoreLabel); status = new JLabel (game.getStatus()); status.setAlignmentX (Component.LEFT_ALIGNMENT); c.add (status); } //----- MAIN METHOD -----\\ public static void main(String[] args) { GameGui player1 = new GameGui(); } /** * INNER class for Button Handler */ public class ButtonHandler implements ActionListener { /* identifies selected button and performs button-specific operations */ public void actionPerformed (ActionEvent e) { JButton b = (JButton) e.getSource(); if (b == roll) { game.rollDice(); status.setText("Select chips"); } else if (b == button1) { game.setSelection(0); status.setText(game.getStatus()); } else if (b == button2) { game.setSelection(1); status.setText(game.getStatus()); } else if (b == button3) { game.setSelection(2); status.setText(game.getStatus()); } else if (b == button4) { game.setSelection(3); status.setText(game.getStatus()); } else if (b == button5) { game.setSelection(4); status.setText(game.getStatus()); } else if (b == button6) { game.setSelection(5); status.setText(game.getStatus()); } else if (b == button7) { game.setSelection(6); status.setText(game.getStatus()); } else if (b == button8) { game.setSelection(7); status.setText(game.getStatus()); } else if (b == button9) { game.setSelection(8); status.setText(game.getStatus()); } else if (b == button10) { game.setSelection(9); status.setText(game.getStatus()); } else if (b == removeChips) { game.removeChips(); score.setText(game.getScoreString()); if (game.getChipStatus(0) == true) button1.setEnabled(false); if (game.getChipStatus(1) == true) button2.setEnabled(false); if (game.getChipStatus(2) == true) button3.setEnabled(false); if (game.getChipStatus(3) == true) button4.setEnabled(false); if (game.getChipStatus(4) == true) button5.setEnabled(false); if (game.getChipStatus(5) == true) button6.setEnabled(false); if (game.getChipStatus(6) == true) button7.setEnabled(false); if (game.getChipStatus(7) == true) button8.setEnabled(false); if (game.getChipStatus(8) == true) button9.setEnabled(false); if (game.getChipStatus(9) == true) button10.setEnabled(false); status.setText(game.getStatus()); } else if (b == endTurn) { game.endTurn(); playerOne.setText(game.getPlayerOneScore()); playerTwo.setText(game.getPlayerTwoScore()); status.setText(game.getStatus()); button1.setEnabled(true); button2.setEnabled(true); button3.setEnabled(true); button4.setEnabled(true); button5.setEnabled(true); button6.setEnabled(true); button7.setEnabled(true); button8.setEnabled(true); button9.setEnabled(true); button10.setEnabled(true); score.setText(game.getScoreString()); } } } /** * INNER class for Handling the "New Game" menu item. */ public class NewGameMenuHandler extends WindowAdapter implements ActionListener { /* calls newGame method from the Dicegame class */ public void actionPerformed (ActionEvent _) { game.newGame(); playerOne.setText(game.getPlayerOneScore()); playerTwo.setText(game.getPlayerTwoScore()); score.setText(game.getScoreString()); button1.setEnabled(true); button2.setEnabled(true); button3.setEnabled(true); button4.setEnabled(true); button5.setEnabled(true); button6.setEnabled(true); button7.setEnabled(true); button8.setEnabled(true); button9.setEnabled(true); button10.setEnabled(true); status.setText(game.getStatus()); } } /** * INNER class for Handling the "Rules of Play" menu item. */ public class RulesMenuHandler extends WindowAdapter implements ActionListener { /* displays a diologue box with game rules */ public void actionPerformed (ActionEvent _) { String rules = "HIGH ROLLER RULES OF PLAY\n\nA player starts a turn with all ten chips face up.\nIn each turn, a player repeatedly rolls the two\ndice and removes any chip(s) that match the sum on\nthe dice. For example, if the player rolls 9, he/she\nmust flip one of the following groups of available\nchips (9), (8,1), (7,2), (6,3), (6,2,1), (5,4),\n(5,3,1), etc. Rolling the dice again without flipping\nany chip is considered cheating. Once flipped, a chip\nbecomes unavailable. The player continues rolling and\nmatching until he gets a sum of dice that cannot\nmatch the available chips. The sum of values on the\nremaining chips is the players score for that turn\nand is added to his current total score. The players\nturn is over when theyclick the end turn button.\nThe next player now takes their turn. A round is\ncomplete when each person has taken a turn After five\nrounds, the game is over and the player with the\nlowest point is the winner.\n"; JOptionPane optionPane = new JOptionPane(); JOptionPane.showMessageDialog(optionPane, rules, "High Roller - Rules of Play", JOptionPane.PLAIN_MESSAGE); } } /** * INNER class for Handling the "About" menu item. */ public class AboutMenuHandler extends WindowAdapter implements ActionListener { /* displays a diologue box with program information */ public void actionPerformed (ActionEvent _) { JOptionPane frame = new JOptionPane(); JOptionPane.showMessageDialog(frame, "", "High Roller v1", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(getClass().getResource("images/title.gif"))); } } /** * INNER class for Exit Handler */ public class ExitHandler extends WindowAdapter implements ActionListener { /* exit through window close button */ public void windowClosing (WindowEvent _) { System.exit(0); } /* exit through exit menu item */ public void actionPerformed (ActionEvent _) { System.exit(0); } } }