package example10_1_BlocksMania; import javax.swing.*; import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Random; /** * Class that manages the GUI for Barnyard Simon. *

Barnyard Simon is a game similar to the electronic Simon memory game * marketed by the Milton Bradley Company. *

The game has four colored buttons (red, green, blue, and yellow). The * buttons are activated in a randomly chosen sequence. The player must then * mimic the sequence in order to continue playing the game. The sequence begins * with just one button and increases in length by one each time the player * successfully mimics the sequence. *

Traditionally, the activation of each button results in the button being * lit and a tone (unique for each button) being played for a short amount of time. * Barnyard Simon differs in that the sounds associated with each button are * barnyard animal sounds. * @author FIX * @version 2010-01-10 * @created 15-Jan-2014 6:47:12 PM */ public class BlocksMania extends JFrame { private GameBoard board; private final JPanel boardSpot; public static void main(String[] args){ new BlocksMania(); } /** * This class listens solely for game play buttons to be pressed. Once pressed, * this class determines which button was pressed and then advances game play * appropriately. * @author FIX * @version 2010-01-10 * @created 15-Jan-2014 6:47:12 PM */ public class HandleGameButton implements ActionListener { /** * Handles an action event caused by one of the four colored buttons in Barnyard * Simon. *

The method first determines if the correct button was pressed. *

If not, the game has ended and the method must ensure that the high score * is updated (if necessary) and resets the label on the start button and enables * it. *

If the correct button is pressed, then the button is activated and the * current count of how many buttons in the sequence the player has pressed * correctly must be incremented. If the player has pressed all of the buttons in * the sequence correctly, the method must ensure that the sequence with one * additional entry be played. * * @param event */ @Override public void actionPerformed(ActionEvent event){ } } /** * This class listens solely to the start button component of the GUI. * @author FIX * @version 2010-01-10 * @created 15-Jan-2014 6:47:12 PM */ public class StartGame implements ActionListener { /** * Initializes and starts a game of Barnyard Simon. *

This method disables the start button and sets the text to "Playing." It * also creates a new sequence and calls playSequence to initiate game play. * * @param event */ @Override public void actionPerformed(ActionEvent event){ } } /** * GUI Component used to initiate a new game. */ private JButton btnStart; // /** // * The buttons used for game play. // */ // private Tile buttons[]; /** * The player's current location in the sequence. */ private int countCorrect; /** * Height of the window */ private static final int HEIGHT = 600; /** * Width of the window */ private static final int WIDTH = 600; /** * Highest score obtained during this running of the program. */ private int highScore = 0; /** * A random number generator used to select the sequence in which the buttons are * activated. */ private Random rGen; /** * Text area used to report the score and high score. */ private JTextArea scoreReport; /** * The sequence in which the buttons are activated. */ private List sequence; /** * Auto-generated serial version ID */ private static final long serialVersionUID = -8930955432889780211L; // Only used to identify code hard-coded to the four-button version. // Don't use elsewhere. // TODO: Better way to enforce/document? private static final int NUM_BUTTONS = 4; /** * Constructor. Creates game buttons, initializes attributes and the GUI layout. */ public BlocksMania(){ rGen = new Random(); sequence = new ArrayList(); setTitle("Barnyard Simon (yoder)"); setSize(WIDTH, HEIGHT); setLayout(new BorderLayout()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); btnStart = new JButton("Start!"); btnStart.addActionListener(new StartGame()); add(btnStart, BorderLayout.NORTH); boardSpot = new JPanel(); boardSpot.setLayout(new BorderLayout()); // So board grows to fill boardSpot add(boardSpot, BorderLayout.CENTER); createGameBoard("level.txt"); scoreReport = new JTextArea(); scoreReport.setEnabled(false); scoreReport.setDisabledTextColor(Color.BLACK); // TODO: How to use text color instead? add(scoreReport, BorderLayout.SOUTH); createMenus(); setVisible(true); } private void createMenus() { JMenuBar menuBar = new JMenuBar(); JMenu menu; menu = new JMenu("File"); JMenuItem item; item = new JMenuItem("Load level"); item.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { openLevel(); } }); menu.add(item); item = new JMenuItem("Save level"); item.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { saveLevel(); } }); menu.add(item); menuBar.add(menu); setJMenuBar(menuBar); } private void saveLevel() { JFileChooser chooser = new JFileChooser("."); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); chooser.showSaveDialog(null); File file = chooser.getSelectedFile(); board.writeLevel(file); } private void openLevel() { JFileChooser chooser = new JFileChooser("."); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); chooser.showOpenDialog(null); File file = chooser.getSelectedFile(); createGameBoard(file); } private void createGameBoard(String filename) { createGameBoard(new File(filename)); } /** * Creates the game board. Should only be called once. * @return the board created so it can be added right there. */ private void createGameBoard(File file) { boardSpot.removeAll(); // Get rid of old board if present. board = new GameBoard(file); boardSpot.add(board); // TODO: Best place for this? validate(); // Needed so that repaint will work. repaint(); } }