[java] Choppy animation

Started by
0 comments, last by porkchop 20 years, 6 months ago
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.
Advertisement
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
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]

This topic is closed to new replies.

Advertisement