Steven Smith revised this gist . Go to revision
1 file changed, 91 insertions
Game.java(file created)
| @@ -0,0 +1,91 @@ | |||
| 1 | + | package com.thecherno.rain; | |
| 2 | + | ||
| 3 | + | import java.awt.Canvas; | |
| 4 | + | import java.awt.Color; | |
| 5 | + | import java.awt.Dimension; | |
| 6 | + | import java.awt.Graphics; | |
| 7 | + | import java.awt.image.BufferStrategy; | |
| 8 | + | import java.awt.image.BufferedImage; | |
| 9 | + | import java.awt.image.DataBufferInt; | |
| 10 | + | ||
| 11 | + | import javax.swing.JFrame; | |
| 12 | + | ||
| 13 | + | public 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 | + | } | |
Newer
Older