• Advertisement
Sign in to follow this  

[java] Choppy Animation

This topic is 2888 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to do a very simple animation test, moving an oval around a screen. However, the animation is very choppy and the oval seems to sputter when I hold down either left or right. The code that follows is a JPanel that runs the simple animation movement. Perhaps it is the way I handle the main game loop? Any help would be greatly appreciated.

	public class Canvas extends JPanel implements Runnable{

	private static Image screen;
	private static Graphics buffer;
	private static KeyboardInput keyboard = new KeyboardInput();
	private int x = 0;
	public Canvas(){
		//set up the keyboard for input

	public void run() {
		//create a buffer screen for double buffering
		screen = createImage(800, 600);
		buffer = screen.getGraphics();
			//get input and update position
			//render to the buffer
			//paint the buffer to the screen
			try {
			} catch (InterruptedException e) {
	private void update(){
                //get the keys being pressed
	private void render(){
		buffer.clearRect(0, 0, 800, 600);
		buffer.fillRect(0, 0, 800, 600);
		buffer.fillOval(x, 100, 20, 20);
	private void paintToScreen(){
		Graphics g = this.getGraphics();
		g.drawImage(screen, 0, 0, null);
	private void processInput() {
		    if( keyboard.keyDown( KeyEvent.VK_RIGHT ) ) {
		    	x += 5;
		    else if( keyboard.keyDown( KeyEvent.VK_LEFT ) ) {
		    	x -= 5;

Share this post

Link to post
Share on other sites
Perhaps the problem has to do with the way you are moving the rect...

x += 5;

I haven't tried your code out, but if your framerate is changing, then the speed at which you move is changing. Do you see what I mean. Perhaps you should try moving by some value, such as 0.01 * time_since_last_frame. That way no matter how many clock cycles have passed, the object will move at the same speed.

Also, check this out if you haven't already...

Java Games: Active Rendering

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement