Skip to content

Instantly share code, notes, and snippets.

@BonsaiDen
Created January 5, 2010 15:41
Show Gist options
  • Save BonsaiDen/269448 to your computer and use it in GitHub Desktop.
Save BonsaiDen/269448 to your computer and use it in GitHub Desktop.
// A simple Demo for creating a Java 2D Game Engine
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Transparency;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class Test extends Thread {
// Graphics Stuff
private boolean isRunning = true;
private JPanel canvasPanel;
private BufferedImage background;
private Graphics2D backgroundGraphics;
private JFrame frame;
private int width = 800;
private int height = 600;
private int scale = 1;
private int fps = 30;
private GraphicsConfiguration config =
GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice()
.getDefaultConfiguration();
// Create a hardware accelerated image
public final BufferedImage create(final int width, final int height,
final boolean alpha) {
return config.createCompatibleImage(width, height, alpha
? Transparency.TRANSLUCENT : Transparency.OPAQUE);
}
// Setup
public Test() {
// JFrame
frame = new JFrame();
frame.addWindowListener(new FrameClose());
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
frame.setSize(width * scale, height * scale);
frame.setVisible(true);
// Canvas
canvasPanel = new JPanel(false);
canvasPanel.setPreferredSize(new Dimension(width * scale, height * scale));
frame.add(canvasPanel, 0);
// Background & Buffer
background = create(width, height, false);
start();
}
private class FrameClose extends WindowAdapter {
@Override
public void windowClosing(final WindowEvent e) {
isRunning = false;
}
}
// MainLoop ----------------------------------------------------------------
public void run() {
// Using a Background Image instead of drawing directly to the buffer
// has three advantages: 1. You're able to scale the output and 2.
// it's actually faster and last but not least it eliminates the need of
// double buffering
backgroundGraphics = (Graphics2D) background.getGraphics();
long fpsWait = (long) (1.0 / fps * 1000);
while (isRunning) {
long renderStart = System.nanoTime();
updateGame();
// Update Graphics
Graphics2D bg = (Graphics2D) canvasPanel.getGraphics();
renderGame(backgroundGraphics);
if (scale != 1) {
bg.drawImage(background, 0, 0, width * scale, height
* scale, 0, 0, width, height, null);
} else {
bg.drawImage(background, 0, 0, null);
}
bg.dispose();
// FPS Limiting
long renderTime = (System.nanoTime() - renderStart) / 1000000;
try {
Thread.sleep(Math.max(0, fpsWait - renderTime));
} catch (InterruptedException e) {
Thread.interrupted();
break;
}
renderTime = (System.nanoTime() - renderStart) / 1000000;
}
frame.dispose();
}
// Game Methods ------------------------------------------------------------
public void updateGame() {
// update game logic here
}
private Random rand = new Random();
public void renderGame(Graphics2D g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, width, height);
g.setColor(Color.ORANGE);
g.fillRect(rand.nextInt(width), rand.nextInt(height), 20, 20);
}
public static void main(final String args[]) {
new Test();
}
}
@deltakid0
Copy link

Could you please indicate why do you say that using a BufferedImage is faster? (I think by that you mean "faster than direct buffer drawing", please correct me if I'm wrong). Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment