Skip to content

Instantly share code, notes, and snippets.

/Game.java Secret

Created August 7, 2016 14:23
Show Gist options
  • Save anonymous/51ab674e760b80b37cc3f313a1bc0173 to your computer and use it in GitHub Desktop.
Save anonymous/51ab674e760b80b37cc3f313a1bc0173 to your computer and use it in GitHub Desktop.
package com.myname.ra;
import com.myname.ra.graphics.Screen;
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 {
public static int width = 300;
public static int height = width / 16 * 9;
public static int scale = 3;
private Thread thread;
private JFrame frame;
private boolean running = false;
private Screen screen;
private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// Converts image into array of integers
private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
public Game() {
Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size);
screen = new Screen(width, height);
frame = new JFrame();
pixels = new int[width * height];
}
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();
}
}
// Displays images to the screen
public void render() {
BufferStrategy bs = getBufferStrategy();
// Checks if the buffer strategy exists. If it doesn't create a triple buffer strategy
if(bs == null) {
// Triple buffering
createBufferStrategy(3);
return;
}
screen.render();
// Copies array in Screen class to pixels array in (this) class
for (int i = 0; i < pixels.length; i++) {
pixels[i] = screen.pixels[i];
}
// Must be in chronological order
Graphics g = bs.getDrawGraphics();
g.setColor(Color.CYAN);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
g.dispose();
// Display next available buffer
bs.show();
}
// Updates the game. Restricted to run at 60 times per second
public void update() {
}
public static void main(String[] args) {
Game game = new Game();
// Prevents window resizing to prevent graphical errors
game.frame.setResizable(false);
game.frame.setTitle("Rain");
// Fill the window with the instance of the Game class
game.frame.add(game);
// Set the size of the frame so that all the contents are at the same size as the resolution that was set
game.frame.pack();
// Closes the program when the window is closed
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Center the window to the screen
game.frame.setLocationRelativeTo(null);
// Prevent game window from being hidden
game.frame.setVisible(true);
game.start();
}
} // end Game class
package com.myname.ra.graphics;
public class Screen {
private int width;
private int height;
public int[] pixels;
public Screen(int width, int height) {
this.width = width;
this.height = height;
// Size of the pixels array reserves one element for each pixel
pixels = new int[width * height];
}
public void render() {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
pixels[x + y * width] = 0xff00ff;
}
}
}
} // end Screen class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment