* GOLController.java * * Created on 17. oktober 2006, 18:17 * */ package gameoflife; import java.awt.event.*; import javax.swing.*; public class GOLController { private final Board board; private final GameOfLifeGUI gui; private boolean[][] currentBoard; private Timer timer; /** Creates a new instance of GOLController */ public GOLController(Board board, GameOfLifeGUI gui) { this.board = board; this.gui = gui; currentBoard = board.getCurrentBoard(); gui.addGridListener(new GridListener()); gui.addStartButtonListener(new startButtonListener()); gui.addStepButtonListener(new stepButtonListener()); gui.addStopButtonListener(new stopButtonListener()); } public class startButtonListener implements ActionListener { public void actionPerformed(ActionEvent e){ } } public class stepButtonListener implements ActionListener { public void actionPerformed(ActionEvent e){ board.calculateNextBoard(); gui.calculateNextBoard(); } } public class stopButtonListener implements ActionListener { public void actionPerformed(ActionEvent e){ } } public class GridListener implements ActionListener { public void actionPerformed(ActionEvent e){ String[] identifier = e.getActionCommand().split(":"); String row = identifier[0]; String column = identifier[1]; if (!board.aliveCell(Integer.parseInt(row), Integer.parseInt(column))) { board.wakeCell(Integer.parseInt(row), Integer.parseInt(column)); gui.wakeCell(Integer.parseInt(row), Integer.parseInt(column)); } else { board.buryCell(Integer.parseInt(row), Integer.parseInt(column)); gui.buryCell(Integer.parseInt(row), Integer.parseInt(column)); } } } } /* * GameOfLifeGUI.java * * Created on 16. oktober 2006, 13:14 * */ package gameoflife; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class GameOfLifeGUI extends JFrame { private JFrame frame; private JPanel mainPanel, gridPanel; private JButton startButton, stepButton, stopButton; private JButton gridButton[][] = new JButton[15][15]; // The model-object public Board board; private boolean[][] currentBoard; /** Creates a new instance of GameOfLifeGUI */ public GameOfLifeGUI(Board board) { this.board = board; currentBoard = board.getCurrentBoard(); frame = new JFrame("Game of Life"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(true); // Create an instance of the main panel mainPanel = new JPanel(); mainPanel.setLayout(new FlowLayout()); // Create an instance of the grid panel housing the actual game gridPanel = new JPanel(); gridPanel.setLayout(new GridLayout(15, 15)); this.addGridButtons(); // Create an instance of the buttons needed startButton = new JButton("Start"); stepButton = new JButton("Step"); stopButton = new JButton("Stop"); mainPanel.add(gridPanel); mainPanel.add(startButton); mainPanel.add(stepButton); mainPanel.add(stopButton); // Place the main panel on the frame frame.getContentPane().add(mainPanel); frame.setSize(550, 230); frame.setResizable(false); frame.setVisible(true); } public void addGridButtons(){ for (int i=0; i<14; i++) { for (int j=0; j<14; j++) { gridButton[i][j] = new JButton(); gridPanel.add(gridButton[i][j]); gridButton[i][j].setBackground(Color.LIGHT_GRAY); gridButton[i][j].setSize(2,2); gridButton[i][j].setActionCommand(i + ":" + j); } } } public void addStartButtonListener (ActionListener startal) { startButton.addActionListener(startal); } public void addStepButtonListener(ActionListener stepal) { stepButton.addActionListener(stepal); } public void addStopButtonListener(ActionListener stopal) { stopButton.addActionListener(stopal); } public void addGridListener(ActionListener gal){ for (int i=0; i<14; i++){ for (int j=0; j<14; j++) { gridButton[i][j].addActionListener(gal); } } } public void wakeCell (int row, int column) { gridButton[row][column].setBackground(Color.GREEN); } public void buryCell (int row, int column) { gridButton[row][column].setBackground(Color.LIGHT_GRAY); } public void calculateNextBoard(){ for (int i=0; i<14; i++){ for (int j=0; j<14; j++){ gridButton[i][j].setBackground(Color.LIGHT_GRAY); if (this.currentBoard[i][j] == true){ if (!gridButton[i][j].getBackground().equals(Color.GREEN)){ gridButton[i][j].setBackground(Color.GREEN); } } } } } }