| Author |
Scatter Objects in canvas Using Swing API/Java
|
Azrael Noor
Ranch Hand
Joined: Jul 29, 2010
Posts: 369
|
|
Hello
I am making a project in which i need to scatter objects (Houseflies) on Content pane.
What i feel is that I need to write complex code using for loops and judge pixel location if i want to move Object freely around content pane.
right now, my housefly is coming straight from Window and go straight to sweet and eating it ( ------------------------------------- )
I want house fly to scatter around content pane, and when it strikes sweet, other also accompany it.
This is just a small module of project and i am new to this Swings API.
Is their any Function available in which objects move freely around or do i have to control it through iterations, cos, sins and if's, threads?
Please enlighten
Thanks
|
Regards
Azrael Noor
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
Write down what you mean by scatter. Do you want random locations? Do you want random speed and direction, vaguely towards the sweet?
When you have got that worked out, consider what sort of algorithm you want. Remember you can work out x and y coordinates for your fly and sweet, so you cna probably get away without using sin and cos. If you randomly move the fly a proportion of the x distance and a proportion of the y distance towards the sweet, you might get the desired effect.
And proportion does not necessarily mean < 100%.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16479
|
|
|
For an animation (i.e. you want to change the image slightly at regular intervals) then you use a javax.swing.Timer. It regularly schedules a TimerTask, and each time that's run (via I think its actionPerformed method, but I haven't checked) you should draw the next iteration of the animation.
|
 |
Darryl Burke
Bartender
Joined: May 03, 2008
Posts: 4163
|
|
Paul Clapham wrote:For an animation (i.e. you want to change the image slightly at regular intervals) then you use a javax.swing.Timer. It regularly schedules a TimerTask
Um, no, that's what java.util.Timer does. A Swing Timer runs the actionPerformed(...) of any and all ActionListeners added to it.
|
luck, db
There are no new questions, but there may be new answers.
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: Scatter Objects in canvas Using Swing API/Java
|
|
|