Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes Scatter Objects in canvas  Using Swing API/Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Scatter Objects in canvas  Using Swing API/Java" Watch "Scatter Objects in canvas  Using Swing API/Java" New topic
Author

Scatter Objects in canvas Using Swing API/Java

Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
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: 38016
    
  22
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: 18541
    
    8

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: 4523
    
    5

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Scatter Objects in canvas Using Swing API/Java
 
Similar Threads
Urgent: Glass Pane is Opaque!!
Newbie question on content pane
Drag and Drop where source and destination are the same.
Make Line Graph scrollable in a JScrollPane?
Glasspane and JDialog