paintComponent() good standars' programming

Hi ,
This is my question , for example I would create a game , and would paint when I win a match a screen with “You win” , generally I, for do this, use a boolean variable in paintComponent method :


public void paintComponent(Graphics g){
      if(game_is_running) // paint the game state 
      else // paint the screen with the string "you have win "

With this approach all work , but I’ve read on web that into paintComponent must not use boolean variable because paintComponent must paint only the game state… So I don’t know how…

What is the problem ??? It works ;D

It’s true that your painting code should not include any “business logic”. The idea is to separate your game’s logic from painting the game, that way you can easily change it later- for example if you wanted to go from 2D to 3D.

That doesn’t mean you can’t have any if statements or for loops in your painting code.

The game state includes boolean variables like whether the game is over. The advice you received is bad advice anyways because it rules out settings that change the GUI. (showFps, showMiniMap, miniMapPosition, textColor, detailLevel, etc.)

[quote]The idea is to separate your game’s logic from painting the game,
[/quote]
Yes this is the logic’s advice… so thank at all … ;D

I too use conditional statements in my render method.


public void render()
{
    switch (gameState)
    {
        case GAME_INTRO:
            renderIntroScreen();
            break;
        case GAME_PAUSED:
            renderPauseScreen();
            break;
        case GAME_PLAYING:
            renderPlayingScreen();
            break;
        case GAME_OVER:
            renderGameOverScreen();
            break;
    }
}

It’s not bad to have conditional statements in render code. It’s only bad to do logic in render method.

Just to clarify, by logic you all mean state changes. Drawing methods should have no side effects. Running the game at 60 FPS, 1 FPS, 120 FPS, or 0 FPS should not change how the game runs. if(doSomething && doSomethingElse) in a paint method is not what you mean by logic.