A simple Game of Life implementation in Java
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
11 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. import edu.stuy.goldfish.rules.*;
  11. public class Render extends Canvas implements Runnable {
  12. private static final long serialVersionUID = 1L;
  13. public static int width;
  14. public static int height;
  15. public static int scale = 2;
  16. public String title = "Title";
  17. private Thread thread;
  18. private JFrame frame;
  19. private boolean running = false;
  20. private BufferedImage image;
  21. private Grid _grid;
  22. private int[] pixels;
  23. Random random = new Random();
  24. public Render(int width, int height) {
  25. Render.width = width;
  26. Render.height = height;
  27. _grid = new Grid(width, height);
  28. image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  29. pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
  30. Dimension size = new Dimension(width*scale, height*scale);
  31. setPreferredSize(size);
  32. frame = new JFrame();
  33. }
  34. public Render() {
  35. width = 320;
  36. height = 240;
  37. _grid = new Grid(10, 10);
  38. image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  39. pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
  40. Dimension size = new Dimension(width*scale, height*scale);
  41. setPreferredSize(size);
  42. frame = new JFrame();
  43. }
  44. public synchronized void start() {
  45. running = true;
  46. thread = new Thread(this, "Display");
  47. thread.start();
  48. }
  49. public synchronized void stop() {
  50. running = false;
  51. try {
  52. thread.join();
  53. } catch (InterruptedException e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. long lastTime;
  58. long timer;
  59. double delta;
  60. int frames;
  61. int updates;
  62. long now;
  63. public void run() {
  64. lastTime = System.nanoTime();
  65. timer = System.currentTimeMillis();
  66. delta = 0;
  67. frames = 0;
  68. updates = 0;
  69. while (running) {
  70. now = System.nanoTime();
  71. delta += (now - lastTime) * 6.0 / 100000000.0;
  72. lastTime = now;
  73. while (delta >= 1) {
  74. update();
  75. updates++;
  76. delta--;
  77. render();
  78. frames++;
  79. }
  80. if (System.currentTimeMillis() - timer > 1000) {
  81. timer += 1000;
  82. frame.setTitle(title + " | updates: " + updates + " | frames: "+ frames);
  83. frames = 0;
  84. updates = 0;
  85. }
  86. }
  87. stop();
  88. }
  89. public void update() {
  90. //TODO: make it run.
  91. for (int i = 0; i < width; i++) {
  92. for (int j = 0; j < height; j++) {
  93. // _grid.getPatch(i,j).setState(1);
  94. draw(i,j,random.nextInt());
  95. // //draw(i,j,_grid.getPatch(i,j).getState());
  96. }
  97. }
  98. // System.out.println(_grid);
  99. // _grid = Conway.run(_grid);
  100. // System.out.println("------------");
  101. // System.out.println(_grid);
  102. }
  103. BufferStrategy bs;
  104. Graphics g;
  105. public void render() {
  106. bs = getBufferStrategy();
  107. if (bs == null) {
  108. createBufferStrategy(3);
  109. return;
  110. }
  111. clear();
  112. update();
  113. g = bs.getDrawGraphics();
  114. g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
  115. g.dispose();
  116. bs.show();
  117. }
  118. public void clear() {
  119. for (int x = 0; x < pixels.length; x++) {
  120. pixels[x] = 0;
  121. }
  122. }
  123. public void draw(int x, int y, int color) {
  124. pixels[x + y * width] = color;
  125. }
  126. public static void main(String[] args) {
  127. Render render = new Render(320,240);
  128. render.frame.setResizable(false);
  129. render.frame.setTitle(render.title);
  130. render.frame.add(render);
  131. render.frame.pack();
  132. render.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  133. render.frame.setLocationRelativeTo(null);
  134. render.frame.setVisible(true);
  135. render.start();
  136. }
  137. }