• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help in Algorithm Construction

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody!
I can't seem to figure out how to make an array of circles move!
The problem requires me to animate a number of circles input by the user clockwise around a square of side 400 pixels with a 20 Milliseconds delay. At each move the circles must be capable of changing direction with probability input by the user at run-time. When all circles are in a state where they move anticlockwise, the animation stops.
Now there are two classes, a Circle class that encapsulates the circle's state and a Main class, which I used to draw the array of circles.
In the main class the code below calls a move method, which is in the Circle class to move each circle. I am having difficulties with coding the actual move method.
for (int j = 0; j < circles; j++) {
circle[j].move();
}
Here is the code of the two classes. So far the circles have been drawn and the probabilites and number of circles have been input, but there is no animation happening. Could anyone please explain how to make the circles (which form the shape of a snake) move, as my logic doesn't seem to be working. Below is what I've done. I'd appreciate any comments as to why, although the code in the move method itself is fine, nothing happens as a result.
Circle Class:
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

That's a bit tricky to read, i would sugest to write
if (clockwise)
do stuff
and drop the else clause unless you have some code for it.
Now your problem is that nothing is been drawn. I've made some animation with applets, and the one thing to do after something changed is to call paint(). Try to call this for your DialogBox in the main-method, after the call to move(). Your code doesn't tell me what DialogBox is, so you have to try it. IIRC all Swing components provide paint().
How is it going? Let us know.
Tobias
 
Amil Mehmedagic
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tobias and thanks for posting!


Now your problem is that nothing is been drawn.


For this task I was provided with classes that create the initial circles.
CirclesFigure.create( ); and CirclesFigure.draw(circle); draws the initial circles. The program requests the user to Input the number of circles at the beginning and that number of circles is displayed in the window.
Ok here is another unsuccessful attempt of mine at the move method. The first circle appears at initial position 10,10. I found this out using Transcript.println(getX());Transcript.println(getY()); This is why I used radius as the variable from where the array of circles moves!
Yet, there there seems to be something missing from the code, because the circles don't move. Am I using the wrong logic here?
 
Tobias Hess
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I don't understand the logic in your move()-method.
However, before attacking that logic, you should replace it with something that _allways_ changes the Position of your circels, let's say let them move to the right by 10 pixels. Now, have a look. If they move, check the logic. If they still not move, and that's what I guess, you have a problem with repainting the circels after changing they're position.
 
Amil Mehmedagic
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After working on this for hours, I've finally figured out how to move the array of circles around and change the direction according to the given probability. However what I still haven't figured out is how to make the animation stop when the circles are moving anti-clockwise. To be more precise each time the circles move a random number is generated and if it's less than the given probability, the circle changes direction and starts moving anticlockwise. Now when the circles are moving anti-clockwise the random number needs to be generated again and if it's less than the given probability, the animation (of that particular circle) needs to stop.
Below are the two classes. Note that only the move method of the Circle class needs to be changed slightly. I have tried it myself without success. Please if there is someone out there who could assist me with this problem, I 'd greatly appreciate it.
Circle Class

Main Class
 
reply
    Bookmark Topic Watch Topic
  • New Topic