Hi!
After reading trough the documentation for the MIDP game library in J2ME I decided to make a clone of that library that could be used with J2SE (i.e. for game programming on the PC).
I’ve created the basic GameCanvas class (see below) but there seems to be a problem with the call to createBufferStrategy(2) in the constructor. If you compile and run the code you’ll get a IllegalStateException saying “Component must have a valid peer”. If I try to fix this by calling addNotify() which is supposed to add a peer I get a NullPointerException saying “peer” instead.
/*
* Created on Apr 26, 2003
*/
package se.bostream.gbg.game;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
/**
* The GameCanvas class provides the basis for a game user interface.
*
* @author Johan Tibell
*/
public class GameCanvas extends Canvas
{
/** The bit representing the DOWN key. */
public static final int DOWN_PRESSED = KeyEvent.VK_DOWN;
/** The bit representing the LEFT key. */
public static final int LEFT_PRESSED = KeyEvent.VK_LEFT;
/** The bit representing the RIGHT key. */
public static final int RIGHT_PRESSED = KeyEvent.VK_RIGHT;
/** The bit representing the UP key. */
public static final int UP_PRESSED = KeyEvent.VK_UP;
/** The bit representing the SPACE key. */
public static final int SPACE_PRESSED = KeyEvent.VK_SPACE;
/** The bit representing the ESCAPE key. */
public static final int ESCAPE_PRESSED = KeyEvent.VK_ESCAPE;
/** The bit representing the ENTER key. */
public static final int ENTER_PRESSED = KeyEvent.VK_ENTER;
private BufferStrategy strategy;
private int keyStates = 0;
/**
* Creates a new instance of a GameCanvas.
*
* @param suppressKeyEvents true to suppress the regular key event mechanism for game keys, otherwise false
*/
public GameCanvas(boolean suppressKeyEvents)
{
super();
if (!suppressKeyEvents)
addKeyListener(new KeyStateListener(this));
// TODO: Fix NullPointerException bug.
//addNotify();
createBufferStrategy(2);
strategy = getBufferStrategy();
}
/**
* Flushes the off-screen buffer to the display.
*/
public void flushGraphics()
{
// If the buffer is intact draw it on the screen.
if (!strategy.contentsLost())
strategy.show();
}
/**
* Flushes the specified region of the off-screen buffer to the display.
*
* @param x the left edge of the region to be flushed
* @param y the top edge of the region to be flushed
* @param width the width of the region to be flushed
* @param height the height of the region to be flushed
*/
public void flushGraphics(int x, int y, int width, int height)
{
throw new UnsupportedOperationException();
}
/**
* Gets the states of the physical game keys.
*
* @return An integer containing the key state information (one bit per key), or 0 if the GameCanvas is not currently shown
*/
public int getKeyStates()
{
int currentKeyStates;
if (this.isVisible())
currentKeyStates = keyStates;
else
currentKeyStates = 0;
keyStates = 0;
return currentKeyStates;
}
/**
* Obtains the Graphics object for rendering a GameCanvas.
*
* @return the Graphics object that renders to this GameCanvas' off-screen buffer
*/
public Graphics getGraphics()
{
return strategy.getDrawGraphics();
}
/**
* Paints this GameCanvas.
*
* @param g the Graphics object with which to render the screen
*/
public void paint(Graphics g)
{
// TODO: Perhaps implement in a different way.
super.paint(g);
//flushGraphics();
}
private class KeyStateListener implements KeyListener
{
private GameCanvas receiver;
public KeyStateListener(GameCanvas receiver)
{
this.receiver = receiver;
}
public void keyTyped(KeyEvent e)
{
receiver.keyStates |= e.getKeyCode();
}
public void keyPressed(KeyEvent e)
{
receiver.keyStates |= e.getKeyCode();
}
public void keyReleased(KeyEvent e)
{
receiver.keyStates |= e.getKeyCode();
}
}
}