Thanks for asking…here’s an exerpt:
public abstract class Game extends Canvas {
/** The stragey that allows us to use accelerate page flipping */
private BufferStrategy strategy;
public abstract void redraw( Graphics g, long delta );
public void init()
{
// create a frame to contain our game
container = new JFrame(“Space Invaders 101”);
// get hold the content of the frame and set up the resolution of the game
JPanel panel = (JPanel) container.getContentPane();
panel.setPreferredSize(new Dimension(WIDTH,HEIGHT));
panel.setLayout(null);
container.setUndecorated( true );
setBackground(Color.black);
setForeground(Color.white);
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice();
gd.setFullScreenWindow(container);
GraphicsConfiguration gc = gd.getDefaultConfiguration();
BufferCapabilities bufCap = gc.getBufferCapabilities();
boolean page = bufCap.isPageFlipping();
if (page) {
// Page flipping is supported
} else {
// Page flipping is not supported
System.out.println("Page flipping is not supported" );
}
// setup our canvas size and put it into the content of the frame
setBounds(0,0,WIDTH,HEIGHT);
panel.add(this);
// Tell AWT not to bother repainting our canvas since we're
// going to do that our self in accelerated mode
setIgnoreRepaint(true);
// finally make the window visible
container.pack();
container.setResizable(false);
container.setVisible(true);
// add a listener to respond to the user closing the window. If they
// do we'd like to exit the game
container.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// request the focus so key events come to us
requestFocus();
// create the buffering strategy which will allow AWT
// to manage our accelerated graphics
createBufferStrategy(2);
strategy = getBufferStrategy();
}
public void gameLoop() {
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
BufferCapabilities bufCap = gc.getBufferCapabilities();
bufCap = strategy.getCapabilities();
BufferCapabilities.FlipContents flipContents = bufCap.getFlipContents();
long lastLoopTime = System.currentTimeMillis();
long t0 = System.currentTimeMillis();
// keep looping round til the game ends
while (gameRunning) {
long delta = System.currentTimeMillis() - lastLoopTime;
lastLoopTime = System.currentTimeMillis();
// Get hold of a graphics context for the accelerated
// surface and blank it out
Graphics/*2D*/ g = /*(Graphics2D)*/ strategy.getDrawGraphics();
try {
// System.out.println( delta );
//
redraw( (Graphics) g, delta );
// finally, we've completed drawing so clear up the graphics
// and flip the buffer over
} catch ( Exception e )
{
e.printStackTrace();
}
strategy.show();
g.dispose();
// finally pause for a bit. Note: this should run us at about
// 100 fps but on windows this might vary each loop due to
// a bad implementation of timer
try { Thread.sleep(10); } catch (Exception e) {}
}
}
}
class MyCanvas extends Game
implements ImageObserver
{
public void redraw( Graphics g, long delta )
{
// some application specific drawing using
// Graphics g.
// eg. g.clearRect();
// g.drawImage();
// g.drawString();
}
public static void main( String[] args )
{
MyCanvas canvas = new MyCanvas();
canvas.init();
canvas.gameLoop();
}
}