Skip to content
Snippets Groups Projects
Commit f4115010 authored by steyer's avatar steyer
Browse files

Új GameRunner (működik)

parent 56c8ad8d
Branches
No related tags found
No related merge requests found
...@@ -16,11 +16,14 @@ public class JTableTest { ...@@ -16,11 +16,14 @@ public class JTableTest {
testFrame.setSize(300, 300); testFrame.setSize(300, 300);
JButton stepButton = new JButton("Step"); JButton stepButton = new JButton("Step");
JButton startButton = new JButton("Start"); JButton startButton = new JButton("Start");
JButton stopButton = new JButton("Stop");
startButton.addActionListener(new StartButtonListener(testModel)); startButton.addActionListener(new StartButtonListener(testModel));
stepButton.addActionListener(new StepButtonListener(testModel)); stepButton.addActionListener(new StepButtonListener(testModel));
stopButton.addActionListener(new StopButtonListener(testModel));
JPanel bottomPanel = new JPanel(); JPanel bottomPanel = new JPanel();
bottomPanel.add(stepButton); bottomPanel.add(stepButton);
bottomPanel.add(startButton); bottomPanel.add(startButton);
bottomPanel.add(stopButton);
testFrame.add(bottomPanel, BorderLayout.PAGE_END); testFrame.add(bottomPanel, BorderLayout.PAGE_END);
testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
testFrame.setVisible(true); testFrame.setVisible(true);
......
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();
}
}
...@@ -6,11 +6,14 @@ public class GameModel extends AbstractTableModel { ...@@ -6,11 +6,14 @@ public class GameModel extends AbstractTableModel {
private GameBoard cells; private GameBoard cells;
private GameRunner runner; private GameRunner runner;
private GameRunner2 runner2; //TODO runners
private boolean editable; private boolean editable;
public GameModel(int height, int width) { public GameModel(int height, int width) {
cells = new GameBoard(height, width); cells = new GameBoard(height, width);
runner = new GameRunner(this, cells); runner = new GameRunner(this, cells);
runner2 = new GameRunner2(this, cells);
runner2.start();
editable = true; editable = true;
} }
...@@ -32,10 +35,13 @@ public class GameModel extends AbstractTableModel { ...@@ -32,10 +35,13 @@ public class GameModel extends AbstractTableModel {
public void start() { public void start() {
editable=false; editable=false;
runner.start(); //runner.start();
//TODO runners
runner2.resumeRunner();
} }
public void stop(){ public void stop(){
/*
runner.stopRunner(); runner.stopRunner();
while (runner.isAlive()){ while (runner.isAlive()){
try { try {
...@@ -44,8 +50,13 @@ public class GameModel extends AbstractTableModel { ...@@ -44,8 +50,13 @@ public class GameModel extends AbstractTableModel {
e.printStackTrace(); e.printStackTrace();
} }
} }
*/
//TODO runners
runner2.pauseRunner();
synchronized (cells) {
editable = true; editable = true;
} }
}
public boolean isCellEditable(int rowIndex, int columnIndex) { public boolean isCellEditable(int rowIndex, int columnIndex) {
return editable; return editable;
......
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();
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment