Game.java
· 1.8 KiB · Java
Raw
package com.thecherno.rain;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public static int width = 300;
public static int height = width / 16 * 9;
public static int scale = 1;
private Thread thread;
private JFrame frame;
private boolean running = false;
private BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
public Game() {
Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size);
frame = new JFrame();
}
public synchronized void start() {
running = true;
thread = new Thread(this, "Display");
thread.start();
}
public synchronized void stop() {
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run() {
while (running) {
update();
render();
}
}
public void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
{
g.setColor(Color.red);
g.fillRect(0, 0, getWidth(), getHeight());
}
g.dispose();
bs.show();
}
public void update() {
}
public static void main(String[] args) {
Game game = new Game();
game.frame.setResizable(false);
game.frame.setTitle("Rain");
game.frame.add(game);
game.frame.pack();
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setLocationRelativeTo(null);
game.frame.setVisible(true);
game.start();
}
}
| 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 | } |
| 92 |