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.

Render.java 8.4 KiB

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