Ok, I'm programming my own Conway's Game Of Life Simulator. Right now I just want to get the basic program down before adding a bunch of horns a whistles. I'm using the acm library. I have one class extending JPanel that puts up the buttons and controls for the other class that extends GCanvas that displays the grid. My goal right now is to be able to press "Start" and have the grid progress in their generations until the "Stop" button is pressed. Following is my code, any ideas?
import javax.swing.*; import java.awt.*; @SuppressWarnings ("serial") public class GameOfLifeApp extends JFrame { // constructor public GameOfLifeApp (String title, String fname) { // calls superclass JFrame to create window super(title); this.setSize(800, 600); // create two panels and add them to the JFrame LifePanel panel = new LifePanel(fname); ControlPanel controlPanel = new ControlPanel(panel); this.add(panel, BorderLayout.CENTER); this.add(controlPanel, BorderLayout.PAGE_END); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } //main method public static void main (String [] args) { new GameOfLifeApp("Game of Life", "src/exploder.txt"); } }
import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; @SuppressWarnings("serial") public class ControlPanel extends JPanel implements ActionListener { JPanel control; JSlider Speed; JButton Previous, Start, Next, Stop, Quit; LifePanel Display; public ControlPanel(LifePanel panel) { super(); Display = panel; Speed = new JSlider(JSlider.HORIZONTAL, 1, 10, 1); control = new JPanel(); Start = new JButton("Start"); Stop = new JButton("Stop"); Quit = new QuitButton(); Speed.setMajorTickSpacing(1); Speed.setMinorTickSpacing(1); Speed.setPaintTicks(true); Speed.setPaintLabels(true); Speed.addChangeListener(new SliderListener()); Start.addActionListener(this); Stop.addActionListener(this); control.add(Start); control.add(Stop); control.add(Speed); control.add(Quit); this.add(control); setVisible(true); } private class SliderListener implements ChangeListener { public void stateChanged(ChangeEvent e) {} } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == Start) { } } }
import acm.graphics.*; import java.io.*; @SuppressWarnings("serial") public class LifePanel extends GCanvas { public char VALUE[][][] = new char [3][20][30]; public String FILE; public GRect Cell; public boolean active = false; public LifePanel(String f) { OpenFile(f); DrawGrid(); } public void OpenFile(String f) { String FILE = f; FileInputStream fis = null; try { fis = new FileInputStream(FILE); } catch (FileNotFoundException e) { System.out.println("File Not Found"); } try { int temp, x = 0, y = 0; while (x != 20) { while (y != 30) { temp = fis.read(); if (temp == 10) temp = fis.read(); VALUE[1][x][y] = (char) temp; y++; } y = 0; x++; } } catch (IOException e) { System.out.println("Error reading file"); } } public void DrawGrid() { this.removeAll(); int x = 0, y = 0; while (x != 20) { while (y != 30) { Cell = new GRect(15,15); if (VALUE[1][x][y] == (char) 48) Cell.setFillColor(java.awt.Color.BLUE); else Cell.setFillColor(java.awt.Color.YELLOW); Cell.setFilled(true); add(Cell,x*15+250,y*15+10); y++; } y = 0;x++; } } public void NextGeneration() { int x=0,y=0,f=-1,g=-1,on=0; while (x != 20) { while (y != 30) { while (f != 2) { while (g != 2) { try { if (VALUE[1][x+f][y+g] == (char) 49) on++; g++; } catch (ArrayIndexOutOfBoundsException e) { g++; } } f++;g=-1; } VALUE[2][x][y] = VALUE[1][x][y]; if (VALUE[1][x][y] == (char) 49) on--; if (VALUE[1][x][y] == (char) 49 && ((on<2) || (on>3))) VALUE[2][x][y] = (char) 48; if (VALUE[1][x][y] == (char) 48 && on == 3) VALUE[2][x][y] = (char) 49; y++;f=-1;on=0; } y = 0;x++; } x=0;y=0; while (x != 20) { while (y != 30) { VALUE[1][x][y] = VALUE[2][x][y]; y++; } x++;y=0; } } }
Only thing I didn't add was my QuitButton code, but that's just a simple System.exit function. I'm guessing my loop issue would be in ControlPanel.java. Please let me know what I should do