views:

61

answers:

3

I'm trying to paint a Welcome Screen for my game, but only when the game loads. I don't want it to repaint everytime during the game.

So I did this (where isStart is instantiated as true):

public myClass(String name){
    setSize(800, 800);
    setVisible(true);
    setResizable(false);
    runGame()
}

public void paint(Graphics g) {
    if(nowStarting)
        g.drawImage(WelcomeGameScreen, 0, 0, null);
    isStart = false;
}

The problem is that the image will pop up for a second and then disappear? Oddly, it works when I leave out the if statement/isStart condition. What's wrong with this?

A: 

I guess your newStarting boolean gets changed to false as soon as the panel is painted.

Riduidel
Yeah - but shouldn't it enter the if-statement one time and paint the background once? Then it would exit the paint() method, and not return since I don't call repaint() again. (Not yet, that is.)
Marky Mark
A: 

The reason it gets disappeared immediately is because of the repaints that are triggered by the Swing framework. Plus you have written the code for Welcome screen inside the overridden paint() method.

Refer this link for a detailed explanation of how to fire a splash window. You also have a SplashScreen class in Java 1.6

Bragboy
Is there another way? One that deals with JFrame and not JWindow?
Marky Mark
+1  A: 

I am guessing that you have not copied verbatim the code, and there is an error in your code above. If your code is what I think it is...

public void paint(Graphics g) {
    if(isStart)
        g.drawImage(WelcomeGameScreen, 0, 0, null);
    isStart = false;
}

Then at start it will draw your splash screen. But, because you are then setting isStart to false, the next time paint is called, the image will no longer be drawn. The paint method is called whenever the OS tells the screen that it needs to be refreshed (and when you force it with repaint).

The way you can get around this, is to set isStart to false in your application when the game has finished loading, and then call repaint.

Codemwnci