aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Swing animation with Start and Stop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing animation with Start and Stop" Watch "Swing animation with Start and Stop" New topic
Author

Swing animation with Start and Stop

nick emanuel
Greenhorn

Joined: Dec 10, 2012
Posts: 3
I've been testing out "randomness" with Java's swing by creating a bunch of random rectangles on the screen (each rectangle on top of the previous one). The program runs as an animation and I just close it or set a limit to the amount of iteration I want performed. The program runs fine but I would like to add a Start and a Stop button to it so I can use the GUI to stop the animation when I have seen the desired result and then when I hit the Start button again it clears the animation and restarts the whole thing. This way I won't need to set iterative limits or restart the program every single time.

So far this is all the code I've got:




I'd appreciate any help whatsoever. Please provide explanations as to how I can fix this since I am new at Java and intend to learn it better.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Thread.sleep(50);

this is absolutely a no-no in Swing.

use a Swing Timer, your start button calls timer.start(),
and, quess what the stop button will do?
nick emanuel
Greenhorn

Joined: Dec 10, 2012
Posts: 3
Michael Dunn wrote:> Thread.sleep(50);

this is absolutely a no-no in Swing.

use a Swing Timer, your start button calls timer.start(),
and, quess what the stop button will do?


Thank you. I followed your advice and figured out how to start and stop the animation. But it does not have the effect I wanted. It paints each rectangle on a new screen. Its as if it refreshes the screen and adds a rectangle on it afterwards. What I'm looking for is this: When I hit the start button, it should start painting all the recs on top of each other. Then when I hit stop. It should stop. Then when I hit start, it should clear the screen and start all over again. Any tips on where I should look to add the functionality? I'd once again appreciate help like Michael's where I am only given tips and not the whole code.

Here's my code so far:


Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
add the indicated line



[edit]
and it should be
myDraw.repaint()
nick emanuel
Greenhorn

Joined: Dec 10, 2012
Posts: 3
Michael Dunn wrote:add the indicated line



Thanks for your reply again Michael.



refreshes the frame every time a new rectangle is made. I want to avoid refreshing the page. Currently my program has the same affect as if I was using the super.paintComponent() method.

I want to be able to create rectangles one on top of the other and only refresh it every time I click the start button. Once it refreshes, I'd like for it to continue drawing rectangles one on top of the other (i.e. the previous ones don't disappear) until I click stop and start again.

Not sure if I am communicating well here...
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Not sure if I am communicating well here...

my fault, I mis-read what you posted.

changing
frame.repaint()
to
myDraw.repaint()
should give you the effect you're after
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4546
    
    5

nick emanuel wrote:I want to be able to create rectangles one on top of the other and only refresh it every time I click the start button. Once it refreshes, I'd like for it to continue drawing rectangles one on top of the other (i.e. the previous ones don't disappear) until I click stop and start again.

Not sure if I am communicating well here...


Yes, that's clear enough. You need to maintain a Collection (probably ArrayList) of Rectangles and iterate over the collection painting each Rectangle. Your stop/start would clear the list. You would call repaint() each time a Rectangle is added or when the list is cleared.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swing animation with Start and Stop