This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Help in Algorithm Construction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help in Algorithm Construction" Watch "Help in Algorithm Construction" New topic
Author

Help in Algorithm Construction

Amil Mehmedagic
Greenhorn

Joined: Feb 27, 2004
Posts: 22
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:
Tobias Hess
Ranch Hand

Joined: Apr 06, 2004
Posts: 55

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

Joined: Feb 27, 2004
Posts: 22
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

Joined: Apr 06, 2004
Posts: 55
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

Joined: Feb 27, 2004
Posts: 22
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help in Algorithm Construction