I a working on a program for a simple game and we are told to add something like a start menu. I have the game working by itself to where I click run, and the game pulls up in a window, but now I need to add the start menu which I am having quite a hard time doing and don't know how to get it working properly. The game I am doing is Asteroids.
The way I have it now is that the game appears on a JFrame right from the start. Since the game appears from the start, I tried making a JButton
that when clicked initializes the value of a boolean variable that determines the status of the game equal to true. Here is the code I got:
public Asteroids()
{
ast = new ArrayList<Asteroid>();
aliveAsteroids = NUM_ASTEROIDS;
bullets = new Bullet[NUM_BULLETS];
ship = new Ship();
g2d = null;
gameButton = new JButton("Start");
gameButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
gameRunning = true;
}
});
JPanel buttonPanel = new JPanel (new FlowLayout());
buttonPanel.add(gameButton);
frame = new JFrame("Asteroids");
identity = new AffineTransform();
rand = new Random();
frame.setSize(FRAMEWIDTH, FRAMEHEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(buttonPanel);
frame.add(this);
frame.setVisible(true);
}
public static void main(String [] args)
{
Asteroids game = new Asteroids();
if(gameRunning == true)
{
game.go();
}
}
public void go()
{
//set up the ship
ship.setX(FRAMEWIDTH / 2);
ship.setY(FRAMEHEIGHT / 2);
//set up the bullets
for (int n = 0; n < NUM_BULLETS; n++)
{
bullets[n] = new Bullet();
}
//set up the asteroids
for (int n = 0; n < NUM_ASTEROIDS; n++)
{
Asteroid a = new Asteroid();
a.setX((double)rand.nextInt(FRAMEWIDTH)+20);
a.setY((double)rand.nextInt(FRAMEHEIGHT)+20);
a.setMoveAngle(rand.nextInt(360));
double ang = a.getMoveAngle() - 90;
a.setVelX(calcAngleMoveX(ang));
a.setVelY(calcAngleMoveY(ang));
ast.add(a);
}
addKeyListener(this);
requestFocusInWindow();
gameloop = new Thread(this);
gameloop.start();
}
For some reason this is not working the way I want it to and I don't understand why. Some things I try give me a NullPointerException
. For the record, I know what a NullPointerException
is and what it means for something to be null
, but I am just not sure why I am getting one in this instance. Other things I try pull up the frame but with nothing on it even and throws a bunch of exceptions even though it should have a button.
EDIT: The exceptions that appear are Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at game.Asteroids.drawBullets(Asteroids.java:176)
at game.Asteroids.paintComponent(Asteroids.java:148)
Another question I have is about the line that says frame.add(this)
. What exactly is this
? I know this
is a keyword that can be used to initialize instance variables that have the same name as a parameter, for instance this.width = width;
. But when I see this
by itself in this context, I have no clue what it is referring to. So that whole line right there, I have no idea what it does. All I know is that it adds something to the frame. What that something is is beyond me.
I was wondering if anyone could help explain to me what is going on and why my program is not working correctly and maybe offer some advice/tips on how to get on the right track? I would greatly appreciate any and all help.