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.

animation timer

Thread view: 
Mike Crenshaw - 24 Sep 2004 16:22 GMT
hi

im programming an animation and im having trouble making it move smoothly.
im programming it in java 1.1 so i cant use the timer in swing..
if i was to make a timer of my own.. how would i go about doing that?

thanx
mike
Andrew Thompson - 24 Sep 2004 17:05 GMT
> im programming an animation and im having trouble making it move smoothly.
> im programming it in java 1.1 so i cant use the timer in swing..
> if i was to make a timer of my own.. how would i go about doing that?

Create a thread or implement Runnable.

For an example of implementing Runnable, check this code by Alberto V.
<http://www.physci.org/launcher.jsp?class=/codes/AnimateBalls/AnimateFrame#32>
and the run method itself..
<http://www.physci.org/launcher.jsp?class=/codes/AnimateBalls/AnimateFrame#57>

You can also launch it from the applet at the top of the page.

HTH

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.lensescapes.com/  Images that escape the mundane

Mike Crenshaw - 24 Sep 2004 17:43 GMT
how does this differ from just calling sleep()?
i've been told that in order to make an animation move smooth and evenly
wether you're on a slow computer or a fast one, you need to use a timer
instead of just calling sleep().. cuz sleep() doesnt guarantee it sleeps for
exactly x amount of time.. so i apparently need a timer to ensure this.. but
does the swing timer work the same way as the one in the link?,,
my animation is already a thread..  but it just calls sleep() at every
update. instead of letting a timer handle the delay..

thanx..
mike

>> im programming an animation and im having trouble making it move
>> smoothly.
[quoted text clipped - 11 lines]
>
> HTH
Andrew Thompson - 24 Sep 2004 21:53 GMT
> how does this differ from just calling sleep()?

How does what differ, Mike?  
<http://www.physci.org/codes/javafaq.jsp#netiquette>

> i've been told ..you need to use a timer
> instead of just calling sleep().. cuz sleep() doesnt guarantee it sleeps for
> exactly x amount of time.. so i apparently need a timer to ensure this.. but
> does the swing timer work the same way as the one in the link?..

Check for yourself.
<sscce>
// To gain smooth time based animation, the renderer needs
// to calculate the positions of the visible elements at the
// time of painting the Graphics object.
import java.awt.event.*;
import javax.swing.Timer;
import java.util.Date;

/** This example demonstrates the time drift of both a
Thread and a javax.swing.Timer set for '1 second' delay.
Using Java 1.5.0 beta, the Timer drifts off time faster
than the Thread.
@author Andrew Thompson */
public class TimeDrifter implements Runnable, ActionListener {

  /** The Timer used in this example */
  Timer timer;

  /** Time started. */
  long timerStart, threadStart;

  /** Duration of test in seconds (give or take 1). */
  final int DURATION = 120;

  /** Compile true for more detailed output
  but less acccurate results. */
  final boolean TEST = false;

  TimeDrifter() {
     //timer.addActionListener(this);
     Thread t = new Thread(this);

     System.out.println("Test duration: " + DURATION + " seconds." );
     System.out.println("Start thread test" );
     threadStart = System.currentTimeMillis();
     t.start();
  }

  public void timerStart() {
     timer = new Timer(1000, this);

     System.out.println("Start timer test" );
     timerStart = System.currentTimeMillis();
     timer.start();
  }

  /** Starts a new Thread to sleep for 1000 ms. */
  public void run() {
     while (true) {
        try {
           float timeInSec =
              (System.currentTimeMillis()-threadStart)/1000f;
           if (TEST) System.out.println("thread: \t" +
              (timeInSec-(int)timeInSec) );
           if ( (int)timeInSec>DURATION ) {
              System.out.println("thread drift:\t " +
                 (timeInSec-(int)timeInSec) +
                 " / over: " + (int)timeInSec + " seconds" );

              timerStart();

              Thread.sleep(DURATION*2*1000);
           } else {
              Thread.sleep(1000);
           }
        } catch (InterruptedException ie) {
              // ignore
        }
     }
  }

  public void actionPerformed(ActionEvent ae) {
     float timeInSec =
        (System.currentTimeMillis()-timerStart)/1000f;
     if (TEST) System.out.println("timer: \t" +
        (timeInSec-(int)timeInSec) );
     if ( (int)timeInSec>DURATION ) {
        timer.stop();
        System.out.println("timer drift: \t " +
           (timeInSec-(int)timeInSec) +
           " / over: " + (int)timeInSec + "seconds" );
        //((Runnable)this).Thread.interrupt();
        System.exit(0);
     }
  }

  public static void main(String[] args) {
     new TimeDrifter();
  }
}
</sscce>

Run 1;

Test duration: 120 seconds.
Start thread test
thread drift:    0.04399872 / over: 121 seconds
Start timer test
timer drift:     0.4850006 / over: 121seconds
<http://www.physci.org/test/graph/index.jsp?nm=Drift+in+ms&lbl=Thread+Timer&val=4
4+485&width=80
>

Run 2:

Test duration: 120 seconds.
Start thread test
thread drift:    0.125 / over: 121 seconds
Start timer test
timer drift:     0.22399902 / over: 121seconds
<http://www.physci.org/test/graph/index.jsp?nm=Drift+in+ms&lbl=Thread+Timer&val=1
25+223&width=80
>

Run 3:

Test duration: 120 seconds.
Start thread test
thread drift:    0.033996582 / over: 121 seconds
Start timer test
timer drift:     0.20400238 / over: 121seconds
<http://www.physci.org/test/graph/index.jsp?nm=Drift+in+ms&lbl=Thread+Timer&val=3
4+204&width=80
>

Run 4:
Test duration: 120 seconds.
Start thread test
thread drift:    0.024002075 / over: 121 seconds
Start timer test
timer drift:     0.2539978 / over: 121seconds
<http://www.physci.org/test/graph/index.jsp?nm=Drift+in+ms&lbl=Thread+Timer&val=2
4+254&width=80
>

HTH

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.lensescapes.com/  Images that escape the mundane



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.