Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to add timer to this code?

 
Karen Wilson
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have two classes - one just creates the jFrame and the other is for displaying shapes onto this Frame
right now the code just displays a random rectangle every time i run the program
but i want to add a timer so a rectangle appears every second
also the other rectangles should not disappear (so eventually the frame fills with rectangles)

here is the shapes class:


so how do i add a timer function to this code to make a random rectangle appear every second?
 
Campbell Ritchie
Sheriff
Posts: 48448
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't you find anything useful in the Java Tutorials?

Moving to our GUIs forum.
 
Tony Docherty
Bartender
Posts: 2943
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For timed events with Swing I would check out the javax.swing.Timer class
 
Rob Camick
Ranch Hand
Posts: 2542
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also the other rectangles should not disappear


Before worrying about the Timer you need to get your program working to support painting multiple shapes. I would suggest you can read the following for some basic ideas:

1. Custom Painting Approaches - for the two basic ways to do incremental painting
2. Playing With Shapes - for a more flexible way to paint a Shape.

Once you decide an which approach to use, you will need to create an addShape(...) method. That you can first test out manually. Once this works you then start working with the Swing Timer to generate random Shapes dynamically.
 
Randall Twede
Ranch Hand
Posts: 4363
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i had a homework assignment in college that was similar to this. the instructor had us make a shape class and use a Vector to hold them. during repaint we went through the vector andd repainted them all. later i made it into a "real" program and i used an ArrayList that held BufferedImages
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic