1

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.

  • Does this class `Asteroids` extend anything (because that would make `frame.add(this)` very different)? – ricky3350 Dec 05 '15 at 02:54
  • Also look at [this answer](http://stackoverflow.com/a/3728101) for a good explanation of `this`. – ricky3350 Dec 05 '15 at 02:55
  • @ricky3350 - Yes, it extends `JComponent` – generic user007 Dec 05 '15 at 02:57
  • your `main` method will terminate immediately since you do not wait for the trigger of `actionPerformed`. – René Blanken Dec 05 '15 at 03:05
  • @RenéBlanken - I am not sure what you mean. Can you explain? – generic user007 Dec 05 '15 at 03:10
  • @RenéBlanken: the Swing event thread, which starts when the GUI is set to visible, has not shut down, and so there is no reason for the application to terminate. To the OP, your while loop has no business being in an event-driven application. Polling is not the way to go. Rather you want to use call-backs and notification. I'm curious why you don't show the seemingly important `go()` method. – Hovercraft Full Of Eels Dec 05 '15 at 03:12
  • Use a CardLayout which will allow you to switch views – MadProgrammer Dec 05 '15 at 03:15
  • Also you mention that your code throws a NullPointerException (NPE) when it runs, and you say that you understand the concepts of what this means, but you don't show the all-important stacktrace nor do you indicate which line throws the exception, two key bits of information that you should know that you need to post when debugging a NPE. Why not show us this? – Hovercraft Full Of Eels Dec 05 '15 at 03:15
  • @HovercraftFullOfEels I never said the application terminates (although I could have phrased that better). But the constructor gets called, the if-statement gets evaluated, and with that, the main method is done. The application is just waiting for the window to close. – René Blanken Dec 05 '15 at 03:17
  • @HovercraftFullOfEels - I added the `go()` method. The lines that threw the exceptions were 148 and 176 but there were others that were marked with `(Unknown Souce)` – generic user007 Dec 05 '15 at 04:10
  • @MadProgrammer - I think this is what I am looking for. I want to have a `card` that has the button on it and when I press the button it switches to another `card` that is running the game. Now I just need to figure out how to implement that – generic user007 Dec 05 '15 at 04:28

0 Answers0