0

I have the following scenario: If I have a while block in the paint() method (used for example to simulate a simple animation such as rotating a polygon, done by multiple drawing and erasing the figure), is there a way to break the while block, when clicking the mouse inside the applet?

The animation of the polygon is done without recalling the paint() method. Also would it be possible to do so if the while block looked something like this:

while (count<n)
{
    //code that draws the polygon rotating
    count++;
}
Tobias Sarnow
  • 1,076
  • 2
  • 12
  • 40
biggdman
  • 2,028
  • 7
  • 32
  • 37

2 Answers2

1

Yes there is a scenario to hold on your while loop.

The simpliest way would be to set up a variable in your classfile private boolean stopLoop=false and within your while loop check for this attribute while (!stopLoop).

Now the MouseEvent just set the attribute stopLoop=true and you are done (if you need help, here you are How to Write a Mouse Listener

The other solution is using Swing Timer as mentioned by @camickr (see other answer). Lets assume you have a general Timer method outside your paint() method. Then you sould't use a while loop in there. I would suggest to just paint a static picture and if you want that your poligon rotates, just draw the next one, but with another angle and so on.

The idea is that you cut out your while loop into the Timer method so paint() gets called a lot of times. If you want to stop the poligon from circling around use a boolean flag for it or stop the timer. In the first case you can handle more then one polygon and each of them can be started and stopped, if you handle the boolean variables and the mouse event correct.

If you have further questions please add some more detail, or bedder show us some minimized code.

Tobias Sarnow
  • 1,076
  • 2
  • 12
  • 40
  • I understand what you are saying, but I need the object to be animated when clicked, and stop being animated when you click somewhere else. Do you see a solution for this? Thanks a lot – biggdman Nov 10 '11 at 23:05
  • What do you mean by somewhere else? Do you mean outside your paint area, for eg. a Panel surrounded with buttons, other panels and so on? If you want to hit your polygon, you need to compare the coordinates from the mouse event to the polygon ones. Please describe a little bit more about the layout and your problem. – Tobias Sarnow Nov 11 '11 at 07:25
  • By somewhere else, I mean outside the area where the polygon is drawn.(when I want my animation to start that is what I am doing: comparing the coord of the mouse event with the coord of the polygon; my problem is I want the animation to stop when I click outside the polygon). There is only one panel where I am drawing. – biggdman Nov 11 '11 at 10:56
1

Don't use a while loop.

Instead use a Swing Timer to schedule the animation. Then you can simply start/stop the timer as required.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Can the swing timer be interrupted by a mouse click, if the animation was triggered by a mouse click( for example if you click on the area where a polygon is drawn it begins to animate, and it stops when you click again). Thanks a lot – biggdman Nov 10 '11 at 22:58
  • And if you want to know how SwingTimer work, look here: http://stackoverflow.com/questions/8088002/how-to-use-a-swing-timer-to-start-stop-animation :D – Tobias Sarnow Nov 11 '11 at 07:32