title: Java Swing - GameLoop - Weird Results
body: <p>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.</p>
  
  <p>The way I so far understand it, the Game loop works something like so: (Pseudocode)</p>
  
  <pre><code>while (gameNotOver):
      modelData = getModelData;
      render(modelData);
  </code></pre>
  
  <p>And ideally this loop would execute and iterate over and over as fast as it could each frame (FPS).</p>
  
  <p>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:</p>
  
  <p><strong>Application Class:</strong></p>
  
  <pre><code>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 &lt; 10){
          System.out.println("T-" + count + "-iteration");
          int modelInfo = Engine.update();
          gui.repaint();
          count++;
          System.out.println("---------------------------");
      }
  }
  </code></pre>
  
  <p><strong>GUI class:</strong></p>
  
  <pre><code>public class GUI extends JPanel{
  
      public GUI(){
  
      }
  
      @Override
      public void paint(Graphics g){
          System.out.println("GUI-paint()");
      }
      }
  </code></pre>
  
  <p>The main method is in its own class, where it has one line: <code>Application.Start();</code></p>
  
  <p>It's not that the code doesn't work, but this is bugging me:</p>
  
  <p><a href="https://i.stack.imgur.com/Oe6Mv.png" rel="nofollow noreferrer">Output results on console</a></p>
  
  <p>As you can see, <code>paint()</code> is only called at the very end, while everything else is falling perfectly in order in the gameloop.</p>
  
  <p>Now I DO know that <code>paint()</code> gets called once when the <code>JPanel</code> is created, but first, wouldn't that mean the print statement in generates would be at the top? </p>
  
  <p>Why is the game loop completing it's 10 iterations FIRST, before calling <code>paint()</code>?</p>
  
  <p>Ideally I'm trying to call repaint IN the game loop each time  (The render portion of the game loop)</p>
  
  <p>Any help is greatly appreciated</p>
  
  <p>Thanks!!</p>