Sign in to follow this  

[java] Choppy Animation

This topic is 2792 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(){
		super();
		
		//set up the keyboard for input
		this.setFocusable(true);
		this.requestFocus();
		this.addKeyListener(keyboard);
	}
	

	public void run() {
		
		//create a buffer screen for double buffering
		screen = createImage(800, 600);
		buffer = screen.getGraphics();
		
		 
		while(true){
			
			//get input and update position
			update();
			
			//render to the buffer
			render();
			
			//paint the buffer to the screen
			paintToScreen();
			
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				
			}
			
		}
		
	}
	
	
	private void update(){
		
                //get the keys being pressed
		keyboard.poll();
		processInput();
	}
	
	private void render(){
		
		buffer.clearRect(0, 0, 800, 600);
		buffer.setColor(Color.DARK_GRAY);
		buffer.fillRect(0, 0, 800, 600);
		buffer.setColor(Color.black);
		buffer.fillOval(x, 100, 20, 20);
	}
	
	private void paintToScreen(){
		
		Graphics g = this.getGraphics();
		
		g.drawImage(screen, 0, 0, null);
		
		Toolkit.getDefaultToolkit().sync();
		
		g.dispose();
	}
	
	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

This topic is 2792 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this