|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- package edu.stuy.goldfish;
-
- import java.util.Random;
-
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.image.*;
-
- import javax.swing.*;
- import javax.swing.event.*;
-
- public class Render extends Canvas implements Runnable, MouseListener,
- MouseMotionListener, ActionListener {
- private static final long serialVersionUID = 1L;
-
- public static String title;
- public static int width;
- public static int height;
- public static int scale;
-
- public int fps_now;
- public boolean paused;
- public String rule;
- public boolean reset;
-
- private Grid _grid;
- private int[] _pixels;
- private BufferedImage _image;
- private long _lastTick;
- private String[] _rules;
- private JFrame _frame;
- private JButton pauseButton;
- private Random random = new Random();
- private Patch _lastPatch;
-
- public Render(int width, int height, Grid g, String[] rules) {
- addMouseListener(this);
- addMouseMotionListener(this);
- Render.title = "Goldfish: " + rules[0];
- Render.width = width;
- Render.height = height;
- setScale();
- paused = false;
- reset = false;
- rule = rules[0];
- _grid = g;
- _image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- _pixels = ((DataBufferInt) _image.getRaster().getDataBuffer()).getData();
- _lastTick = 0;
- _rules = rules;
- _lastPatch = null;
- fps_now = 15;
- setFrame();
- }
-
- public Render(int width, int height, Grid g) {
- this(width, height, g, new String[0]);
- }
-
- public Render(int width, int height) {
- this(width, height, new Grid(width, height));
- }
-
- public Render() {
- this(256, 256);
- }
-
- private void setScale() {
- if (height <= 128 || width <= 128) {
- Render.scale = 4;
- } else if (height <= 256 || width <= 256) {
- Render.scale = 2;
- } else {
- Render.scale = 1;
- }
- }
-
- private void setFrame() {
- JMenuBar menuBar = new JMenuBar();
- JMenu menuAlgo = new JMenu("Algorithms");
- menuAlgo.setFont(new Font("Arial",1,12));
- menuAlgo.setPreferredSize(new Dimension(75,0));
- for (String rule : _rules) {
- JMenuItem menuAlgoItem = new JMenuItem(rule);
- menuAlgo.add(menuAlgoItem);
- menuAlgoItem.addActionListener(this);
- }
- menuBar.add(menuAlgo);
-
- pauseButton = new JButton("Pause");
- pauseButton.setActionCommand("pause");
- pauseButton.setFont(new Font("Arial",1,12));
- pauseButton.setPreferredSize(new Dimension(90,0));
- menuBar.add(pauseButton);
- pauseButton.addActionListener(this);
-
- JButton resetButton = new JButton("Reset");
- resetButton.setActionCommand("reset");
- resetButton.setFont(new Font("Arial",1,12));
- menuBar.add(resetButton);
- resetButton.addActionListener(this);
-
- JButton randomButton = new JButton("Random");
- randomButton.setActionCommand("random");
- randomButton.setFont(new Font("Arial",1,12));
- menuBar.add(randomButton);
- randomButton.addActionListener(this);
-
- JButton clearButton = new JButton("Clear");
- clearButton.setActionCommand("clear");
- clearButton.setFont(new Font("Arial",1,12));
- menuBar.add(clearButton);
- clearButton.addActionListener(this);
-
- JSlider framesPerSecond = new JSlider(JSlider.HORIZONTAL, 0, 30, 15);
- ChangeListener fpschange = new ChangeListener() {
- @Override
- public void stateChanged(ChangeEvent event) {
- JSlider source = (JSlider) event.getSource();
- if (!source.getValueIsAdjusting()) {
- int fps = (int) source.getValue();
- if (fps == 0) {
- paused = true;
- pauseButton.setText("Unpause");
- } else {
- paused = false;
- fps_now = fps;
- pauseButton.setText("Pause");
- }
- }
- }
- };
- framesPerSecond.addChangeListener(fpschange);
- framesPerSecond.setMajorTickSpacing(10);
- framesPerSecond.setMinorTickSpacing(1);
- framesPerSecond.setPaintTicks(true);
- framesPerSecond.setPaintLabels(true);
- framesPerSecond.setFont(new Font("Arial",1,12));
- framesPerSecond.setPreferredSize(new Dimension(100,0));
- menuBar.add(framesPerSecond);
-
- menuBar.setPreferredSize(new Dimension(width * scale, 40));
- setPreferredSize(new Dimension(width * scale, height * scale));
- _frame = new JFrame();
- _frame.setJMenuBar(menuBar);
- _frame.setResizable(false);
- _frame.setTitle(title);
- _frame.add(this);
- _frame.pack();
- _frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- _frame.setLocationRelativeTo(null);
- _frame.setVisible(true);
- }
-
- private void update() {
- int state;
- int states = Goldfish.getMaxStates(rule);
- for (int i = 0; i < width; i++) {
- for (int j = 0; j < height; j++) {
- state = _grid.getPatch(i, j).getState();
- draw(i, j, (int) ((state / ((double) states - 1)) * 0xffffff));
- }
- }
- }
-
- public void run() {
- BufferStrategy bs;
- Graphics g;
-
- bs = getBufferStrategy();
- if (bs == null) {
- createBufferStrategy(1);
- return;
- }
-
- update();
-
- g = bs.getDrawGraphics();
- g.drawImage(_image, 0, 0, getWidth(), getHeight(), null);
- g.dispose();
- bs.show();
- }
-
- public void sleep() {
- long since = System.currentTimeMillis() - _lastTick;
- if (since < 1000 / fps_now) {
- try {
- Thread.sleep(1000 / fps_now - since);
- } catch (InterruptedException e) {
- return;
- }
- }
- _lastTick = System.currentTimeMillis();
- }
-
- public void setGrid(Grid g) {
- _grid = g;
- }
-
- public void draw(int x, int y, int color) {
- if (_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);
- }
- }
- }
-
- public void randomize() {
- for (int i = 0; i < _grid.getHeight(); i++) {
- for (int j = 0; j < _grid.getWidth(); j++) {
- _grid.getPatch(i, j).setState(random.nextInt(Goldfish.getMaxStates(rule)));
- }
- }
- }
-
- private void mouseDraw(MouseEvent e) {
- int states = Goldfish.getMaxStates(rule);
- if (e.getX() < 0 || e.getY() < 0 || e.getX() / scale >= width || e.getY() / scale >= height)
- return;
- Patch p = _grid.getPatch(e.getX()/scale, e.getY()/scale);
- if (_lastPatch != p) {
- if (p.getState() != 0) {
- p.setState(0);
- } else {
- if (SwingUtilities.isLeftMouseButton(e)) {
- p.setState(states - 1);
- } else if (SwingUtilities.isRightMouseButton(e)) {
- p.setState(1);
- }
- }
- _lastPatch = p;
- }
- e.consume();
- }
-
- @Override
- public void mouseDragged(MouseEvent e) {
- mouseDraw(e);
- }
-
- @Override
- public void mouseClicked(MouseEvent e) {
- }
-
- @Override
- public void mouseEntered(MouseEvent e) {
- }
-
- @Override
- public void mouseExited(MouseEvent e) {
- }
-
- @Override
- public void mousePressed(MouseEvent e) {
- mouseDraw(e);
- }
-
- @Override
- public void mouseReleased(MouseEvent e) {
- _lastPatch = null;
- }
-
- @Override
- public void mouseMoved(MouseEvent e) {
- }
-
- @Override
- public void actionPerformed(ActionEvent event) {
- 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 ("random".equals(event.getActionCommand())) {
- randomize();
- } else if ("clear".equals(event.getActionCommand())) {
- clear();
- } else {
- String oldRule = rule;
- rule = event.getActionCommand();
- if (oldRule.equals("Brian's Brain") && !rule.equals("Brian's Brain")) {
- for (int i = 0; i < _grid.getWidth(); i++) {
- for (int j = 0; j < _grid.getHeight(); j++) {
- if (_grid.getPatch(i,j).getState() == 2)
- _grid.getPatch(i,j).setState(1);
- }
- }
- } else if (!oldRule.equals("Brian's Brain") && rule.equals("Brian's Brain")) {
- for (int i = 0; i < _grid.getWidth(); i++) {
- for (int j = 0; j < _grid.getHeight(); j++) {
- if (_grid.getPatch(i,j).getState() == 1)
- _grid.getPatch(i,j).setState(2);
- }
- }
- }
- title = "Goldfish: " + rule;
- _frame.setTitle(title);
- }
- }
- }
|