import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Observable; import java.util.Observer; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; /** * This class represents a GUI for a basic rock-paper-scissors game. * @author Marty Stepp * @version CSE 331 Spring 2011, 5/2/2011 */ public class RockPaperScissorsGui { private JFrame frame; private JButton move1; private JButton move2; private JButton play; private Game game; /** * Constructs the GUI and displays it on the screen. */ public RockPaperScissorsGui() { game = Game.getInstance(); setupComponents(); handleEvents(); doLayout(); frame.setVisible(true); } // sets up graphical components in the window private void setupComponents() { frame = new JFrame("Rock-Paper-Scissors"); frame.setLocation(300, 100); frame.setSize(400, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); move1 = new JButton(new ImageIcon("blank.png")); move2 = new JButton(new ImageIcon("blank.png")); play = new JButton("Play"); } // attaches various listeners to handle events private void handleEvents() { ActionListener listener = new ButtonListener(); play.addActionListener(listener); Observer observer = new GameObserver(); game.addObserver(observer); } // sets up containers for layout in the window private void doLayout() { Container moves = new JPanel(new FlowLayout()); moves.add(move1); moves.add(move2); frame.add(moves); frame.add(play, BorderLayout.SOUTH); } // This observer responds to changes in the state of the TTT game // by updating the GUI to reflect the changes. private class GameObserver implements Observer { public void update(Observable arg0, Object arg1) { // update the weapon buttons String file1 = game.getWeapon1().toString().toLowerCase() + ".png"; String file2 = game.getWeapon2().toString().toLowerCase() + ".png"; move1.setIcon(new ImageIcon(file1)); move2.setIcon(new ImageIcon(file2)); // highlight the winner int winner = game.winner(); move1.setBackground(null); move2.setBackground(null); if (winner == 1) { move1.setBackground(Color.YELLOW); } else if (winner == 2) { move2.setBackground(Color.YELLOW); } } } // This listener responds to clicks on the "Play" button. private class ButtonListener implements ActionListener { /** * Called when the Play button is clicked. * Plays a new game round of tic-tac-toe. */ public void actionPerformed(ActionEvent event) { game.playRound(); } } }