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
How to Solve the 'Lock Obtain Timed Out' When Using Solr Plainly
"Java.Sql.Sqlexception: I/O Error: Connection Reset" in Linux Server
Seeing Too Many Lsof Can't Identify Protocol
What Determines the Current Working Directory of Tomcat Java Process
Itext Get Field Coordinates from Existing PDF
Exception in Thread "Main" Java.Lang.Noclassdeffounderror: Org/Apache/Hadoop/Util/Platformname
Object Header Size in Java on 64Bit Vm with <4Gb Ram
Reading Remote Hdfs File with Java
Which Operating Systems Support Native (Inotify-Like) File Watching in Java
Why Does Killing Jvm Also Terminates Its Child Process If Waitfor Has Been Used
Java's 'Tnameserv' Takes 3+ Minutes to Be "Ready", Why
How to Set Classpath in Linux to Let Java Find Jar File
Webdriver for Firefox: Browser Starts W/ Empty Page, Hangs for 2 Min, Restarts, Then Test Runs. Why