A simple Game of Life implementation in Java
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Render.java 9.1 KiB

11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
11 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. menuAlgo.setFont(new Font("Courier New",1,12));
  69. menuAlgo.setPreferredSize(new Dimension(85,0));
  70. for (String rule : _rules) {
  71. JMenuItem menuAlgoItem = new JMenuItem(rule);
  72. menuAlgo.add(menuAlgoItem);
  73. menuAlgoItem.addActionListener(this);
  74. }
  75. menuBar.add(menuAlgo);
  76. pauseButton = new JButton("Pause");
  77. pauseButton.setActionCommand("pause");
  78. pauseButton.setFont(new Font("Courier New",1,12));
  79. pauseButton.setPreferredSize(new Dimension(90,0));
  80. menuBar.add(pauseButton);
  81. pauseButton.addActionListener(this);
  82. JButton resetButton = new JButton("Reset");
  83. resetButton.setActionCommand("reset");
  84. resetButton.setFont(new Font("Courier New",1,12));
  85. menuBar.add(resetButton);
  86. resetButton.addActionListener(this);
  87. JButton randomButton = new JButton("Random");
  88. randomButton.setActionCommand("random");
  89. randomButton.setFont(new Font("Courier New",1,12));
  90. menuBar.add(randomButton);
  91. randomButton.addActionListener(this);
  92. JButton clearButton = new JButton("Clear");
  93. clearButton.setActionCommand("clear");
  94. clearButton.setFont(new Font("Courier New",1,12));
  95. menuBar.add(clearButton);
  96. clearButton.addActionListener(this);
  97. JSlider framesPerSecond = new JSlider(JSlider.HORIZONTAL, 0, 30, 15);
  98. ChangeListener fpschange = new ChangeListener() {
  99. @Override
  100. public void stateChanged(ChangeEvent event) {
  101. JSlider source = (JSlider) event.getSource();
  102. if (!source.getValueIsAdjusting()) {
  103. int fps = (int) source.getValue();
  104. if (fps == 0) {
  105. paused = true;
  106. pauseButton.setText("Unpause");
  107. } else {
  108. paused = false;
  109. fps_now = fps;
  110. pauseButton.setText("Pause");
  111. }
  112. }
  113. }
  114. };
  115. framesPerSecond.addChangeListener(fpschange);
  116. framesPerSecond.setMajorTickSpacing(10);
  117. framesPerSecond.setMinorTickSpacing(1);
  118. framesPerSecond.setPaintTicks(true);
  119. framesPerSecond.setPaintLabels(true);
  120. framesPerSecond.setFont(new Font("Courier New",1,12));
  121. framesPerSecond.setPreferredSize(new Dimension(100,0));
  122. menuBar.add(framesPerSecond);
  123. menuBar.setPreferredSize(new Dimension(width * scale, 35));
  124. setPreferredSize(new Dimension(width * scale, height * scale));
  125. _frame = new JFrame();
  126. _frame.setJMenuBar(menuBar);
  127. _frame.setResizable(false);
  128. _frame.setTitle(title);
  129. _frame.add(this);
  130. _frame.pack();
  131. _frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  132. _frame.setLocationRelativeTo(null);
  133. _frame.setVisible(true);
  134. }
  135. private void update() {
  136. int state;
  137. int states = Goldfish.getMaxStates(rule);
  138. for (int i = 0; i < width; i++) {
  139. for (int j = 0; j < height; j++) {
  140. state = _grid.getPatch(i, j).getState();
  141. if (_pixels[i + j * width] == state) {
  142. } else {
  143. draw(i, j, (int) ((state / ((double) states - 1)) * 0xffffff));
  144. }
  145. }
  146. }
  147. }
  148. public void run() {
  149. BufferStrategy bs;
  150. Graphics g;
  151. bs = getBufferStrategy();
  152. if (bs == null) {
  153. createBufferStrategy(1);
  154. return;
  155. }
  156. update();
  157. g = bs.getDrawGraphics();
  158. g.drawImage(_image, 0, 0, getWidth(), getHeight(), null);
  159. g.dispose();
  160. bs.show();
  161. }
  162. public void sleep() {
  163. long since = System.currentTimeMillis() - _lastTick;
  164. if (since < 1000 / fps_now) {
  165. try {
  166. Thread.sleep(1000 / fps_now - since);
  167. } catch (InterruptedException e) {
  168. return;
  169. }
  170. }
  171. _lastTick = System.currentTimeMillis();
  172. }
  173. public void setGrid(Grid g) {
  174. _grid = g;
  175. }
  176. public void draw(int x, int y, int color) {
  177. _pixels[x + y * width] = color;
  178. }
  179. public void clear() {
  180. for (int i = 0; i < _grid.getHeight(); i++) {
  181. for (int j = 0; j < _grid.getWidth(); j++) {
  182. _grid.getPatch(i, j).setState(0);
  183. }
  184. }
  185. }
  186. public void randomize() {
  187. for (int i = 0; i < _grid.getHeight(); i++) {
  188. for (int j = 0; j < _grid.getWidth(); j++) {
  189. _grid.getPatch(i, j).setState(random.nextInt(Goldfish.getMaxStates(rule)));
  190. }
  191. }
  192. }
  193. private void mouseDraw(MouseEvent e) {
  194. int states = Goldfish.getMaxStates(rule);
  195. if (e.getX() < 0 || e.getY() < 0 || e.getX() / scale >= width || e.getY() / scale >= height)
  196. return;
  197. Patch p = _grid.getPatch(e.getX()/scale, e.getY()/scale);
  198. if (_lastPatch != p) {
  199. if (SwingUtilities.isLeftMouseButton(e)) {
  200. if (p.getState() != 0) {
  201. p.setState(0);
  202. } else {
  203. p.setState(states - 1);
  204. }
  205. } else if (SwingUtilities.isRightMouseButton(e)) {
  206. if (p.getState() != 0) {
  207. p.setState(0);
  208. } else {
  209. p.setState(1);
  210. }
  211. }
  212. _lastPatch = p;
  213. }
  214. e.consume();
  215. }
  216. @Override
  217. public void mouseDragged(MouseEvent e) {
  218. mouseDraw(e);
  219. }
  220. @Override
  221. public void mouseClicked(MouseEvent e) {
  222. }
  223. @Override
  224. public void mouseEntered(MouseEvent e) {
  225. }
  226. @Override
  227. public void mouseExited(MouseEvent e) {
  228. }
  229. @Override
  230. public void mousePressed(MouseEvent e) {
  231. mouseDraw(e);
  232. }
  233. @Override
  234. public void mouseReleased(MouseEvent e) {
  235. _lastPatch = null;
  236. }
  237. @Override
  238. public void mouseMoved(MouseEvent e) {
  239. }
  240. @Override
  241. public void actionPerformed(ActionEvent event) {
  242. if ("pause".equals(event.getActionCommand())) {
  243. if (paused) {
  244. paused = false;
  245. pauseButton.setText("Pause");
  246. } else {
  247. paused = true;
  248. pauseButton.setText("Unpause");
  249. }
  250. } else if ("reset".equals(event.getActionCommand())) {
  251. clear();
  252. reset = true;
  253. } else if ("random".equals(event.getActionCommand())) {
  254. randomize();
  255. } else if ("clear".equals(event.getActionCommand())) {
  256. clear();
  257. } else {
  258. rule = event.getActionCommand();
  259. title = "Goldfish: " + rule;
  260. _frame.setTitle(title);
  261. }
  262. }
  263. }