Java animation programs running on Linux

I wrote a simple Java animation program in Ubuntu 14.4.1. The ball moves inside the JPanel. But at execution, the ball steps into JPanel rather jerkily. This problem continues until I move the mouse inside the JPanel. While moving the mouse inside the JPanel, the movement of the ball is pretty smooth. I must say that I launched this program in Windows 10, and there were no problems. My program code is as follows:

import java.awt.*; import javax.swing.*; public class BouncingBall extends JPanel { Ball ball = new Ball(); void startAnimation() { while( true ) { try { Thread.sleep( 25 ); ball.go(); repaint(); } catch( InterruptedException e ) {} } // end while( true ) } // end method startAnimation() protected void paintComponent( Graphics g ) { super.paintComponent( g ); ball.draw( g ); } // end method paintComponent // inner class Ball class Ball { int x; int y; int diameter = 10; int xSpeed = 100; int ySpeed = 70; void go() { x = x + (xSpeed*25)/1000; y = y + (ySpeed*25)/1000; int maxX = getWidth() - diameter; int maxY = getHeight() - diameter; if( x < 0 ) { // bounce at the left side x = 0; xSpeed = -xSpeed; } else if( x > maxX ) { // bounce at the right side x = maxX; xSpeed = -xSpeed; } else if( y < 0 ) { // bounce at the top side y = 0; ySpeed = -ySpeed; } else if( y > maxY ) { // bounce at the bottom size y = maxY; ySpeed = -ySpeed; } // end if-else block } // end method go() void draw( Graphics g ) { g.fillOval( x , y , diameter , diameter ); } // end method draw } // end inner class Ball public static void main( String[] args ) { JFrame window = new JFrame(); window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); BouncingBall animation = new BouncingBall(); animation.setPreferredSize( new Dimension( 500 , 500 ) ); animation.setBackground( Color.white ); window.add( animation ); window.pack(); window.setVisible( true ); animation.startAnimation(); } // end method main } // end class BouncingBall 

What is the problem? Do I need to change some settings in my Ubuntu? I also put some test code inside the paintComponent method as follows:

 protected void paintComponent( Graphics g ) { System.out.println( "paintComponent call number: " + counter ); ++counter; super.printComponent( g ); ball.draw( g ); } 

with the initial value of the counter variable 0 declared in the MovingBall class. I noticed that the number of paintComponent calls per second is much larger than the actual JPanel refresh rate as it appears.

+5
source share
1 answer

Video acceleration is enabled by default on Windows, but by default it is not enabled on Linux. (This has been true for many years, I could have sworn that this one was changed by default for recent releases of Java, but obviously I was wrong.)

You can enable OpenGL to speed things up:

 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 
+6
source

Source: https://habr.com/ru/post/1260893/


All Articles