Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scatter Objects in canvas Using Swing API/Java

 
Azrael Noor
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48649
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20946
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic