Boog33

1

Dec 7th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. package com.thecherno.rain;
  2.  
  3. import java.awt.Canvas;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.image.BufferStrategy;
  7. import java.awt.image.BufferedImage;
  8. import java.awt.image.DataBufferInt;
  9.  
  10. import javax.swing.JFrame;
  11.  
  12. import com.thecherno.rain.graphics.Screen;
  13.  
  14. public class Game extends Canvas implements Runnable {
  15. private static final long serialVersionUID = 1L;
  16.  
  17. public static int width = 300;
  18. public static int height = width / 16 * 9;
  19. public static int scale = 3;
  20.  
  21. private Thread thread;
  22. private JFrame frame;
  23. private boolean running = false;
  24.  
  25. private Screen screen;
  26.  
  27. private BufferedImage image = new BufferedImage(width, height,
  28. BufferedImage.TYPE_INT_RGB);
  29. private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer())
  30. .getData();
  31.  
  32. public Game() {
  33. Dimension size = new Dimension(width * scale, height * scale);
  34. setPreferredSize(size);
  35. screen = new Screen(width, height);
  36. frame = new JFrame();
  37.  
  38. }
  39.  
  40. public synchronized void start() {
  41. running = true;
  42. thread = new Thread(this, "Display");
  43. thread.start();
  44. }
  45.  
  46. public synchronized void stop() {
  47. running = false;
  48. try {
  49. thread.join();
  50. } catch (InterruptedException e) {
  51. e.printStackTrace();
  52. }
  53. }
  54.  
  55. public void run() {
  56. long lastTime = System.nanoTime();
  57. final double ns = 1000000000.0 / 60.0;
  58. double delta = 0;
  59. while (running) {
  60. long now = System.nanoTime();
  61. delta += (now-lastTime) / ns;
  62. lastTime = now;
  63. while(delta >= 1) {
  64. update();
  65. delta--;
  66. }
  67. render();
  68. }
  69. stop();
  70. }
  71.  
  72. public void update() {
  73.  
  74. }
  75.  
  76. public void render() {
  77. BufferStrategy bs = getBufferStrategy();
  78. if (bs == null) {
  79. createBufferStrategy(3);
  80. return;
  81. }
  82.  
  83. screen.clear();
  84.  
  85. screen.render();
  86.  
  87. for (int i = 0; i < pixels.length; i++) {
  88. pixels[i] = screen.pixels[i];
  89. }
  90.  
  91. Graphics g = bs.getDrawGraphics();
  92. //g.setColor(new Color(102, 153, 204));
  93. //g.fillRect(0, 0, getWidth(), getHeight());
  94. g.drawImage(image, 0, 0, getWidth(), getHeight(),null);
  95. g.dispose();
  96. bs.show();
  97. }
  98.  
  99. public static void main(String[] args) {
  100. Game game = new Game();
  101. game.frame.setResizable(false);
  102. game.frame.setTitle("Amish Cheese Voyage");
  103. game.frame.add(game);
  104. game.frame.pack();
  105. game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  106. game.frame.setLocationRelativeTo(null);
  107. game.frame.setVisible(true);
  108.  
  109. game.start();
  110. }
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment