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.

134 lines
3.0 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 int width;
  12. public static int height;
  13. public static int scale;
  14. public String title = "Goldfish";
  15. private JFrame frame;
  16. private BufferedImage image;
  17. private Grid _grid;
  18. private int[] pixels;
  19. public Render(int width, int height, Grid g) {
  20. Render.width = width;
  21. Render.height = height;
  22. setScale();
  23. _grid = g;
  24. image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  25. pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
  26. Dimension size = new Dimension(width * scale, height * scale);
  27. setPreferredSize(size);
  28. setFrame();
  29. }
  30. public Render(int width, int height) {
  31. Render.width = width;
  32. Render.height = height;
  33. setScale();
  34. _grid = new Grid(width, height);
  35. image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  36. pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
  37. Dimension size = new Dimension(width * scale, height * scale);
  38. setPreferredSize(size);
  39. setFrame();
  40. }
  41. public Render() {
  42. width = 320;
  43. height = 240;
  44. setScale();
  45. _grid = new Grid(10, 10);
  46. image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  47. pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
  48. Dimension size = new Dimension(width * scale, height * scale);
  49. setPreferredSize(size);
  50. frame = new JFrame();
  51. }
  52. private void setScale() {
  53. if (height < 128 || width < 128) {
  54. Render.scale = 3;
  55. } else if (height < 256 || width < 256) {
  56. Render.scale = 2;
  57. }
  58. }
  59. private void setFrame() {
  60. frame = new JFrame();
  61. frame.setResizable(false);
  62. frame.setTitle(title);
  63. frame.add(this);
  64. frame.pack();
  65. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  66. frame.setLocationRelativeTo(null);
  67. frame.setVisible(true);
  68. }
  69. public void run() {
  70. render();
  71. }
  72. public void update() {
  73. for (int i = 0; i < width; i++) {
  74. for (int j = 0; j < height; j++) {
  75. draw(i, j, _grid.getPatch(i, j).getState() * 0xffffff);
  76. }
  77. }
  78. }
  79. BufferStrategy bs;
  80. Graphics g;
  81. public void render() {
  82. bs = getBufferStrategy();
  83. if (bs == null) {
  84. createBufferStrategy(1);
  85. return;
  86. }
  87. clear();
  88. update();
  89. g = bs.getDrawGraphics();
  90. g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
  91. g.dispose();
  92. bs.show();
  93. }
  94. public void clear() {
  95. for (int x = 0; x < pixels.length; x++) {
  96. pixels[x] = 0;
  97. }
  98. }
  99. public void setGrid(Grid g) {
  100. _grid = g;
  101. }
  102. public void draw(int x, int y, int color) {
  103. pixels[x + y * width] = color;
  104. }
  105. public static void main(String[] args) {
  106. Render render = new Render(320, 240);
  107. while (true) {
  108. render.run();
  109. }
  110. }
  111. }