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:
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!!