Message Boxes wont' stop appearing
Asked Answered
D

1

5

I'm honestly not sure if the title fits the question completely, but here it is anyway. I'm making a simple game in java where alien spaceships fall down from the top of the screen and if you don't kill them and they get to the bottom of the screen, the space station takes damage. But whenever the space station gets destroyed, the message-box that should tell the player that they died, won't stop appearing, it just keeps popping up over and over again. And in the console I get an error message that won't stop getting bigger! This is the code I have for the space station's health:

public class SpaceStation extends Entity {
public static int stationHealth = 100;

public SpaceStation(int x, int y) {
    super(x, y);
}

public void update() {
}

public void draw(Graphics2D g2d) {
    g2d.drawImage(ImageLocator.getSpaceStation(), 0, 595, null);

    // HEALTH BAR
    g2d.fillRect(5, 25, stationHealth, 10);

    if(stationHealth <= 0) {
        try{
            JOptionPane.showMessageDialog(null, "You died on level "
                    + GameFrame.level + ". Better luck next time!");
            GameFrame.mainTimer.stop();
            System.exit(0);
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e.getMessage());
            System.exit(0);
        } 
    }
}

public Rectangle getBounds() {
    return new Rectangle(x, y, 600, 200);
}

}

Apparently the line that the error is on is the JOptionPane.showMessageDialog(null, "You died on level " Here's the error message:

at java.lang.Thread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.InternalError: The current process has         used all of its system allowance of handles for Window Manager objects.

at sun.awt.windows.WToolkit.eventLoop(Native Method)
at sun.awt.windows.WToolkit.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.InternalError: The current process has used all of its system allowance of handles for Window Manager objects.

Thank you for your time.

Dak answered 21/8, 2012 at 1:0 Comment(5)
You've likely got occult recursion going on causing a stackoverflow error. Please post the error message, at least the first 20-30 lines of it. The error is also likely in code that you're not showing. What line is causing your exception to occur? Please be sure to post that line and related code as well. Otherwise you're kind of making us guess at what's going on, and that usually doesn't work too well.Mimetic
@Andrew: 5+ up-votes. Please make that into an answer so I can up-vote it for real and so the original poster can accept it. That's the problem and the solution. We only needed the OP to post enough information to allow us not to have to guess!Mimetic
Oh! Thank you, I don't get that error anymore, but it doesn't seem to have much effect, the stationHealth ends up getting into the negative numbers and the game continues. I put it into the update() method. Is that ok? Oh, and sorry about not posting enough. =\Dak
Then you need to check stationHealth in some other way, either in your game loop (possibly a Swing Timer) or by making stationHealth a bound property by using a PropertyChangeListener.Mimetic
I'd tend to check it in the actionListener of the Timer. ifGameOver() returns true, stop the timer and call assessWinner() which might pop the 'your character has 0 health' (much nicer than 'got you, sucker') dialog.Meganmeganthropus
M
9

Don't open a dialog from within the paint method! Or perhaps more domain specific, don't do that in a method that might be called from the paint method of another class, as the draw(Graphics2D) suggests.

I can only presume your code has a timer that calls repaint(), which in turn might cause a call to paint(Graphics) or paintComponent(Graphics). But that is also done by the JRE itself whenever it knows a component might have an element appear over the top, ..like a JOptionPane. Hence 'infinite loop'.

Meganmeganthropus answered 21/8, 2012 at 1:38 Comment(2)
Thanks, and 1+. OP, this is your problem and its solution.Mimetic
Thanks! I would +1 if I could.Thanks for the help everybody!Dak

© 2022 - 2024 — McMap. All rights reserved.