A simple Game of Life implementation in Java
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

273 lines
8.2 KiB

  1. package edu.stuy.goldfish;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.awt.image.*;
  5. import javax.swing.*;
  6. import javax.swing.event.*;
  7. import java.util.Random;
  8. public class Render extends Canvas implements Runnable, MouseListener,
  9. MouseMotionListener, ActionListener {
  10. private static final long serialVersionUID = 1L;
  11. public static String title = "Goldfish";
  12. public static int width;
  13. public static int height;
  14. public static int scale;
  15. public int fps_now;
  16. public boolean paused;
  17. public String rule;
  18. public boolean reset;
  19. private Grid _grid;
  20. private int[] _pixels;
  21. private BufferedImage _image;
  22. private long _lastTick;
  23. private String[] _rules;
  24. private JFrame _frame;
  25. private JButton pauseButton;
  26. private Random random = new Random();
  27. public Render(int width, int height, Grid g, String[] rules) {
  28. addMouseListener(this);
  29. addMouseMotionListener(this);
  30. Render.width = width;
  31. Render.height = height;
  32. setScale();
  33. paused = false;
  34. reset = false;
  35. rule = rules[0];
  36. _grid = g;
  37. _image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  38. _pixels = ((DataBufferInt) _image.getRaster().getDataBuffer()).getData();
  39. _lastTick = 0;
  40. _rules = rules;
  41. fps_now = 15;
  42. setFrame();
  43. }
  44. public Render(int width, int height, Grid g) {
  45. this(width, height, g, new String[0]);
  46. }
  47. public Render(int width, int height) {
  48. this(width, height, new Grid(width, height));
  49. }
  50. public Render() {
  51. this(256, 256);
  52. }
  53. private void setScale() {
  54. if (height <= 128 || width <= 128) {
  55. Render.scale = 4;
  56. } else if (height <= 256 || width <= 256) {
  57. Render.scale = 2;
  58. } else {
  59. Render.scale = 1;
  60. }
  61. }
  62. private void setFrame() {
  63. JMenuBar menuBar = new JMenuBar();
  64. JMenu menuAlgo = new JMenu("Algorithms");
  65. for (String rule : _rules) {
  66. JMenuItem menuAlgoItem = new JMenuItem(rule);
  67. menuAlgo.add(menuAlgoItem);
  68. menuAlgoItem.addActionListener(this);
  69. }
  70. menuBar.add(menuAlgo);
  71. pauseButton = new JButton("Pause");
  72. pauseButton.setActionCommand("pause");
  73. pauseButton.setPreferredSize(new Dimension(90,0));
  74. menuBar.add(pauseButton);
  75. pauseButton.addActionListener(this);
  76. JButton resetButton = new JButton("Reset");
  77. resetButton.setActionCommand("reset");
  78. menuBar.add(resetButton);
  79. resetButton.addActionListener(this);
  80. JButton randomButton = new JButton("Random");
  81. randomButton.setActionCommand("random");
  82. menuBar.add(randomButton);
  83. randomButton.addActionListener(this);
  84. JButton clearButton = new JButton("Clear");
  85. clearButton.setActionCommand("clear");
  86. menuBar.add(clearButton);
  87. clearButton.addActionListener(this);
  88. JSlider framesPerSecond = new JSlider(JSlider.HORIZONTAL, 0, 30, 15);
  89. ChangeListener fpschange = new ChangeListener() {
  90. @Override
  91. public void stateChanged(ChangeEvent event) {
  92. JSlider source = (JSlider) event.getSource();
  93. if (!source.getValueIsAdjusting()) {
  94. int fps = (int) source.getValue();
  95. if (fps == 0) {
  96. paused = true;
  97. pauseButton.setText("Unpause");
  98. } else {
  99. paused = false;
  100. fps_now = fps;
  101. pauseButton.setText("Pause");
  102. }
  103. }
  104. }
  105. };
  106. framesPerSecond.addChangeListener(fpschange);
  107. framesPerSecond.setMajorTickSpacing(10);
  108. framesPerSecond.setMinorTickSpacing(1);
  109. framesPerSecond.setPaintTicks(true);
  110. framesPerSecond.setPaintLabels(true);
  111. framesPerSecond.setPreferredSize(new Dimension(100,40));
  112. menuBar.add(framesPerSecond);
  113. setPreferredSize(new Dimension(width * scale, height * scale));
  114. _frame = new JFrame();
  115. _frame.setJMenuBar(menuBar);
  116. _frame.setResizable(false);
  117. _frame.setTitle(title);
  118. _frame.add(this);
  119. _frame.pack();
  120. _frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  121. _frame.setLocationRelativeTo(null);
  122. _frame.setVisible(true);
  123. }
  124. private void update() {
  125. int state;
  126. int states = Goldfish.getMaxStates(rule);
  127. for (int i = 0; i < width; i++) {
  128. for (int j = 0; j < height; j++) {
  129. state = _grid.getPatch(i, j).getState();
  130. if (_pixels[i + j * width] == state) {
  131. } else {
  132. draw(i, j, (int) ((state / ((double) states - 1)) * 0xffffff));
  133. }
  134. }
  135. }
  136. }
  137. public void run() {
  138. BufferStrategy bs;
  139. Graphics g;
  140. bs = getBufferStrategy();
  141. if (bs == null) {
  142. createBufferStrategy(1);
  143. return;
  144. }
  145. update();
  146. g = bs.getDrawGraphics();
  147. g.drawImage(_image, 0, 0, getWidth(), getHeight(), null);
  148. g.dispose();
  149. bs.show();
  150. }
  151. public void sleep() {
  152. long since = System.currentTimeMillis() - _lastTick;
  153. if (since < 1000 / fps_now) {
  154. try {
  155. Thread.sleep(1000 / fps_now - since);
  156. } catch (InterruptedException e) {
  157. return;
  158. }
  159. }
  160. _lastTick = System.currentTimeMillis();
  161. }
  162. public void setGrid(Grid g) {
  163. _grid = g;
  164. }
  165. public void draw(int x, int y, int color) {
  166. _pixels[x + y * width] = color;
  167. }
  168. public void clear() {
  169. for (int i = 0; i < _grid.getHeight(); i++) {
  170. for (int j = 0; j < _grid.getWidth(); j++) {
  171. _grid.getPatch(i, j).setState(0);
  172. }
  173. }
  174. }
  175. public void randomize() {
  176. for (int i = 0; i < _grid.getHeight(); i++) {
  177. for (int j = 0; j < _grid.getWidth(); j++) {
  178. _grid.getPatch(i, j).setState(random.nextInt(Goldfish.getMaxStates(rule)));
  179. }
  180. }
  181. }
  182. @Override
  183. public void mouseDragged(MouseEvent e) {
  184. int states = Goldfish.getMaxStates(rule) - 1;
  185. if (e.getX() < 0 || e.getY() < 0 || e.getX() / scale > width || e.getY() / scale > height)
  186. return;
  187. _grid.getPatch(e.getX() / scale, e.getY() / scale).setState(states);
  188. e.consume();
  189. }
  190. @Override
  191. public void mouseClicked(MouseEvent e) {
  192. int states = Goldfish.getMaxStates(rule) - 1;
  193. _grid.getPatch(e.getX() / scale, e.getY() / scale).setState(states);
  194. e.consume();
  195. }
  196. @Override
  197. public void mouseEntered(MouseEvent e) {
  198. }
  199. @Override
  200. public void mouseExited(MouseEvent e) {
  201. }
  202. @Override
  203. public void mousePressed(MouseEvent e) {
  204. int states = Goldfish.getMaxStates(rule) - 1;
  205. _grid.getPatch(e.getX() / scale, e.getY() / scale).setState(states);
  206. e.consume();
  207. }
  208. @Override
  209. public void mouseReleased(MouseEvent e) {
  210. }
  211. @Override
  212. public void mouseMoved(MouseEvent e) {
  213. }
  214. @Override
  215. public void actionPerformed(ActionEvent event) {
  216. if ("pause".equals(event.getActionCommand())) {
  217. if (paused) {
  218. paused = false;
  219. pauseButton.setText("Pause");
  220. } else {
  221. paused = true;
  222. pauseButton.setText("Unpause");
  223. }
  224. } else if ("reset".equals(event.getActionCommand())) {
  225. clear();
  226. reset = true;
  227. } else if ("random".equals(event.getActionCommand())) {
  228. randomize();
  229. } else if ("clear".equals(event.getActionCommand())) {
  230. clear();
  231. } else {
  232. rule = event.getActionCommand();
  233. }
  234. }
  235. }