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.

122 lines
2.7 KiB

  1. package edu.stuy.goldfish;
  2. import java.util.Date;
  3. import java.awt.Canvas;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.image.BufferStrategy;
  7. import java.awt.image.BufferedImage;
  8. import java.awt.image.DataBufferInt;
  9. import javax.swing.JFrame;
  10. public class Render extends Canvas implements Runnable {
  11. private static final long serialVersionUID = 1L;
  12. public static String title = "Goldfish";
  13. public static int width;
  14. public static int height;
  15. public static int scale;
  16. public static int max_fps = 15;
  17. private JFrame _frame;
  18. private BufferedImage _image;
  19. private Grid _grid;
  20. private int[] _pixels;
  21. private long _last_tick;
  22. public Render(int width, int height, Grid g) {
  23. Render.width = width;
  24. Render.height = height;
  25. setScale();
  26. _grid = g;
  27. _image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  28. _pixels = ((DataBufferInt) _image.getRaster().getDataBuffer()).getData();
  29. _last_tick = 0;
  30. Dimension size = new Dimension(width * scale, height * scale);
  31. setPreferredSize(size);
  32. setFrame();
  33. }
  34. public Render(int width, int height) {
  35. this(width, height, new Grid(width, height));
  36. }
  37. public Render() {
  38. this(256, 256, new Grid(128, 128));
  39. }
  40. private void setScale() {
  41. if (height <= 128 || width <= 128) {
  42. Render.scale = 3;
  43. } else if (height <= 256 || width <= 256) {
  44. Render.scale = 2;
  45. }
  46. }
  47. private void setFrame() {
  48. _frame = new JFrame();
  49. _frame.setResizable(false);
  50. _frame.setTitle(title);
  51. _frame.add(this);
  52. _frame.pack();
  53. _frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  54. _frame.setLocationRelativeTo(null);
  55. _frame.setVisible(true);
  56. }
  57. private void clear() {
  58. for (int x = 0; x < _pixels.length; x++) {
  59. _pixels[x] = 0;
  60. }
  61. }
  62. private void update() {
  63. for (int i = 0; i < width; i++) {
  64. for (int j = 0; j < height; j++) {
  65. draw(i, j, _grid.getPatch(i, j).getState() * 0xffffff);
  66. }
  67. }
  68. }
  69. public void run() {
  70. BufferStrategy bs;
  71. Graphics g;
  72. bs = getBufferStrategy();
  73. if (bs == null) {
  74. createBufferStrategy(1);
  75. return;
  76. }
  77. clear();
  78. update();
  79. g = bs.getDrawGraphics();
  80. g.drawImage(_image, 0, 0, getWidth(), getHeight(), null);
  81. g.dispose();
  82. bs.show();
  83. }
  84. public void sleep() {
  85. long since = (new Date()).getTime() - _last_tick;
  86. if (since < 1000 / max_fps) {
  87. try {
  88. Thread.sleep(1000 / max_fps - since);
  89. }
  90. catch (InterruptedException e) {
  91. return;
  92. }
  93. }
  94. _last_tick = (new Date()).getTime();
  95. }
  96. public void setGrid(Grid g) {
  97. _grid = g;
  98. }
  99. public void draw(int x, int y, int color) {
  100. _pixels[x + y * width] = color;
  101. }
  102. }