aspose file tools*
The moose likes Applets and the fly likes animation in applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "animation in applet " Watch "animation in applet " New topic
Author

animation in applet

Paul Chamsay
Ranch Hand

Joined: Feb 06, 2009
Posts: 36
Hi,

First of all, I'm sorry if this is the wrong forum to post my problem. I wasn't sure if it would fit better in the threads forum or here or the beginner's ..

I have this task of animating 2 strings and one image in an Applet.

Basically what the 2 strings would do is move from left to the right of the screen and the image will move from right to left, all at the same time.

My question is, do I use 3 threads in my applet so those 2 strings and the image will move accordingly all at the same time?

Thanks!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42285
    
  64
Yes, that's perfectly possible. Each animated object can have its own thread. If the strings move in tandem, then one thread for both of them may suffice, but there's nothing wrong with creating multiple threads.


Ping & DNS - my free Android networking tools app
Paul Chamsay
Ranch Hand

Joined: Feb 06, 2009
Posts: 36
Thanks! So I got started on this.. This is what I have so far:



I can't get it to work using that way.. Any ideas why? It doesn't move at all.. Also, I'm not sure how to have 2 threads going on the same time. I'm planning to create 1 more class..

Also, is it possible to have a paint method that does something different from another paint method?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42285
    
  64
Why is Move extending Test2Threads? That's not the way to go. Here's a Move class that'll work. I'll leave it to you to figure which changes you need to make to the main class to use it:
Paul Chamsay
Ranch Hand

Joined: Feb 06, 2009
Posts: 36
Thanks Ulf! Works great!



Paul Chamsay
Ranch Hand

Joined: Feb 06, 2009
Posts: 36
Hi, I worked on it again tonight and this is what I have:



I'm curious about the music thread though.. Is it okay if it's like that or should a separate thread be created for it? I'm thinking that I have too many classes for a small task like this ..

Also, I would like to know if the code has good OO design? Thanks!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42285
    
  64
I wouldn't use separate classes Move and Move2. They only differ in the way the for-loop is set up. That could be controlled be a boolean parameter ("up" or "down") that is passed into the constructor.
Paul Chamsay
Ranch Hand

Joined: Feb 06, 2009
Posts: 36
I have additional codes inside the for loop of Move2

Something like:



Where I switch images. Do you think I should still combine though?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42285
    
  64
Sounds still like something that can be handled by parameters.
Paul Chamsay
Ranch Hand

Joined: Feb 06, 2009
Posts: 36
Does it make sense if I do a setName() on those two threads then do something like:




Is that what you meant?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42285
    
  64
I wouldn't use the thread name, but yes, you'd need some differentiating logic. All data that's relevant for that logic should be passed in through the constructor or through setter methods.
Paul Chamsay
Ranch Hand

Joined: Feb 06, 2009
Posts: 36
Hi Ulf, I really tried to think of some differentiating logic but I just can't figure out how the run method will know which for loop to run .. If I'm not mistaken, you were trying to tell me to do something like this where there are two threads but only one job (run method).



Any clues? I really appreciate your help.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42285
    
  64
you were trying to tell me to do something like this where there are two threads but only one job (run method).

Almost :-) What I meant was to use just one class; but you will need two separate objects. So it would be something like:

and the Move constructor would be
public Move (Test2Threads parent, int appWidth, boolean leftToRight)

Then you just need to think about how to use the "leftToRight" parameter for controlling the loop.
Paul Chamsay
Ranch Hand

Joined: Feb 06, 2009
Posts: 36
Ah, you're a genius Ulf, thanks a lot!

I never thought of actually creating two move objects .. I kept on thinking how to identify which thread is going to call the run() method
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: animation in applet