Browse Source

Selecting rules from the menu should work now.

master
Ben Kurtovic 12 years ago
parent
commit
0bc0510e8c
2 changed files with 71 additions and 81 deletions
  1. +23
    -10
      src/Goldfish.java
  2. +48
    -71
      src/Render.java

+ 23
- 10
src/Goldfish.java View File

@@ -3,6 +3,8 @@ package edu.stuy.goldfish;
import edu.stuy.goldfish.rules.*; import edu.stuy.goldfish.rules.*;
public class Goldfish { public class Goldfish {
private static final String[] RULES = {"Conway", "Conway4", "Life Without Death", "Brian's Brain"};
private Grid _grid; private Grid _grid;
private Render _render; private Render _render;
@@ -10,10 +12,30 @@ public class Goldfish {
int width = 128; int width = 128;
int height = 128; int height = 128;
_grid = new Grid(width, height); _grid = new Grid(width, height);
_render = new Render(width, height, _grid);
_render = new Render(width, height, _grid, RULES);
} }
public void run() { public void run() {
setup();
while (true) {
if (!_render.paused) {
String rule = _render.rule;
if (rule.equals("Conway"))
_grid = Conway.run(_grid);
else if (rule.equals("Conway4"))
_grid = Conway4.run(_grid);
else if (rule.equals("Life Without Death"))
_grid = LifeWithoutDeath.run(_grid);
else if (rule.equals("Brian's Brain"))
_grid = BriansBrain.run(_grid);
}
_render.setGrid(_grid);
_render.run();
_render.sleep();
}
}
private void setup() {
for (int i = 0; i < _grid.getWidth(); i += 16) { for (int i = 0; i < _grid.getWidth(); i += 16) {
for (int j = 0; j < _grid.getHeight(); j += 16) { for (int j = 0; j < _grid.getHeight(); j += 16) {
_grid.getPatch(i + 1, j + 0).setState(1); _grid.getPatch(i + 1, j + 0).setState(1);
@@ -23,15 +45,6 @@ public class Goldfish {
_grid.getPatch(i + 0, j + 2).setState(1); _grid.getPatch(i + 0, j + 2).setState(1);
} }
} }
while (true) {
if(!_render.paused){
_grid = Conway.run(_grid);
}
_render.setGrid(_grid);
_render.run();
_render.sleep();
}
} }
public static void main(String[] args) { public static void main(String[] args) {


+ 48
- 71
src/Render.java View File

@@ -1,29 +1,10 @@
package edu.stuy.goldfish; package edu.stuy.goldfish;


import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

import javax.swing.*;


public class Render extends Canvas implements Runnable, MouseListener, public class Render extends Canvas implements Runnable, MouseListener,
MouseMotionListener, ActionListener, ItemListener { MouseMotionListener, ActionListener, ItemListener {
@@ -36,33 +17,35 @@ public class Render extends Canvas implements Runnable, MouseListener,
public static int max_fps = 15; public static int max_fps = 15;


public boolean paused; public boolean paused;
private JFrame _frame;
private BufferedImage _image;
public String rule;
private Grid _grid; private Grid _grid;
private BufferedImage _image;
private int[] _pixels; private int[] _pixels;
private long _last_tick;

JMenuBar menu_bar;
JMenu menu_main, menu_algo;
JMenuItem menu_algo_conway;
JCheckBoxMenuItem pause_button;
private long _lastTick;
private String[] _rules;
private JFrame _frame;


public Render(int width, int height, Grid g) {
public Render(int width, int height, Grid g, String[] rules) {
addMouseListener(this); addMouseListener(this);
addMouseMotionListener(this); addMouseMotionListener(this);
Render.width = width; Render.width = width;
Render.height = height; Render.height = height;
setScale(); setScale();
paused = false;
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);
_pixels = ((DataBufferInt) _image.getRaster().getDataBuffer())
.getData();
_last_tick = 0;
_pixels = ((DataBufferInt) _image.getRaster().getDataBuffer()).getData();
_lastTick = 0;
_rules = rules;
Dimension size = new Dimension(width * scale, height * scale); Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size); setPreferredSize(size);
setFrame(); setFrame();
menu_algo_conway.addActionListener(this);
pause_button.addActionListener(this);
}

public Render(int width, int height, Grid g) {
this(width, height, g, new String[0]);
} }


public Render(int width, int height) { public Render(int width, int height) {
@@ -84,36 +67,23 @@ public class Render extends Canvas implements Runnable, MouseListener,
} }


private void setFrame() { private void setFrame() {
menu_bar = new JMenuBar();

menu_main = new JMenu("Menu");

menu_algo = new JMenu("Algorithms");

menu_algo_conway = new JMenuItem("Conway");
menu_algo.add(menu_algo_conway);

menu_main.add(menu_algo);
menu_bar.add(menu_main);
JMenuBar menuBar = new JMenuBar();
JMenu menuMain = new JMenu("Menu");
JMenu menuAlgo = new JMenu("Algorithms");
for (String rule : _rules) {
JMenuItem menuAlgoItem = new JMenuItem(rule);
menuAlgo.add(menuAlgoItem);
menuAlgoItem.addActionListener(this);
}
menuMain.add(menuAlgo);
menuBar.add(menuMain);


pause_button = new JCheckBoxMenuItem("Pause");
menu_bar.add(pause_button);
ItemListener pauseListener = new ItemListener() {
public void itemStateChanged(ItemEvent event) {
AbstractButton pause_button = (AbstractButton) event
.getSource();
if (event.getStateChange() == ItemEvent.SELECTED) {
paused = true;
} else {
paused = false;
}
return;
}
};
pause_button.addItemListener(pauseListener);
JCheckBoxMenuItem pauseButton = new JCheckBoxMenuItem("Pause");
menuBar.add(pauseButton);
pauseButton.addItemListener(this);


_frame = new JFrame(); _frame = new JFrame();
_frame.setJMenuBar(menu_bar);
_frame.setJMenuBar(menuBar);
_frame.setResizable(false); _frame.setResizable(false);
_frame.setTitle(title); _frame.setTitle(title);
_frame.add(this); _frame.add(this);
@@ -123,8 +93,8 @@ public class Render extends Canvas implements Runnable, MouseListener,
_frame.setVisible(true); _frame.setVisible(true);
} }


int state;
private void update() { private void update() {
int state;
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();
@@ -155,7 +125,7 @@ public class Render extends Canvas implements Runnable, MouseListener,
} }


public void sleep() { public void sleep() {
long since = System.currentTimeMillis() - _last_tick;
long since = System.currentTimeMillis() - _lastTick;
if (since < 1000 / max_fps) { if (since < 1000 / max_fps) {
try { try {
Thread.sleep(1000 / max_fps - since); Thread.sleep(1000 / max_fps - since);
@@ -163,7 +133,7 @@ public class Render extends Canvas implements Runnable, MouseListener,
return; return;
} }
} }
_last_tick = System.currentTimeMillis();
_lastTick = System.currentTimeMillis();
} }


public void setGrid(Grid g) { public void setGrid(Grid g) {
@@ -210,12 +180,19 @@ public class Render extends Canvas implements Runnable, MouseListener,
public void mouseMoved(MouseEvent e) { public void mouseMoved(MouseEvent e) {
} }


// Affects the algorithms menu
@Override @Override
public void itemStateChanged(ItemEvent arg0) {
public void actionPerformed(ActionEvent event) {
rule = event.getActionCommand();
} }


// Affects the pause button
@Override @Override
public void actionPerformed(ActionEvent arg0) {
public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED)
paused = true;
else
paused = false;
return;
} }

} }

Loading…
Cancel
Save