Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

animation in applet

 
Paul Chamsay
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Chamsay
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf! Works great!



 
Paul Chamsay
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds still like something that can be handled by parameters.
 
Paul Chamsay
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it make sense if I do a setName() on those two threads then do something like:




Is that what you meant?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic