diff --git a/manualtest/game/JTableTest.java b/manualtest/game/JTableTest.java index f6da19cfe4bdea7acfc95c888424bb1ed053ddad..52170258d1e23be52b3a32074517685b309bea30 100644 --- a/manualtest/game/JTableTest.java +++ b/manualtest/game/JTableTest.java @@ -16,11 +16,14 @@ public class JTableTest { testFrame.setSize(300, 300); JButton stepButton = new JButton("Step"); JButton startButton = new JButton("Start"); + JButton stopButton = new JButton("Stop"); startButton.addActionListener(new StartButtonListener(testModel)); stepButton.addActionListener(new StepButtonListener(testModel)); + stopButton.addActionListener(new StopButtonListener(testModel)); JPanel bottomPanel = new JPanel(); bottomPanel.add(stepButton); bottomPanel.add(startButton); + bottomPanel.add(stopButton); testFrame.add(bottomPanel, BorderLayout.PAGE_END); testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); testFrame.setVisible(true); diff --git a/manualtest/game/StopButtonListener.java b/manualtest/game/StopButtonListener.java new file mode 100644 index 0000000000000000000000000000000000000000..a90ccbcb485c67d5ee3b9d649aa83c99902ca50d --- /dev/null +++ b/manualtest/game/StopButtonListener.java @@ -0,0 +1,17 @@ +package game; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class StopButtonListener implements ActionListener { + + private GameModel model; + + public StopButtonListener(GameModel model) { + this.model = model; + } + + public void actionPerformed(ActionEvent e) { + model.stop(); + } +} diff --git a/src/game/GameModel.java b/src/game/GameModel.java index c1ca890efa52bc79da1328625a81bb3a7e9f2ab7..d64720b8f0fc8fbcb147a8f509dfbc5e2ef051fb 100644 --- a/src/game/GameModel.java +++ b/src/game/GameModel.java @@ -6,11 +6,14 @@ public class GameModel extends AbstractTableModel { private GameBoard cells; private GameRunner runner; + private GameRunner2 runner2; //TODO runners private boolean editable; public GameModel(int height, int width) { cells = new GameBoard(height, width); runner = new GameRunner(this, cells); + runner2 = new GameRunner2(this, cells); + runner2.start(); editable = true; } @@ -32,10 +35,13 @@ public class GameModel extends AbstractTableModel { public void start() { editable=false; - runner.start(); + //runner.start(); + //TODO runners + runner2.resumeRunner(); } public void stop(){ + /* runner.stopRunner(); while (runner.isAlive()){ try { @@ -44,7 +50,12 @@ public class GameModel extends AbstractTableModel { e.printStackTrace(); } } - editable = true; + */ + //TODO runners + runner2.pauseRunner(); + synchronized (cells) { + editable = true; + } } public boolean isCellEditable(int rowIndex, int columnIndex) { diff --git a/src/game/GameRunner2.java b/src/game/GameRunner2.java new file mode 100644 index 0000000000000000000000000000000000000000..a646ac06bc5b5a76a6128c92a5dfdf70db49609c --- /dev/null +++ b/src/game/GameRunner2.java @@ -0,0 +1,58 @@ +package game; + +public class GameRunner2 extends Thread { + + private final GameModel model; + private final GameBoard board; + private volatile boolean paused; + + public GameRunner2(GameBoard board) { + this.board = board; + model = null; + paused = true; + } + + public GameRunner2(GameModel model, GameBoard board) { + this.model = model; + this.board = board; + paused = true; + } + + public void run() { + synchronized (board) { + while (true) { + while (paused) { + try { + board.wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + while (!paused) { + board.step(); + if (model != null) + model.fireTableDataChanged(); + try { + sleep(500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + } + } + + public void pauseRunner() { + paused = true; + } + + public void resumeRunner() { + if (paused) { + synchronized (board) { + paused = false; + board.notify(); + } + } + } +}