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.

Render.java 3.0 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import java.awt.Canvas;
  2. import java.awt.Dimension;
  3. import java.awt.Graphics;
  4. import java.awt.image.BufferStrategy;
  5. import java.awt.image.BufferedImage;
  6. import java.awt.image.DataBufferInt;
  7. import javax.swing.JFrame;
  8. public class Render extends Canvas implements Runnable {
  9. private static final long serialVersionUID = 1L;
  10. public static int width;
  11. public static int height;
  12. public static int scale = 2;
  13. public String title = "Title";
  14. private Thread thread;
  15. private JFrame frame;
  16. private boolean running = false;
  17. private Screen screen;
  18. private BufferedImage image;
  19. private int[] pixels;
  20. public Render(int width, int height) {
  21. this.width = width;
  22. this.height = height;
  23. image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  24. pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
  25. Dimension size = new Dimension(width*scale, height*scale);
  26. setPreferredSize(size);
  27. screen = new Screen(width, height);
  28. frame = new JFrame();
  29. }
  30. public Render() {
  31. width = 320;
  32. height = 240;
  33. image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  34. pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
  35. Dimension size = new Dimension(width*scale, height*scale);
  36. setPreferredSize(size);
  37. screen = new Screen(width, height);
  38. frame = new JFrame();
  39. }
  40. public synchronized void start() {
  41. running = true;
  42. thread = new Thread(this, "Display");
  43. thread.start();
  44. }
  45. public synchronized void stop() {
  46. running = false;
  47. try {
  48. thread.join();
  49. } catch (InterruptedException e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. public void run() {
  54. long lastTime = System.nanoTime();
  55. long timer = System.currentTimeMillis();
  56. double delta = 0;
  57. int frames = 0;
  58. int updates = 0;
  59. long now;
  60. while (running) {
  61. now = System.nanoTime();
  62. delta += (now - lastTime) * 6.0 / 100000000.0;
  63. lastTime = now;
  64. while (delta >= 1) {
  65. update();
  66. updates++;
  67. delta--;
  68. render();
  69. frames++;
  70. }
  71. if (System.currentTimeMillis() - timer > 1000) {
  72. timer += 1000;
  73. frame.setTitle(title + " | updates: " + updates + " | frames: "+ frames);
  74. frames = 0;
  75. updates = 0;
  76. }
  77. }
  78. stop();
  79. }
  80. public void update() {
  81. }
  82. public void render() {
  83. BufferStrategy bs = getBufferStrategy();
  84. if (bs == null) {
  85. createBufferStrategy(3);
  86. return;
  87. }
  88. screen.clear();
  89. screen.render();
  90. for (int i = 0; i < pixels.length; i++) {
  91. pixels[i] = screen.pixels[i];
  92. }
  93. Graphics g = bs.getDrawGraphics();
  94. g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
  95. g.dispose();
  96. bs.show();
  97. }
  98. public static void main(String[] args) {
  99. Render render = new Render(160,120);
  100. render.frame.setResizable(false);
  101. render.frame.setTitle(render.title);
  102. render.frame.add(render);
  103. render.frame.pack();
  104. render.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  105. render.frame.setLocationRelativeTo(null);
  106. render.frame.setVisible(true);
  107. render.start();
  108. }
  109. }