Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2004

Tip: Looking for answers? Try searching our database.

smooth animation

Thread view: 
Mike Crenshaw - 28 Sep 2004 17:06 GMT
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 - 28 Sep 2004 17:50 GMT
> 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.

Signature

Paul Lutus
http://www.arachnoid.com

Mark Haase - 28 Sep 2004 22:14 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.