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 2.4 KiB

11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. private static int max_fps = 15;
  15. public String title = "Goldfish";
  16. private JFrame _frame;
  17. private BufferedImage _image;
  18. private Grid _grid;
  19. private int[] _pixels;
  20. public Render(int width, int height, Grid g) {
  21. Render.width = width;
  22. Render.height = height;
  23. setScale();
  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. this(width, height, new Grid(width, height));
  33. }
  34. public Render() {
  35. this(256, 256, new Grid(128, 128));
  36. }
  37. private void setScale() {
  38. if (height <= 128 || width <= 128) {
  39. Render.scale = 3;
  40. } else if (height <= 256 || width <= 256) {
  41. Render.scale = 2;
  42. }
  43. }
  44. private void setFrame() {
  45. _frame = new JFrame();
  46. _frame.setResizable(false);
  47. _frame.setTitle(title);
  48. _frame.add(this);
  49. _frame.pack();
  50. _frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  51. _frame.setLocationRelativeTo(null);
  52. _frame.setVisible(true);
  53. }
  54. private void clear() {
  55. for (int x = 0; x < _pixels.length; x++) {
  56. _pixels[x] = 0;
  57. }
  58. }
  59. private void update() {
  60. for (int i = 0; i < width; i++) {
  61. for (int j = 0; j < height; j++) {
  62. draw(i, j, _grid.getPatch(i, j).getState() * 0xffffff);
  63. }
  64. }
  65. }
  66. public void run() {
  67. BufferStrategy bs;
  68. Graphics g;
  69. bs = getBufferStrategy();
  70. if (bs == null) {
  71. createBufferStrategy(1);
  72. return;
  73. }
  74. clear();
  75. update();
  76. g = bs.getDrawGraphics();
  77. g.drawImage(_image, 0, 0, getWidth(), getHeight(), null);
  78. g.dispose();
  79. bs.show();
  80. }
  81. public void sleep() {
  82. }
  83. public void setGrid(Grid g) {
  84. _grid = g;
  85. }
  86. public void draw(int x, int y, int color) {
  87. _pixels[x + y * width] = color;
  88. }
  89. }