Last active 1444543086

TheCherno Rain

Game.java Raw
1package com.thecherno.rain;
2
3import java.awt.Canvas;
4import java.awt.Color;
5import java.awt.Dimension;
6import java.awt.Graphics;
7import java.awt.image.BufferStrategy;
8import java.awt.image.BufferedImage;
9import java.awt.image.DataBufferInt;
10
11import javax.swing.JFrame;
12
13public class Game extends Canvas implements Runnable {
14 private static final long serialVersionUID = 1L;
15
16 public static int width = 300;
17 public static int height = width / 16 * 9;
18 public static int scale = 1;
19
20 private Thread thread;
21 private JFrame frame;
22 private boolean running = false;
23
24 private BufferedImage image = new BufferedImage(width, height,
25 BufferedImage.TYPE_INT_RGB);
26 private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
27
28 public Game() {
29 Dimension size = new Dimension(width * scale, height * scale);
30 setPreferredSize(size);
31
32 frame = new JFrame();
33 }
34
35 public synchronized void start() {
36 running = true;
37 thread = new Thread(this, "Display");
38 thread.start();
39 }
40
41 public synchronized void stop() {
42 running = false;
43 try {
44 thread.join();
45 } catch (InterruptedException e) {
46 e.printStackTrace();
47 }
48 }
49
50 public void run() {
51 while (running) {
52 update();
53 render();
54 }
55 }
56
57 public void render() {
58 BufferStrategy bs = getBufferStrategy();
59 if (bs == null) {
60 createBufferStrategy(3);
61 return;
62 }
63
64 Graphics g = bs.getDrawGraphics();
65 {
66 g.setColor(Color.red);
67 g.fillRect(0, 0, getWidth(), getHeight());
68 }
69
70 g.dispose();
71 bs.show();
72 }
73
74 public void update() {
75
76 }
77
78 public static void main(String[] args) {
79 Game game = new Game();
80 game.frame.setResizable(false);
81 game.frame.setTitle("Rain");
82 game.frame.add(game);
83 game.frame.pack();
84 game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
85 game.frame.setLocationRelativeTo(null);
86 game.frame.setVisible(true);
87
88 game.start();
89 }
90
91}
92