permaculture playing cards
The moose likes Swing / AWT / SWT and the fly likes how to add timer to this code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how to add timer to this code?" Watch "how to add timer to this code?" New topic

how to add timer to this code?

Karen Wilson
Ranch Hand

Joined: Nov 19, 2012
Posts: 70
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

Joined: Oct 13, 2005
Posts: 46367
Didn't you find anything useful in the Java Tutorials?

Moving to our GUIs forum.
Tony Docherty

Joined: Aug 07, 2007
Posts: 2839
For timed events with Swing I would check out the javax.swing.Timer class
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2451
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

Joined: Oct 21, 2000
Posts: 4351

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

Visit my download page
I agree. Here's the link:
subject: how to add timer to this code?
It's not a secret anymore!