Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. import java.awt.Canvas;
  2. import java.awt.Graphics;
  3. import java.awt.image.BufferStrategy;
  4. import java.awt.image.BufferedImage;
  5. import java.awt.image.DataBufferInt;
  6.  
  7. import javax.swing.JFrame;
  8.  
  9. public class Display extends Canvas implements Runnable {
  10.  
  11.  
  12. public static int width = 800;
  13. public static int height = 600;
  14. public static final String TITLE = "Rid alpha 0.1";
  15.  
  16.  
  17.  
  18. private Thread thread;
  19. private Screen screen;
  20. private BufferedImage img;
  21. private Render render;
  22. private boolean running = false;
  23. private int[] pixels;
  24.  
  25. public Display(){
  26. screen = new Screen(WIDTH, HEIGHT);
  27. img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
  28. pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
  29. }
  30.  
  31.  
  32. public void start(){
  33.  
  34. if(running)
  35. return;
  36.  
  37. running = true;
  38. thread = new Thread(this);
  39. thread.start();
  40.  
  41.  
  42. }
  43.  
  44.  
  45. private void stop(){
  46.  
  47. if(!running)
  48. return;
  49.  
  50. running = false;
  51. try
  52. {
  53. thread.join();
  54. } catch(Exception e) {
  55. e.printStackTrace();
  56. System.exit(0);
  57. }
  58. }
  59.  
  60.  
  61.  
  62. public void run(){
  63.  
  64. while(running)
  65. {
  66. tick();
  67. render();
  68. }
  69.  
  70. }
  71.  
  72.  
  73. private void tick(){
  74.  
  75. }
  76.  
  77.  
  78. private void render(){
  79.  
  80. BufferStrategy bs = this.getBufferStrategy();
  81. if(bs == null)
  82. {
  83. createBufferStrategy(3);
  84. return;
  85. }
  86.  
  87. screen.render();
  88.  
  89.  
  90. for(int i=0; i < WIDTH * HEIGHT; i++)
  91. {
  92. pixels[i] = screen.pixels[i];
  93. }
  94.  
  95.  
  96.  
  97. Graphics g = bs.getDrawGraphics();
  98.  
  99. g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
  100. g.dispose();
  101. bs.show();
  102.  
  103.  
  104.  
  105.  
  106.  
  107. }
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117. public static void main (String[] args){
  118.  
  119. Display game = new Display();
  120. JFrame frame = new JFrame();
  121. frame.add(game);
  122. frame.setTitle(TITLE);
  123. frame.setResizable(false);
  124. frame.setSize(width, height);
  125. frame.setVisible(true);
  126. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  127. frame.setLocationRelativeTo(null);
  128.  
  129. System.out.println("Running...");
  130. game.start();
  131. }
  132.  
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement