Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Updated to use Timer instead of a thread, made a few optimisations

  • Loading branch information...
commit 690e3074a45193459d5af6e86f2c75af6c1320fe 1 parent deccbfe
@JonnoFTW authored
Showing with 125 additions and 121 deletions.
  1. +22 −15 Buttons.java
  2. +30 −0 GUI.java
  3. +71 −85 LifeGame.java
  4. +2 −21 Main.java
View
37 Buttons.java
@@ -11,30 +11,35 @@
import javax.swing.JPanel;
import javax.swing.JToggleButton;
+/**
+ * @author Jonathan
+ *
+ */
public class Buttons extends JPanel {
- /**
- * @param args
- */
- private LifeGame life;
+
+ private GUI g;
public JToggleButton stopStart;
private JLabel tickCount;
- Buttons(final LifeGame life) {
- this.life = life;
+ /**
+ * The buttons that go at the top of the gui
+ * @param g
+ */
+ Buttons(final GUI g) {
+ this.g = g;
this.setSize(300, 50);
stopStart = new JToggleButton("Start",false);
- final Buttons t = this;
stopStart.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent ev) {
// TODO Auto-generated method stub
if(ev.getStateChange()== ItemEvent.SELECTED){
stopStart.setText("Running");
- life.start(t);
+ g.life.start();
} else if(ev.getStateChange() == ItemEvent.DESELECTED) {
stopStart.setText("Stopped");
- life.stop();
+ g.life.stop();
}
}
@@ -46,7 +51,7 @@ public void itemStateChanged(ItemEvent ev) {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
- life.exportBoard();
+ g.life.exportBoard();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
@@ -61,7 +66,7 @@ public void actionPerformed(ActionEvent e) {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
- life.importBoard();
+ g.life.importBoard();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
@@ -73,12 +78,11 @@ public void actionPerformed(ActionEvent e) {
@Override
public void actionPerformed(ActionEvent arg0) {
- // TODO Auto-generated method stub
- life.init();
+ g.life.init();
repaint();
}
});
- tickCount = new JLabel("Tick:"+life.getTicks());
+ tickCount = new JLabel("Tick:"+g.life.getTicks());
add(stopStart);
add(imp);
add(reset);
@@ -86,7 +90,10 @@ public void actionPerformed(ActionEvent arg0) {
add(imp);
add(tickCount);
}
+ /**
+ * Sets the tick count on the ticket label
+ */
public void setTick() {
- tickCount.setText("Tick:"+life.getTicks());
+ tickCount.setText("Tick:"+g.life.getTicks());
}
}
View
30 GUI.java
@@ -0,0 +1,30 @@
+import java.awt.BorderLayout;
+
+
+import javax.swing.*;
+public class GUI {
+
+ public LifeGame life;
+ public Buttons btns;
+ /**
+ * @param args
+ */
+
+ GUI() {
+ // Start the gui
+ JFrame frame = new JFrame("Game Of Life");
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ life = new LifeGame(this);
+ btns = new Buttons(this);
+ frame.setLayout(new BorderLayout());
+ frame.add(btns,BorderLayout.PAGE_START);
+ frame.add(life,BorderLayout.CENTER);
+ life.init();
+ frame.pack();
+ // frame.setResizable(false);
+ frame.setBounds(100, 100, 660, 720);
+ frame.setVisible(true);
+
+ }
+
+}
View
156 LifeGame.java
@@ -1,88 +1,59 @@
import java.io.*;
import java.util.Scanner;
import java.awt.*;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
-import java.awt.event.MouseListener;
import java.awt.image.BufferStrategy;
+
+import javax.swing.Timer;
+/**
+ * @author Jonathan
+ *
+ */
public class LifeGame extends Canvas {
- private byte[][] cells;
+ private byte[][] cells, newCells;
+
private long tick;
- private int size = 50;
- private boolean running = false;
- private Thread t;
- private Buttons btn;
+ private int size = 100;
+ private Timer t;
private BufferStrategy bf;
- LifeGame() {
+ private GUI g;
+ /**
+ * A new game of life,
+ * @param g the GUI object that holds this frame and the buttons
+ */
+ LifeGame(final GUI g) {
+ this.g = g;
bf = getBufferStrategy();
- init();
setSize(1000+size,1000+size);
setBackground(Color.WHITE);
- addMouseListener(new MouseListener() {
-
- @Override
- public void mouseReleased(MouseEvent arg0) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void mousePressed(MouseEvent arg0) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void mouseExited(MouseEvent arg0) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void mouseEntered(MouseEvent arg0) {
- // TODO Auto-generated method stub
-
- }
-
+ addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
int x = e.getX()/(getWidth()/size);
int y = e.getY()/(getHeight()/size);
- System.out.println("Marking cell at: x="+x+" y="+y);
- if(cells[x][y] == 0) {
- cells[x][y] = 1;
- } else {
- cells[x][y] = 0;
- }
+
+ cells[x][y] = (byte) (~cells[x][y] & 1);
+ System.out.println("Marking cell at: x="+x+" y="+y+" as "+cells[x][y]);
repaint();
}
});
- t = new Thread(new Runnable() {
-
- @Override
- public void run() {
- // TODO Auto-generated method stub
- while(true) {
- try {
- Thread.sleep(250);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- if(running) {
- tick();
- try{
- btn.setTick();
- } catch (NullPointerException e) {
- // TODO: handle exception
- }
- }
- }
- }
- });
- t.start();
+ t = new Timer(500,new ActionListener() {
+
+ @Override
+ public void actionPerformed(ActionEvent arg0) {
+ tick();
+ g.btns.setTick();
+ }
+ });
}
+ /* (non-Javadoc)
+ * @see java.awt.Canvas#paint(java.awt.Graphics)
+ */
public void paint(Graphics g) {
createBufferStrategy(1);
@@ -99,35 +70,41 @@ public void paint(Graphics g) {
}
+ /**
+ * Render the cells in the frame with a neat border around each cell
+ * @param g
+ */
private void render(Graphics g) {
for(int x = 0; x < cells.length; x++ ) {
for (int y = 0; y < cells[x].length; y++) {
if(cells[x][y] ==1){
g.setColor(Color.BLACK);
g.fillRect(x*(getWidth()/size), y*(getHeight()/size),getWidth()/size ,getWidth()/size);
- // System.out.println("Filled Square at "+x+","+y);
}
g.setColor(Color.GRAY);
g.drawRect(x*(getWidth()/size), y*(getHeight()/size),getWidth()/size ,getWidth()/size);
}
}
}
+ /**
+ * Resets the game board
+ */
public void init() {
cells = new byte[size][size];
- //Arrays.fill(cells,new byte[size]);
+ // Set all cells to 0 (dead)
for (int i = 0; i < cells.length; i++) {
for (int j = 0; j < cells[i].length; j++) {
cells[i][j] = 0;
}
}
tick = 0;
- try{
- btn.setTick();
- } catch (NullPointerException e) {
- // TODO: handle exception
- }
+ g.btns.setTick();
repaint();
}
+ /**
+ * Takes in the board.dat file and uses it to fill the board
+ * @throws FileNotFoundException
+ */
public void importBoard() throws FileNotFoundException {
init();
Scanner in = new Scanner(new File("board.dat"));
@@ -136,9 +113,17 @@ public void importBoard() throws FileNotFoundException {
}
in.close();
}
+ /**
+ * Returns the number of ticks that have passed in the simulation
+ * @return
+ */
public long getTicks() {
return tick;
}
+ /**
+ * Exports the current board to the file board.dat for later use.
+ * @throws IOException
+ */
public void exportBoard() throws IOException {
// Save the board to a file as x y
BufferedWriter out = new BufferedWriter(new FileWriter("board.dat"));
@@ -151,24 +136,27 @@ public void exportBoard() throws IOException {
}
out.close();
}
- public void start(Buttons btn) {
- // Should probably return a message to the GUI
- this.btn = btn;
- running = true;
- /* synchronized (t) {
- t.notify();
- }*/
+ /**
+ * Starts the simulation
+ */
+ public void start() {
+ t.start();
}
+ /**
+ * Stops the simulation
+ */
public void stop() {
- running = false;
- /*synchronized (t) {
- t.notify();
- }*/
+ t.stop();
+
}
+ /**
+ * Progresses the simulation one step forward. Uses the 8 neighbours around each cell
+ * to calculate if a given cell lives, dies or is born.
+ */
private void tick() {
tick++;
- byte[][] newCells = new byte[size][size];
+ newCells = new byte[size][size];
int totalSum = 0;
// The array should wrap around so that cells[1000][1001] refers to the top right cell
for (int i = 0; i < cells.length; i++) {
@@ -215,8 +203,6 @@ private void tick() {
if(totalSum == 0) {
// Should probably stop now, since everyone is dead
System.out.println("All cells are dead, stopping!");
- btn.stopStart.setText("Stopped");
- btn.stopStart.setSelected(false);
stop();
}
cells = newCells;
View
23 Main.java
@@ -1,30 +1,11 @@
-import java.awt.BorderLayout;
-
-import javax.swing.*;
+
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
- // TODO Auto-generated method stub
- // Start the gui
- JFrame frame = new JFrame("Game Of Life");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
-
- // Has a play button, pause button, a grid of cells that can be flipped 1,0 on click.
-
- LifeGame lfe = new LifeGame();
- Buttons btns = new Buttons(lfe);
- frame.setLayout(new BorderLayout());
- frame.add(btns,BorderLayout.PAGE_START);
- frame.add(lfe,BorderLayout.CENTER);
- frame.pack();
- frame.setResizable(false);
- frame.setBounds(100, 100, 660, 720);
- frame.setVisible(true);
-
+ new GUI();
}
}

0 comments on commit 690e307

Please sign in to comment.
Something went wrong with that request. Please try again.