File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Animation Threads... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Animation Threads..." Watch "Animation Threads..." New topic
Author

Animation Threads...

Anthony Yulo
Ranch Hand

Joined: Feb 22, 2003
Posts: 52
I have a main thread running the animation loop that increment timers and draw all sprites...
I also have the sprite threads...

in the sprite thread...

and then in the main thread...
[CODE]
void run{
while(true)
{
//increment timer()
//notify sprite threads of timer increment so they can udpate position
//wait until all sprite threads have updated their positions
//draw the sprite locations
delay(timerdelay);
}
}
So thats what i want to do..HOw do i implement this?
Where do i put the synchronized keyword? what object do i put it against?
DO i use Object.notifyAll() and Object.wait()?


it's alright, the gun isn't loaded... - hey
Abdul Narayan D'souza TP
Greenhorn

Joined: Apr 01, 2004
Posts: 10
Anthony,
You could try this. I have assumed that each sprite thread has
a reference to a sprite object. i mean one to one mapping of sprite objects
and sprite threads
class YourTimer{
incrementTimer(){
//increment code goes here
}
}
class Sprite{
//Keeps track of how many sprite intances are created /
//threads are running
static int spriteCount;
updatePosition(){
//
}

drawSprite(Graphics g){
//drawing code goes here
}
}
volatile boolean timerIncremented=false;
volatile int drawingComplete=0;
Main Thread
void run{
while(true)
{
synchronized(yourTimer){
//increment timer()
yourTimer.incrementTimer();
timerIncremented=true;
//notify sprite threads of timer increment so
//they can udpate position
yourTimer.notifyAll();
}
drawingComplete=Sprite.spriteCount;
while(drawingComplete!=0){
//wait until all sprite threads have updated their
//positions
sleep(delay);//surround with try catch
}
//draw the sprite locations
//call the drawSprite method of each sprite object
delay(timerdelay);
}
}
Sprite Thread
void run()
{
while(true){
synchronized(yourTimer){
if(!timerIncremented)
{
yourTimer.wait();//surround with try catch
}
}
//wake up when the timer has incremented..
// timer is in the mainthread
sprite.updateposition();
//decrement drawingComplete
drawingComplete--;
}
}
I am pretty sure this will work.
-
 
Consider Paul's rocket mass heater.
 
subject: Animation Threads...
 
Similar Threads
Applet Image Animation
Why does a char value also have an integer value?
Need Explanation of unpredicted output even if methods are synchronized
Animating independent objects?
Problem in doing animation using multithreading