File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

how to add timer to this code?

Karen Wilson
Ranch Hand

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

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

Moving to our GUIs forum.
Tony Docherty
Bartender

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

Joined: Jun 13, 2009
Posts: 2091
    
    7
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: 4339
    
    2

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


SCJP
Visit my download page
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to add timer to this code?
 
Similar Threads
Help needed with the Code
Why does it not show the new coordinates?!!??!!
A Paint Application with Combo Boxes and Buttons
Parallel dragging
Wanted: Java2D example of zooming and panning with scroll bars