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.

преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package edu.stuy.goldfish;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.awt.image.*;
  5. import javax.swing.*;
  6. public class Render extends Canvas implements Runnable, MouseListener,
  7. MouseMotionListener, ActionListener, ItemListener {
  8. private static final long serialVersionUID = 1L;
  9. public static String title = "Goldfish";
  10. public static int width;
  11. public static int height;
  12. public static int scale;
  13. public static int max_fps = 15;
  14. public boolean paused;
  15. public String rule;
  16. private Grid _grid;
  17. private BufferedImage _image;
  18. private int[] _pixels;
  19. private long _lastTick;
  20. private String[] _rules;
  21. private JFrame _frame;
  22. public Render(int width, int height, Grid g, String[] rules) {
  23. addMouseListener(this);
  24. addMouseMotionListener(this);
  25. Render.width = width;
  26. Render.height = height;
  27. setScale();
  28. paused = false;
  29. rule = rules[0];
  30. _grid = g;
  31. _image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  32. _pixels = ((DataBufferInt) _image.getRaster().getDataBuffer()).getData();
  33. _lastTick = 0;
  34. _rules = rules;
  35. Dimension size = new Dimension(width * scale, height * scale);
  36. setPreferredSize(size);
  37. setFrame();
  38. }
  39. public Render(int width, int height, Grid g) {
  40. this(width, height, g, new String[0]);
  41. }
  42. public Render(int width, int height) {
  43. this(width, height, new Grid(width, height));
  44. }
  45. public Render() {
  46. this(256, 256);
  47. }
  48. private void setScale() {
  49. if (height <= 128 || width <= 128) {
  50. Render.scale = 4;
  51. } else if (height <= 256 || width <= 256) {
  52. Render.scale = 2;
  53. } else {
  54. Render.scale = 1;
  55. }
  56. }
  57. private void setFrame() {
  58. JMenuBar menuBar = new JMenuBar();
  59. JMenu menuMain = new JMenu("Menu");
  60. JMenu menuAlgo = new JMenu("Algorithms");
  61. for (String rule : _rules) {
  62. JMenuItem menuAlgoItem = new JMenuItem(rule);
  63. menuAlgo.add(menuAlgoItem);
  64. menuAlgoItem.addActionListener(this);
  65. }
  66. menuMain.add(menuAlgo);
  67. menuBar.add(menuMain);
  68. JCheckBoxMenuItem pauseButton = new JCheckBoxMenuItem("Pause");
  69. menuBar.add(pauseButton);
  70. pauseButton.addItemListener(this);
  71. _frame = new JFrame();
  72. _frame.setJMenuBar(menuBar);
  73. _frame.setResizable(false);
  74. _frame.setTitle(title);
  75. _frame.add(this);
  76. _frame.pack();
  77. _frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  78. _frame.setLocationRelativeTo(null);
  79. _frame.setVisible(true);
  80. }
  81. private void update() {
  82. int state;
  83. for (int i = 0; i < width; i++) {
  84. for (int j = 0; j < height; j++) {
  85. state = _grid.getPatch(i, j).getState();
  86. if(_pixels[i + j * width] == state) {
  87. } else {
  88. draw(i, j, state * 0xffffff);
  89. }
  90. }
  91. }
  92. }
  93. public void run() {
  94. BufferStrategy bs;
  95. Graphics g;
  96. bs = getBufferStrategy();
  97. if (bs == null) {
  98. createBufferStrategy(1);
  99. return;
  100. }
  101. update();
  102. g = bs.getDrawGraphics();
  103. g.drawImage(_image, 0, 0, getWidth(), getHeight(), null);
  104. g.dispose();
  105. bs.show();
  106. }
  107. public void sleep() {
  108. long since = System.currentTimeMillis() - _lastTick;
  109. if (since < 1000 / max_fps) {
  110. try {
  111. Thread.sleep(1000 / max_fps - since);
  112. } catch (InterruptedException e) {
  113. return;
  114. }
  115. }
  116. _lastTick = System.currentTimeMillis();
  117. }
  118. public void setGrid(Grid g) {
  119. _grid = g;
  120. }
  121. public void draw(int x, int y, int color) {
  122. _pixels[x + y * width] = color;
  123. }
  124. @Override
  125. public void mouseDragged(MouseEvent e) {
  126. if (e.getX() < 0 || e.getY() < 0 || e.getX() / scale > width || e.getY() / scale > height)
  127. return;
  128. _grid.getPatch(e.getX() / scale, e.getY() / scale).setState(1);
  129. e.consume();
  130. }
  131. @Override
  132. public void mouseClicked(MouseEvent e) {
  133. _grid.getPatch(e.getX() / scale, e.getY() / scale).setState(1);
  134. e.consume();
  135. }
  136. @Override
  137. public void mouseEntered(MouseEvent e) {
  138. }
  139. @Override
  140. public void mouseExited(MouseEvent e) {
  141. }
  142. @Override
  143. public void mousePressed(MouseEvent e) {
  144. _grid.getPatch(e.getX() / scale, e.getY() / scale).setState(1);
  145. e.consume();
  146. }
  147. @Override
  148. public void mouseReleased(MouseEvent e) {
  149. }
  150. @Override
  151. public void mouseMoved(MouseEvent e) {
  152. }
  153. // Affects the algorithms menu
  154. @Override
  155. public void actionPerformed(ActionEvent event) {
  156. rule = event.getActionCommand();
  157. }
  158. // Affects the pause button
  159. @Override
  160. public void itemStateChanged(ItemEvent event) {
  161. if (event.getStateChange() == ItemEvent.SELECTED)
  162. paused = true;
  163. else
  164. paused = false;
  165. return;
  166. }
  167. }