• 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

Swing animation with Start and Stop

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
add the indicated line



[edit]
and it should be
myDraw.repaint()
 
nick emanuel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic