Преглед изворни кода

Better pause/reset/clear buttons.

Also set some default patterns for some of the algorithms.
master
Lee пре 12 година
родитељ
комит
c6fba23c2b
2 измењених фајлова са 92 додато и 25 уклоњено
  1. +46
    -13
      src/Goldfish.java
  2. +46
    -12
      src/Render.java

+ 46
- 13
src/Goldfish.java Прегледај датотеку

@@ -1,5 +1,7 @@
package edu.stuy.goldfish;
import java.util.Random;
import edu.stuy.goldfish.rules.*;
public class Goldfish {
@@ -7,6 +9,8 @@ public class Goldfish {
private Grid _grid;
private Render _render;
private Random random = new Random();
public Goldfish() {
int width = 128;
@@ -16,8 +20,12 @@ public class Goldfish {
}
public void run() {
setup();
setup(_render.rule);
while (true) {
if (_render.reset) {
setup(_render.rule);
_render.reset = false;
}
if (!_render.paused) {
String rule = _render.rule;
if (rule.equals("Conway"))
@@ -34,7 +42,6 @@ public class Goldfish {
_render.sleep();
}
}

public static int getMaxStates(String rule) {
if (rule.equals("Conway"))
@@ -46,17 +53,43 @@ public class Goldfish {
else if (rule.equals("Brian's Brain"))
return BriansBrain.states;
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) {


+ 46
- 12
src/Render.java Прегледај датотеку

@@ -18,14 +18,19 @@ public class Render extends Canvas implements Runnable, MouseListener,
public boolean paused;
public String rule;
public boolean reset;
private Grid _grid;
private BufferedImage _image;
private int[] _pixels;
private BufferedImage _image;
private long _lastTick;
private String[] _rules;
private JFrame _frame;
private JButton pauseButton;
public Render(int width, int height, Grid g, String[] rules) {
addMouseListener(this);
addMouseMotionListener(this);
@@ -33,6 +38,7 @@ public class Render extends Canvas implements Runnable, MouseListener,
Render.height = height;
setScale();
paused = false;
reset = false;
rule = rules[0];
_grid = g;
_image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
@@ -78,9 +84,20 @@ public class Render extends Canvas implements Runnable, MouseListener,
menuMain.add(menuAlgo);
menuBar.add(menuMain);
JCheckBoxMenuItem pauseButton = new JCheckBoxMenuItem("Pause");
pauseButton = new JButton("Pause");
pauseButton.setActionCommand("pause");
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.setJMenuBar(menuBar);
@@ -99,9 +116,9 @@ public class Render extends Canvas implements Runnable, MouseListener,
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
state = _grid.getPatch(i, j).getState();
if(_pixels[i + j * width] == state) {
if (_pixels[i + j * width] == state) {
} 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) {
_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
public void mouseDragged(MouseEvent e) {
@@ -187,16 +212,25 @@ public class Render extends Canvas implements Runnable, MouseListener,
// Affects the algorithms menu
@Override
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
public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED)
paused = true;
else
paused = false;
return;
}
}

Loading…
Откажи
Сачувај