Hi,
I was trying to follow Killer Game Programming in Java but I failed to do so (Too tough for me for now - I need to get a good understanding about game loops and all those delta and other variables first). Then I tried to follow Black Art of Java Game Programming. Looks easier.
It does all stuff on an applet and I hate applets. So I tried to convert it to a JFrame application.
I figured init() is like a constructor, start() would be my main method, stop() would be performed by JFrame and run() is a Thread method (no need to convert). So, I converted it into a JFrame app.
It worked, but with a little problem:
[quote]Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException
at Broadway.paint(MyFrame.java:42)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$700(Unknown Source)
… Goes on
[/quote]
So I figured I forgot to initiate something. The 42nd line is this:
offScreen.setColor(Color.BLACK); //offScreen is the Graphics object I use for double buffering
But I actually initiated it in the constructor. Here is my full code which can compile and show the error:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JFrame;
public class MyFrame extends JFrame implements Runnable {
Graphics offScreen; // That's how the book tells me to implement it
Image buffer;
int a = 5;
int b = 3;
int locx, locy, width, height;
int aWidth = 400;
int aHeight = 400;
static final int REFRESH_RATE = 50; // Sleep duration. The lower this is,the faster the rectangle.
public MyFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBackground(Color.black);
setSize(aWidth, aHeight);
locx = 80;
locy = 100;
width = 110;
height = 90;
setVisible(true);
buffer = createImage(aWidth, aHeight); // Creating an image in the same size as JFrame
offScreen = buffer.getGraphics(); // It is initialized man...
}
public static void main(String args[]) {
MyFrame b = new MyFrame();
Thread animation = new Thread(b);
if (animation != null) {
animation.start();
}
}
public void paint(Graphics g) {
offScreen.setColor(Color.BLACK);
offScreen.fillRect(0, 0, aWidth, aHeight);
offScreen.setColor(Color.blue);
offScreen.fillRect(80, 200, 220, 90);
offScreen.fillRect(100, 10, 90, 80);
offScreen.setColor(Color.lightGray);
offScreen.fillRect(locx, locy, width, height);
g.drawImage(buffer, 0, 0, this); // Why is this "g" instead of "offScreen"? Figure.
}
public void update(Graphics g) { // Book tells this is needed. Figure why.
paint(g); // Also look into 'active rendering' thing. This looks nasty.
}
public void updateRectangle() {
locx += a;
locy += b;
if (locx >= 290 || locx <= 0) {
a = -a;
}
if (locy >= 310 || locy <= 0) {
b = -b;
}
}
public void run() {
while (true) {
repaint(); // It's said that calling repaint() is not a guaranteed way.
updateRectangle();
try {
Thread.sleep(REFRESH_RATE);
} catch (Exception exc) {
}
}
}
}