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?
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…
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.