Archived

This topic is now archived and is closed to further replies.

porkchop

[java] Choppy animation

Recommended Posts

I was wondering if java animation has to be choppy if you are not using bindings to some grapichs library? For some reason the following code gives a fairly smooth animation, but sometimes it stops and the animation gets choppy. This code is placed inside a class that inherits from JPanel and is invoked every 40 ms by a javax.swing.Timer. public void paintComponent( Graphics g ){ super.paintComponent( g ); Graphics2D gBuffer=(Graphics2D) g; gBuffer.drawLine( 0, 25, 392, 25 ); gBuffer.drawLine( 0, 250, 392, 250 ); gBuffer.drawString( "Score: "+score, 150, 20 ); gBuffer.fillRect( ball.getXpos1(), ball.getYpos1(), ball.size(), ball.size() ); gBuffer.fillRect( paddOne.getXpos1(), paddOne.getYpos1(), paddOne.getDepth(), paddOne.getWidth() ); gBuffer.fillRect( paddTwo.getXpos1(), paddTwo.getYpos1(), paddTwo.getDepth(), paddTwo.getWidth() ); } Are there any way to make this paint code faster or is my computer just not fast enough? By the way I am using a P3 800 MHz with 128 Mb RAM.

Share this post


Link to post
Share on other sites
Don''t invoke the animation code with the Timer. It can be prempted by other events in the event dispatch thread.

Create a custom thread to do all your main loop work like moving and painting. This way it will run as fast as your computer can.



First make it work,
then make it fast.

--Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte

Share this post


Link to post
Share on other sites