A simple Game of Life implementation in Java
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

131 lignes
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 java.util.Random;
  9. import javax.swing.JFrame;
  10. public class Render extends Canvas implements Runnable {
  11. private static final long serialVersionUID = 1L;
  12. public static int width;
  13. public static int height;
  14. public static int scale = 2;
  15. public String title = "Goldfish";
  16. private JFrame frame;
  17. private BufferedImage image;
  18. private Grid _grid;
  19. private int[] pixels;
  20. Random random = new Random();
  21. public Render(int width, int height, Grid g) {
  22. Render.width = width;
  23. Render.height = height;
  24. _grid = g;
  25. image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  26. pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
  27. Dimension size = new Dimension(width * scale, height * scale);
  28. setPreferredSize(size);
  29. setFrame();
  30. }
  31. public Render(int width, int height) {
  32. Render.width = width;
  33. Render.height = height;
  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. _grid = new Grid(10, 10);
  45. image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  46. pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
  47. Dimension size = new Dimension(width * scale, height * scale);
  48. setPreferredSize(size);
  49. frame = new JFrame();
  50. }
  51. private void setFrame() {
  52. frame = new JFrame();
  53. frame.setResizable(false);
  54. frame.setTitle(title);
  55. frame.add(this);
  56. frame.pack();
  57. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  58. frame.setLocationRelativeTo(null);
  59. frame.setVisible(true);
  60. }
  61. public void run() {
  62. render();
  63. }
  64. public void update() {
  65. for (int i = 0; i < width; i++) {
  66. for (int j = 0; j < height; j++) {
  67. // draw(i,j,random.nextInt());
  68. // Patch x = new Patch();
  69. // x.setState(0x13bd76);
  70. // _grid.setPatch(i,j,x);
  71. draw(i, j, _grid.getPatch(i, j).getState() * 0xffffff);
  72. }
  73. }
  74. }
  75. BufferStrategy bs;
  76. Graphics g;
  77. public void render() {
  78. bs = getBufferStrategy();
  79. if (bs == null) {
  80. createBufferStrategy(3);
  81. return;
  82. }
  83. clear();
  84. update();
  85. g = bs.getDrawGraphics();
  86. g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
  87. g.dispose();
  88. bs.show();
  89. }
  90. public void clear() {
  91. for (int x = 0; x < pixels.length; x++) {
  92. pixels[x] = 0;
  93. }
  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. public static void main(String[] args) {
  102. Render render = new Render(320, 240);
  103. while (true) {
  104. render.run();
  105. }
  106. }
  107. }