0

I'm a comp-sci student trying to learn the basics of game development. I've worked with GUI frameworks like .NET Winforms and etc before, so I'm not completely new to GUI development, but now I'm trying to get into Game Dev, and as I understand it, a huge central part to game dev is the Game Loop.

The way I so far understand it, the Game loop works something like so: (Pseudocode)

while (gameNotOver):
    modelData = getModelData;
    render(modelData);

And ideally this loop would execute and iterate over and over as fast as it could each frame (FPS).

Now I'm trying to do this in Java Swing BEFORE I advance into Unity or anything so I have some fundamentals down. Here's some of my code:

Application Class:

public static void Start(){
    GameLoop();
}

private static void GameLoop(){
    System.out.println("GameLoop()");
    System.out.println("---------------------------");
    JFrame frame = new JFrame();
    frame.setSize(300, 400);
    GUI gui = new GUI();
    frame.add(gui);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    int count = 0;
    while (count < 10){
        System.out.println("T-" + count + "-iteration");
        int modelInfo = Engine.update();
        gui.repaint();
        count++;
        System.out.println("---------------------------");
    }
}

GUI class:

public class GUI extends JPanel{

    public GUI(){

    }

    @Override
    public void paint(Graphics g){
        System.out.println("GUI-paint()");
    }
    }

The main method is in its own class, where it has one line: Application.Start();

It's not that the code doesn't work, but this is bugging me:

Output results on console

As you can see, paint() is only called at the very end, while everything else is falling perfectly in order in the gameloop.

Now I DO know that paint() gets called once when the JPanel is created, but first, wouldn't that mean the print statement in generates would be at the top?

Why is the game loop completing it's 10 iterations FIRST, before calling paint()?

Ideally I'm trying to call repaint IN the game loop each time (The render portion of the game loop)

Any help is greatly appreciated

Thanks!!

Cleo
  • 136
  • 1
  • 10
Nassim Assaf
  • 773
  • 5
  • 5
  • 1
    1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. 3) *"paint() is only called at the very end"* Don't block the EDT (Event Dispatch Thread). The GUI will 'freeze' when that happens. See [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for details and the fix. – Andrew Thompson Mar 31 '20 at 17:21

1 Answers1

0

The pseudo game loop looks more like this:

while (game not over)
    update model
    render graphics
    sleep

This loop cannot happen in the same code that constructs and executes the Swing components. The thread that constructs and executes the Swing components is called the Event Dispatch Thread.

The pseudo game loop has to happen in a separate thread or a Swing timer. When I say render graphics, what I mean is drawing on a drawing panel (JPanel) using the information stored in the game model.

When you create a drawing panel, you override the paintComponent method of the JPanel class. The first statement of your method must be a call to super.paintComponent to maintain the Swing paint chain.

The answers to this Stack Overflow question, "How can you organize the code for a game to fit the MVC pattern?", should be helpful to you.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111