Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Message Boxes wont' stop appearing

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.

like image 764
Connor Avatar asked Aug 21 '12 01:08

Connor


1 Answers

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'.

like image 75
Andrew Thompson Avatar answered Nov 16 '22 13:11

Andrew Thompson