import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import java.io.*; import java.util.Scanner; public class GridPanel extends JPanel { double x1, y1; final int gridSize = 100; final int draw = 0; final int fill = 1; final int pad = 40; int DELAY = 1; Engine eng; JButton next, start, stop, clear; JTextField run; private JFileChooser chooser; private Scanner scan; int iterations; private Timer timer; int counter = 0; private JComboBox combobox; static final long serialVersionUID = 01; public GridPanel() { eng = new Engine(gridSize, gridSize); next = new JButton("Next"); next.addActionListener(new GridPanel.ButtonListener()); start = new JButton("Start"); start.addActionListener (new GridPanel.ButtonListener()); stop = new JButton("Stop"); stop.addActionListener (new GridPanel.ButtonListener()); clear = new JButton("Clear"); clear.addActionListener (new GridPanel.ButtonListener()); run = new JTextField("Run Count"); add(next); add(start); add(stop); add(run); add(clear); timer = new Timer(DELAY, new LoopListener()); // File Operation Combo Box String[] options = {"File...","Save","Open","Quit"}; combobox = new JComboBox (options); combobox.addActionListener((new ComboListener())); add(combobox); } public void paintComponent (Graphics a) { super.paintComponent(a); Graphics2D d2 = (Graphics2D)a; d2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); double w = getWidth(); double h = getHeight(); x1 = (w - 2 * pad) / gridSize; y1 = (h - 2 * pad) / gridSize; // Draw Rows double x2 = pad, y2 = pad, x3 = w - pad, y3 = h - pad; for (int m = 0; m <= gridSize; m++) { d2.draw(new Line2D.Double (x2, y2, x3, y2)); y2 += y1; } // Draw Columns y2 = pad; for (int m = 0; m <= gridSize; m++) { d2.draw(new Line2D.Double(x2,y2,x2,y3)); x2 += x1; } // Fill Cells when Clicked d2.setPaint(Color.blue); for (int row = 0; row < eng.returnRowCount(); row++) { for (int col = 0; col < eng.returnColumnCount(row); col++) { if (eng.getCell(row, col) == true) { x2 = pad + col * x1 + 1; y2 = pad + row * y1 + 1; d2.fill(new Rectangle2D.Double(x2, y2, x1-1, y1-1)); } } } } public void changeCellColor(int row, int column) { if (eng.getCell(row, column) == false) eng.setCell(row, column, true); else eng.setCell(row, column, false); //System.out.println(row + " " + column); repaint(); } private class ButtonListener implements ActionListener { public void actionPerformed (ActionEvent event) { Object source = event.getSource(); if (source == next) { eng.nextStep(); repaint(); } if (source == start) { timer.start(); } if (source == stop) { timer.stop(); counter = 0; } if (source == clear) { for (int i=0; i " ); System.exit(0); } BufferedWriter bw = new BufferedWriter(fw); PrintWriter outFile = new PrintWriter(bw); for (int i = 0 ; i < eng.returnRowCount() ; i++) { for (int j = 0 ; j < eng.returnColumnCount(i) ; j++) outFile.print(booleanToString(eng.getCell(i, j)) + " ") ; outFile.println(); } outFile.close(); } private void openFile(File file) throws IOException { try { scan = new Scanner(file); } catch (IOException e) { System.out.println("PUKE!"); } String s1; s1 = scan.nextLine(); String tmp[] = s1.split(" "); for (int i=0; i < eng.returnRowCount(); i++) { s1 = scan.nextLine(); tmp = s1.split(" "); for (int j=0; j