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.

121 lines
2.6 KiB

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