I’m trying to familiarize myself with making GUIs in java and wrote a program that should move a square on the screen depending on WASD key inputs (Following an online tutorial).
However, when running it, it seems that the square flickers and “lags”, sometimes rendering smoothly but most of the time not.
Here is my JPanel class, which is where the main stuff happens :
package me.analyzers.scs.game;
import me.analyzers.scs.utilities.KeyPressHandler;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainPanel extends JPanel implements Runnable{
final int FPS = 240;
final int tileSize = 16;
final int scaling = 3;
final int realTileSize = tileSize * scaling;
final int widthX = 16;
final int widthY = 12;
final int screenWidth = realTileSize * widthX;
final int screenHeight = realTileSize * widthY;
Thread mainLoop;
KeyPressHandler keyPressHandler = new KeyPressHandler();
public MainPanel() {
super();
this.setPreferredSize(new Dimension(screenWidth, screenHeight));
this.setBackground(Color.WHITE);
this.addKeyListener(keyPressHandler);
this.setFocusable(true);
}
public void startGameLoop() {
mainLoop = new Thread(this);
mainLoop.start();
}
public boolean isRunning() {
return mainLoop != null;
}
int posX = 0;
int posY = 0;
@Override
public void run() {
long lastTime = 0;
while(isRunning()) {
long currentTime = System.currentTimeMillis();
if(currentTime-lastTime > 1000/FPS) {
lastTime = System.currentTimeMillis();
tick();
repaint();
}
}
}
public void tick() {
switch (keyPressHandler.getLastKey()) {
case 'w' -> posY-=1;
case 's' -> posY+=1;
case 'a' -> posX-=1;
case 'd' -> posX+=1;
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
g2d.fillRect(posX, posY, realTileSize, realTileSize);
g2d.dispose();
}
}
At first I thought this could be caused by concurrency issues, and so I switched up the “main loop” code a lot; I tried conditionally executing it by checking currentTime() with lastTime, I tried doing Thread.sleep, and also tried using swing’s Timer class. They all yield the same result.
An interesting thing to note is that if I call repaint() inside the paintComponent() method (causing a loop, I think), it will work fine; but this calls paintComponent() hundreds of times per game loop.
After searching on the net for similar problems and not being able to fix it, I tried running the same jar archive in a Windows virtual machine. Surprisingly, it renders every smoothly! It also appears that the square moves way slower on the Windows machine than on mine. This confuses me a lot because I thought the JVM was supposed to be consistent across all platforms.
Analyzers is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.