Java Animation programs running jerky in Linux [closed]

I have written a simple Java animation program in Ubuntu 14.4.1. A ball moving inside a JPanel. But at execution, the ball moves quite jerky in the JPanel. This problem continues until I move the mouse inside the JPanel. At the time of moving the mouse inside the JPanel the ball movement is quite smooth. It should be said that I've run this program in Windows 10, and no problem occurred. The code for my program is as follows:

import java.awt.*;
import javax.swing.*;
public class MovingBall 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 class Ball { int x; int y; int xSpeed = 100; int ySpeed = 70; void go() { x = x + (xSpeed*25)/1000; y = y + (ySpeed*25)/1000; if( x < 0 ) { x = 0; xSpeed = -xSpeed; } else if( x > 490 ) { x = 490; xSpeed = -xSpeed; } else if( y < 0 ) { y = 0; ySpeed = -ySpeed; } else if( y > 490 ) { y = 490; ySpeed = -ySpeed; } // end if-else block } // end method go() void draw( Graphics g ) { g.fillOval( x , y , 10 , 10 ); } // end method draw } // end inner class Ball public static void main( String[] args ) { JFrame window = new JFrame(); window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); MovingBall animation = new MovingBall(); 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 MovingBall

What is the problem? Do I have to change some settings in mu Ubuntu?
I've 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 variable counter initial value of 0 declared in class MovingBall. I observed that the number of paintComponent's calls per second is much more than the actual refresh rate of the JPanel as it appears.

1 Reset to default

You Might Also Like