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.6 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package edu.stuy.goldfish;
  2. import java.awt.Canvas;
  3. import java.awt.Dimension;
  4. import java.awt.Graphics;
  5. import java.awt.event.MouseEvent;
  6. import java.awt.event.MouseListener;
  7. import java.awt.event.MouseMotionListener;
  8. import java.awt.image.BufferStrategy;
  9. import java.awt.image.BufferedImage;
  10. import java.awt.image.DataBufferInt;
  11. import javax.swing.JFrame;
  12. public class Render extends Canvas implements Runnable, MouseListener,
  13. MouseMotionListener {
  14. private static final long serialVersionUID = 1L;
  15. public static String title = "Goldfish";
  16. public static int width;
  17. public static int height;
  18. public static int scale;
  19. public static int max_fps = 15;
  20. private JFrame _frame;
  21. private BufferedImage _image;
  22. private Grid _grid;
  23. private int[] _pixels;
  24. private long _last_tick;
  25. public Render(int width, int height, Grid g) {
  26. addMouseListener(this);
  27. addMouseMotionListener(this);
  28. Render.width = width;
  29. Render.height = height;
  30. setScale();
  31. _grid = g;
  32. _image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  33. _pixels = ((DataBufferInt) _image.getRaster().getDataBuffer())
  34. .getData();
  35. _last_tick = 0;
  36. Dimension size = new Dimension(width * scale, height * scale);
  37. setPreferredSize(size);
  38. setFrame();
  39. }
  40. public Render(int width, int height) {
  41. this(width, height, new Grid(width, height));
  42. }
  43. public Render() {
  44. this(256, 256);
  45. }
  46. private void setScale() {
  47. if (height <= 128 || width <= 128) {
  48. Render.scale = 4;
  49. } else if (height <= 256 || width <= 256) {
  50. Render.scale = 2;
  51. } else {
  52. Render.scale = 1;
  53. }
  54. }
  55. private void setFrame() {
  56. _frame = new JFrame();
  57. _frame.setResizable(false);
  58. _frame.setTitle(title);
  59. _frame.add(this);
  60. _frame.pack();
  61. _frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  62. _frame.setLocationRelativeTo(null);
  63. _frame.setVisible(true);
  64. }
  65. private void clear() {
  66. for (int x = 0; x < _pixels.length; x++) {
  67. _pixels[x] = 0;
  68. }
  69. }
  70. private void update() {
  71. for (int i = 0; i < width; i++) {
  72. for (int j = 0; j < height; j++) {
  73. draw(i, j, _grid.getPatch(i, j).getState() * 0xffffff);
  74. }
  75. }
  76. }
  77. public void run() {
  78. BufferStrategy bs;
  79. Graphics g;
  80. bs = getBufferStrategy();
  81. if (bs == null) {
  82. createBufferStrategy(1);
  83. return;
  84. }
  85. clear();
  86. update();
  87. g = bs.getDrawGraphics();
  88. g.drawImage(_image, 0, 0, getWidth(), getHeight(), null);
  89. g.dispose();
  90. bs.show();
  91. }
  92. public void sleep() {
  93. long since = System.currentTimeMillis() - _last_tick;
  94. if (since < 1000 / max_fps) {
  95. try {
  96. Thread.sleep(1000 / max_fps - since);
  97. } catch (InterruptedException e) {
  98. return;
  99. }
  100. }
  101. _last_tick = System.currentTimeMillis();
  102. }
  103. public void setGrid(Grid g) {
  104. _grid = g;
  105. }
  106. public void draw(int x, int y, int color) {
  107. _pixels[x + y * width] = color;
  108. }
  109. @Override
  110. public void mouseDragged(MouseEvent e) {
  111. _grid.getPatch(e.getX() / scale, e.getY() / scale).setState(1);
  112. draw(e.getX() / scale, e.getY() / scale, 0xffffff);
  113. e.consume();
  114. }
  115. @Override
  116. public void mouseClicked(MouseEvent e) {
  117. }
  118. @Override
  119. public void mouseEntered(MouseEvent e) {
  120. // TODO Auto-generated method stub
  121. }
  122. @Override
  123. public void mouseExited(MouseEvent e) {
  124. // TODO Auto-generated method stub
  125. }
  126. @Override
  127. public void mousePressed(MouseEvent e) {
  128. // TODO Auto-generated method stub
  129. }
  130. @Override
  131. public void mouseReleased(MouseEvent e) {
  132. // TODO Auto-generated method stub
  133. }
  134. @Override
  135. public void mouseMoved(MouseEvent e) {
  136. // TODO Auto-generated method stub
  137. }
  138. }