Also set some default patterns for some of the algorithms.master
@@ -1,5 +1,7 @@ | |||||
package edu.stuy.goldfish; | package edu.stuy.goldfish; | ||||
import java.util.Random; | |||||
import edu.stuy.goldfish.rules.*; | import edu.stuy.goldfish.rules.*; | ||||
public class Goldfish { | public class Goldfish { | ||||
@@ -7,6 +9,8 @@ public class Goldfish { | |||||
private Grid _grid; | private Grid _grid; | ||||
private Render _render; | private Render _render; | ||||
private Random random = new Random(); | |||||
public Goldfish() { | public Goldfish() { | ||||
int width = 128; | int width = 128; | ||||
@@ -16,8 +20,12 @@ public class Goldfish { | |||||
} | } | ||||
public void run() { | public void run() { | ||||
setup(); | |||||
setup(_render.rule); | |||||
while (true) { | while (true) { | ||||
if (_render.reset) { | |||||
setup(_render.rule); | |||||
_render.reset = false; | |||||
} | |||||
if (!_render.paused) { | if (!_render.paused) { | ||||
String rule = _render.rule; | String rule = _render.rule; | ||||
if (rule.equals("Conway")) | if (rule.equals("Conway")) | ||||
@@ -34,7 +42,6 @@ public class Goldfish { | |||||
_render.sleep(); | _render.sleep(); | ||||
} | } | ||||
} | } | ||||
public static int getMaxStates(String rule) { | public static int getMaxStates(String rule) { | ||||
if (rule.equals("Conway")) | if (rule.equals("Conway")) | ||||
@@ -46,17 +53,43 @@ public class Goldfish { | |||||
else if (rule.equals("Brian's Brain")) | else if (rule.equals("Brian's Brain")) | ||||
return BriansBrain.states; | return BriansBrain.states; | ||||
return 2; | return 2; | ||||
} | |||||
private void setup() { | |||||
for (int i = 0; i < _grid.getWidth(); i += 16) { | |||||
for (int j = 0; j < _grid.getHeight(); j += 16) { | |||||
_grid.getPatch(i + 1, j + 0).setState(1); | |||||
_grid.getPatch(i + 2, j + 1).setState(1); | |||||
_grid.getPatch(i + 2, j + 2).setState(1); | |||||
_grid.getPatch(i + 1, j + 2).setState(1); | |||||
_grid.getPatch(i + 0, j + 2).setState(1); | |||||
} | |||||
} | |||||
} | |||||
private void setup(String rule) { | |||||
if(rule.equals("Conway")) { | |||||
int[][] glidergun = { | |||||
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0}, | |||||
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0}, | |||||
{0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1}, | |||||
{0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1}, | |||||
{1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, | |||||
{1,1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0}, | |||||
{0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0}, | |||||
{0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, | |||||
{0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} | |||||
}; | |||||
for(int i = 0; i < 36; i++) { | |||||
for(int j = 0; j < 9; j++) { | |||||
_grid.getPatch(i+2,j+2).setState(glidergun[j][i]); | |||||
} | |||||
} | |||||
} else if (rule.equals("Life Without Death")) { | |||||
int[][] pattern = { | |||||
{1,1,1,1,0,1}, | |||||
{1,0,1,1,1,1} | |||||
}; | |||||
for(int i = 0; i < 6; i++) { | |||||
for(int j = 0; j < 2; j++) { | |||||
_grid.getPatch(i+((_grid.getHeight()-6)/2),j+((_grid.getWidth()-2)/2)).setState(pattern[j][i]); | |||||
} | |||||
} | |||||
} else if (rule.equals("Brian's Brain")) { | |||||
for(int i = 0; i < _grid.getHeight(); i++) { | |||||
for (int j = 0; j < _grid.getWidth(); j++) { | |||||
_grid.getPatch(i,j).setState(random.nextInt(3)); | |||||
} | |||||
} | |||||
} | |||||
} | } | ||||
public static void main(String[] args) { | public static void main(String[] args) { | ||||
@@ -18,14 +18,19 @@ public class Render extends Canvas implements Runnable, MouseListener, | |||||
public boolean paused; | public boolean paused; | ||||
public String rule; | public String rule; | ||||
public boolean reset; | |||||
private Grid _grid; | private Grid _grid; | ||||
private BufferedImage _image; | |||||
private int[] _pixels; | private int[] _pixels; | ||||
private BufferedImage _image; | |||||
private long _lastTick; | private long _lastTick; | ||||
private String[] _rules; | private String[] _rules; | ||||
private JFrame _frame; | private JFrame _frame; | ||||
private JButton pauseButton; | |||||
public Render(int width, int height, Grid g, String[] rules) { | public Render(int width, int height, Grid g, String[] rules) { | ||||
addMouseListener(this); | addMouseListener(this); | ||||
addMouseMotionListener(this); | addMouseMotionListener(this); | ||||
@@ -33,6 +38,7 @@ public class Render extends Canvas implements Runnable, MouseListener, | |||||
Render.height = height; | Render.height = height; | ||||
setScale(); | setScale(); | ||||
paused = false; | paused = false; | ||||
reset = false; | |||||
rule = rules[0]; | rule = rules[0]; | ||||
_grid = g; | _grid = g; | ||||
_image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); | _image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); | ||||
@@ -78,9 +84,20 @@ public class Render extends Canvas implements Runnable, MouseListener, | |||||
menuMain.add(menuAlgo); | menuMain.add(menuAlgo); | ||||
menuBar.add(menuMain); | menuBar.add(menuMain); | ||||
JCheckBoxMenuItem pauseButton = new JCheckBoxMenuItem("Pause"); | |||||
pauseButton = new JButton("Pause"); | |||||
pauseButton.setActionCommand("pause"); | |||||
menuBar.add(pauseButton); | menuBar.add(pauseButton); | ||||
pauseButton.addItemListener(this); | |||||
pauseButton.addActionListener(this); | |||||
JButton resetButton = new JButton("Reset"); | |||||
resetButton.setActionCommand("reset"); | |||||
menuBar.add(resetButton); | |||||
resetButton.addActionListener(this); | |||||
JButton clearButton = new JButton("Clear"); | |||||
clearButton.setActionCommand("clear"); | |||||
menuBar.add(clearButton); | |||||
clearButton.addActionListener(this); | |||||
_frame = new JFrame(); | _frame = new JFrame(); | ||||
_frame.setJMenuBar(menuBar); | _frame.setJMenuBar(menuBar); | ||||
@@ -99,9 +116,9 @@ public class Render extends Canvas implements Runnable, MouseListener, | |||||
for (int i = 0; i < width; i++) { | for (int i = 0; i < width; i++) { | ||||
for (int j = 0; j < height; j++) { | for (int j = 0; j < height; j++) { | ||||
state = _grid.getPatch(i, j).getState(); | state = _grid.getPatch(i, j).getState(); | ||||
if(_pixels[i + j * width] == state) { | |||||
if (_pixels[i + j * width] == state) { | |||||
} else { | } else { | ||||
draw(i, j, (int) ((state/((double) states - 1)) * 0xffffff)); | |||||
draw(i, j, (int) ((state / ((double) states - 1)) * 0xffffff)); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
@@ -144,6 +161,14 @@ public class Render extends Canvas implements Runnable, MouseListener, | |||||
public void draw(int x, int y, int color) { | public void draw(int x, int y, int color) { | ||||
_pixels[x + y * width] = color; | _pixels[x + y * width] = color; | ||||
} | } | ||||
public void clear() { | |||||
for (int i = 0; i < _grid.getHeight(); i++) { | |||||
for (int j = 0; j < _grid.getWidth(); j++) { | |||||
_grid.getPatch(i, j).setState(0); | |||||
} | |||||
} | |||||
} | |||||
@Override | @Override | ||||
public void mouseDragged(MouseEvent e) { | public void mouseDragged(MouseEvent e) { | ||||
@@ -187,16 +212,25 @@ public class Render extends Canvas implements Runnable, MouseListener, | |||||
// Affects the algorithms menu | // Affects the algorithms menu | ||||
@Override | @Override | ||||
public void actionPerformed(ActionEvent event) { | public void actionPerformed(ActionEvent event) { | ||||
rule = event.getActionCommand(); | |||||
if ("pause".equals(event.getActionCommand())) { | |||||
if (paused) { | |||||
paused = false; | |||||
pauseButton.setText("Pause"); | |||||
} else { | |||||
paused = true; | |||||
pauseButton.setText("Unpause"); | |||||
} | |||||
} else if ("reset".equals(event.getActionCommand())) { | |||||
clear(); | |||||
reset = true; | |||||
} else if ("clear".equals(event.getActionCommand())) { | |||||
clear(); | |||||
} else { | |||||
rule = event.getActionCommand(); | |||||
} | |||||
} | } | ||||
// Affects the pause button | |||||
@Override | @Override | ||||
public void itemStateChanged(ItemEvent event) { | public void itemStateChanged(ItemEvent event) { | ||||
if (event.getStateChange() == ItemEvent.SELECTED) | |||||
paused = true; | |||||
else | |||||
paused = false; | |||||
return; | |||||
} | } | ||||
} | } |