Java Animation Programs Running Jerky in Linux

Java Animation programs running jerky in Linux

Video acceleration is enabled by default in Windows, but is not enabled by default in Linux. (This has been true for many years now; I could have sworn this default was changed for recent Java releases, but evidently I was wrong.)

You can enable OpenGL to get accelerated performance:

public static void main( String[] args ) {
System.setProperty("sun.java2d.opengl", "true");

JFrame window = new JFrame();

Alternatively, you can set the property on the command line:

java -Dsun.java2d.opengl=true BouncingBall

JProgressBar slow on Linux

Found the answer here

Looks like this is due to opengl being disabled by default on Linux. Can be fixed by adding the following line to main.

System.setProperty("sun.java2d.opengl", "true");

Smooth animation in Swing

I am not sure repainting at the monitor's refresh rate is a good idea. But if you are looking to find out what the refresh rate is you can use this piece of code (shamelessly copied from the interwebs).

GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();

for (int i = 0; i < gs.length; i++) {
DisplayMode dm = gs[i].getDisplayMode();

// Get refresh rate in Hz
int refreshRate = dm.getRefreshRate();
if (refreshRate == DisplayMode.REFRESH_RATE_UNKNOWN) {
// Unknown rate
} else {
System.out.println(refreshRate);
}
}

Why does my custom Swing component repaint faster when I move the mouse? (Java)

I guess you probably cannot solve your issue by enabling OpenGL, since your gpu does not support it, a possible silly workaround could be to fire a kind of event manually in each timer's iteration.

/* Update the scene every 40 milliseconds. */
final Robot robot = new Robot();
Timer timer = new Timer(40, (e) -> {
robot.mouseRelease(0); //some event
updateScene();
});
timer.start();

(And the only place you can Thread.sleep() in a Swing Application is inside a SwingWorker's doInBackground method. If you call it in EDT the whole GUI will freeze since events cannot take place.)

Why is java application running smoother when moving mouse over it? Video included

The repaints should be triggered from a Swing based Timer, which ensures that GUI updates are called on the EDT. See Concurrency in Swing for more details.



Related Topics



Leave a reply



Submit