For Programmers: Free Programming Magazines  


Home > Archive > Java Help > September 2004 > smooth animation









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author smooth animation
Mike Crenshaw

2004-09-28, 4:13 pm

Hi

I'm trying to figure out how to make an animation move smoothly.. so i've
been looking around.. i've found 3 other methods besides just using sleep()
at each frame.. i've tried implementing these methods but i cant see much of
a difference.. but which of the following, if any, is the best way to go
about it.. or is there a better way..

method 1: the nono
...
while(true){
x++; //some simple movement
y++;
paint(getGraphics());
try{
Thread.currentThread().sleep(delay);
}
catch(Exception e){}
}

method 2:
....
while(true){
long start = System.currentTimeMillis();
x++; //some simple movement
y++;
paint(getGraphics());
try {
Thread.sleep(Math.max(0, (start + delay) -
System.currentTimeMillis()));
}
catch (InterruptedException e) {};
}

method 3:
....
while(true){
long start = System.currentTimeMillis();
x++; //some simple movement
y++;
paint(getGraphics());
long sleepTime = delay - (System.currentTimeMillis() - start);
if (sleepTime > 0)
try{
Thread.currentThread().sleep(sleepTime);
}
catch(Exception e){}
}

method 4: frame independent

int velocity = 500;
long end_time = 0;
long start_time = System.currentTimeMillis();
float elapsed_time = 0;
float dtime = 0;
while(true){
end_time = System.currentTimeMillis();
elapsed_time = end_time - start_time ;
dtime = elapsed_time / 1000 ;
x += dtime * velocity; //some simple movement
y += dtime * velocity;
start_time = System.currentTimeMillis();
paint(getGraphics());
try{
Thread.currentThread().sleep(delay);
}
catch(Exception e){}
}

thanx
mike


Paul Lutus

2004-09-28, 4:13 pm

Mike Crenshaw wrote:

> Hi
>
> I'm trying to figure out how to make an animation move smoothly.. so i've
> been looking around..


Define "move smoothly". How many frames per second do you require? How large
is the animated frame? What is the color depth? What is the target
environment? What is the expected target platform's clock speed?

Define "looking around". Which painting methods have you tried?

> i've found 3 other methods besides just using
> sleep() at each frame.. i've tried implementing these methods but i cant
> see much of a difference.. but which of the following, if any, is the best
> way to go about it.. or is there a better way..


The "better way" is the one that delivers what you want, which you don't
specify. You don't specify what is being drawn, how it is being drawn, or
provide a working code example.

--
Paul Lutus
http://www.arachnoid.com

Mark Haase

2004-09-28, 9:28 pm

In article <d_f6d.1198$Wv1.248@news.get2net.dk>,
"Mike Crenshaw" <someone@microsoft.com> wrote:

> method 1: the nono


These are all "nonos". You shouldn't be calling paint() directly.

What you are probably referring to is double buffering, where you do
your drawing into an offscreen image, then copy the image onscreen all
at once, the effect being that the user doesn't see the flickering that
results from successive drawing operations over the same canvas.

If you're concerned about keeping a constant frame rate, then just call
System.currentTimeMillis() or whatever it is, and subtract how long you
spend painting from how long of a delay you want between frames, and
then Thread.sleep() for that long.

--
|\/| /| |2 |<
mehaase(at)sas(dot)upenn(dot)edu
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com