/*Judy Yang * CS210 * 4/5/11 * * * This class provides an interface for the user to visualize the cell * grid. The user can click on a cell and change it from live to dead * or dead to live. The user can also use the next button to generate * the next generation with each click. There is also a start button * for if the user wants to continuously generate the subsequent * generations which uses a timer to keep track of the time between * each generation. There are drop down menus for choosing the speed * at which the user wants the next generation to be generated and for * choosing the seeds from which the future generations are * generated. There is also a stop button for when the user wants to * stop the automatic generation of next generations. */ import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; public class LifeGUI{ // size and width of the JFrame private static final int FRAME_WIDTH = 605; private static final int FRAME_LENGTH = 680; // size and width of JPanel holding drop list of seed options private static final int BP_LENGTH = 40; // the different speeds the timer could be set at private static final int SLOW = 1000; private static final int MEDIUM = 500; private static final int FAST = 100; // all the options for seeds private static String[] seeds = {"Clear", "Blinker", "Block", "Toad", "Beehive", "Beacon", "Acorn", "Gosper's Glider Gun", "Exploder", "Fpentomino"}; // all the options for speed private static String[] speedString = {"Slow", "Medium", "Fast"}; // the game of life this GUI uses private GameOfLife gol; // the JPanel on which the drawing of the cell grid // is done private CellGrid cg; // keeps track of time for generating // the next generation private Timer timer; // calls setUpFrame to set up the JFrame and all the // components of the GUI public LifeGUI(GameOfLife gol) { // sets the gol declared above as the gol sent in // the parameter this.gol = gol; // calls setUpFrame to make the JFrame, JPanel... setUpFrame(); } // creates the JFrame, the JComboBox, the JPanels holding seedChooser // and the drawingPanel and set up seedChooser as an action listener public void setUpFrame() { // creates a JFrame, sets the size and specifies to exit the // program when the exit button is clicked JFrame frame = new JFrame("Game of Life"); frame.setSize(FRAME_WIDTH, FRAME_LENGTH); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // JPanel for holding the JComboBox seedChooser // and set its size JPanel bottomPanel = new JPanel(); bottomPanel.setPreferredSize(new Dimension(0,BP_LENGTH)); // adds bottomPanel to the content pane of frame frame.getContentPane().add("South", bottomPanel); // a drop down list for selecting the seeds and sets // the default to "clear" JComboBox seedChooser = new JComboBox(seeds); seedChooser.setSelectedIndex(0); // adds seedChooser as an action listener seedChooser.addActionListener (new ActionListener() { public void actionPerformed (ActionEvent e) { JComboBox cb = (JComboBox)e.getSource(); String patternName = (String)cb.getSelectedItem(); // if the user chooses clear, call clear if (patternName == "Clear") { gol.clear(); cg.repaint(); } // if user chooses block, call block if (patternName == "Block") { gol.block(); cg.repaint(); } // if user chooses blinker, call blinker if (patternName == "Blinker") { gol.blinker(); cg.repaint(); } // if user chooses toad, call toad if (patternName == "Toad") { gol.toad(); cg.repaint(); } // if user chooses beehive, call beehive if (patternName == "Beehive") { gol.beehive(); cg.repaint(); } // if user chooses beacon, call beacon if (patternName == "Beacon") { gol.beacon(); cg.repaint(); } // if user chooses acorn, call acorn if (patternName == "Acorn") { gol.acorn(); cg.repaint(); } // if user chooses ggun, call ggun if (patternName == "Gosper's Glider Gun") { gol.ggGun(); cg.repaint(); } // if user chooses exploder, call exploder if (patternName == "Exploder") { gol.exploder(); cg.repaint(); } // if user chooses fpentomino, call fpentomino if (patternName == "Fpentomino") { gol.fpentomino(); cg.repaint(); } } }); // button for generating the results of the next // generation of cells JButton next = new JButton("Next"); // calls the nextGen and setNextGen methods on the // game if next button is clicked next.addActionListener (new ActionListener() { public void actionPerformed (ActionEvent e) { gol.nextGen(); cg.repaint(); } }); // add seedChooser and next button to the bottom JPanel bottomPanel.add(next); bottomPanel.add(seedChooser); // creates the DrawingPanel that holds the cell grid // and adds it to the content pane of the JFrame cg = new CellGrid(gol); frame.getContentPane().add(cg, BorderLayout.CENTER); // a drop down list for selecting the speed at which // the next generations are generated and sets the // default speed to slow and adds it to the bottom // panel JComboBox speedChooser = new JComboBox(speedString); speedChooser.setSelectedIndex(0); bottomPanel.add(speedChooser); // adds speedChooser as an action listener speedChooser.addActionListener (new ActionListener() { public void actionPerformed (ActionEvent e) { JComboBox cb = (JComboBox)e.getSource(); String patternName = (String)cb.getSelectedItem(); // sets the timer delay according to which speed the // user chooses if (patternName == "Slow") timer.setDelay(SLOW); else if (patternName == "Medium") timer.setDelay(MEDIUM); else if (patternName == "Fast") timer.setDelay(FAST); } }); // creates the timer and sets the initial delay speed to // SLOW, adds an action listener that calls the nextGen // method and calls repaint to show the next generation // of the game and sets the initial delay time timer = new Timer(SLOW, new ActionListener() { public void actionPerformed(ActionEvent e) { gol.nextGen(); cg.repaint(); } }); // a start button for the user to click on that // starts the timer JButton start = new JButton("Start"); bottomPanel.add(start); // calls the nextGen and setNextGen methods on the game // if start button is clicked start.addActionListener (new ActionListener() { public void actionPerformed (ActionEvent e) { timer.start(); } }); // a stop button for the user to click on // to stop the timer and to stop the continuous // generation of the next generation of cells JButton stop = new JButton("Stop"); bottomPanel.add(stop); // calls the nextGen and setNextGen methods on the game // if start button is clicked stop.addActionListener (new ActionListener() { public void actionPerformed (ActionEvent e) { timer.stop(); } }); // sets the frame to visible frame.setVisible(true); } public static void main(String[] args) { // creates a new GameOfLife and creates a new GUI // of it GameOfLife g = new GameOfLife(); new LifeGUI(g); } }