jQuery in Action, 3rd edition
The moose likes Threads and Synchronization and the fly likes Why does animation need threads???? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Why does animation need threads????" Watch "Why does animation need threads????" New topic

Why does animation need threads????

Dee Reavis

Joined: Dec 13, 2000
Posts: 8
This is modified animation code from the code barn. I have essentially just removed the thread. If you execute this code it will just show the final position of the rectangle, but none of the intermediate positions. Why can't I see the animation this way(on the main thread)? What does an additional thread add?
import java.awt.* ;
import java.applet.Applet ;
public class HelloAnimThreadFirst extends Applet // implements Runnable

private int imageHeight , imageWidth ; //size of animating rectangle
private int currX , currY ; // left,top corner of rectangle

public void init()
imageHeight = 25 ;
imageWidth = 40 ;
// set up starting position
currX = 30 ;
currY = 30 ;
// create and start the thread
// Thread animator = new Thread( this );
// animator.start();
} //close init

public void run()
// update location of the rectangle 40 times, and call repaint each time
for ( int i = 0 ; i <= 80 ; i++ )
{for(int j = 0;j<10000000;j++){}
// update the x and y coordinates for the rectangle
currX = currX + 3 ;
currY = currY + 3 ;
// now call repaint which will ultimately lead to paint being called
} //close for loop
} //close run

public void paint( Graphics g )
g.drawRect( currX , currY , imageWidth , imageHeight );
} // close paint
} // close class

Rahul Rathore
Ranch Hand

Joined: Sep 30, 2000
Posts: 324
In your single-thread version, the reason why the applet shows only the final position of the rectangle is that - the first ever call to the paint() of applet is only AFTER init() returns. But when init() returns, the run() method has completed execution- so when the first call is made to paint() only the final position of the rectangle is retained.
Note that repaint() calls in the run() do not directly call the paint() method- rather these calls simply schedule calls to paint()-and the first call to paint will only be after init() has returned.
We may rectify by making the call to run() in the start() method rather than the init() method. Again no separate thread is created. But we have no control over animation which will execute completely once start() is called. And if start() is executing, how will other methods like stop() execute ?
In the original code example we start a separate thread in init()- then thread executing init() will immediately return and then applet paint() is called. The repaint calls in the concurrently executing animation thread will therefore take effect.
It is probably clear by now why we want a separate thread. The reason is that we may want to control the animation. For eg. we may use the start() and stop() method for starting/resuming animation and the stop() method for suspending animation.

[This message has been edited by Rahul Rathore (edited March 07, 2001).]
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: Why does animation need threads????
It's not a secret anymore!