Search code examples
javaswingconcurrency

Java Swing rendering inconsistencies and flickering


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.

EDIT : I've figured out that constant-inputs (this includes spam-clicking on the window) temporarily halt the weird stuttering. This makes me wonder if it's not caused by some process trying to "save CPU time" on my GUI? Does this exist ?

EDIT 2 : Toolkit.sync() successfully solved this issue. I believe it's because of "graphic event buffering" that my window system does (which explains the constant-input halting the stuttering).


Solution

  • The main reason for this is that you need to call getToolkit().sync() after every repaint.

    However, it’s not your only issue. You must not dispose of a Graphics object unless you yourself created it. The Graphics passed to paintComponent does not belong to you. Swing created it, and only Swing may dispose of it. Interfering with that may cause painting issues.

    Another problem is your use of a Thread. All Swing methods must run in the single, dedicated thread for AWT and Swing operations. If you call those methods from another thread, they may work sometimes and fail at other times. Or they may only work on certain computers. If you want to be sure your program works every time, on every computer, avoid calling Swing methods from a different Thread. (All of this is documented at https://docs.oracle.com/javase/tutorial/uiswing/concurrency/ .)

    The correct way to run Swing code at regular intervals is with javax.swing.Timer. (This is not the same class as java.util.Timer!) For example:

    Timer timer = new Timer(1000 / FPS,
        e -> {
            tick();
            repaint();
            getToolkit().sync();
        });
    timer.start();