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 ) {} }
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.
source share