Results 1 to 8 of 8
- 10-07-2012, 03:25 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
Swing Timer issue for Drawing Shapes
hello, I'm in the process of creating a code that will draw 10 of either a rectangle, oval or round rectangle depending on the users input. My goal is to have all 10 shapes drawn one at a time with a 2 second gap between each.
Here is my code:
Java Code:import java.awt.Graphics; import javax.swing.JPanel; import javax.swing.Timer; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class Shapes extends JPanel implements ActionListener { private int choice; // user's choice of which shape to draw private Timer timer; private int shapesToShow = 10; //Total of shapes to show private int shapesShown = 0; //Currently shown shapes // constructor sets the user's choice public Shapes( int userChoice ) { choice = userChoice; timer = new Timer(2000, this); //200ms delay and fire an event to this class. The actionPerformed method will be called timer.setInitialDelay(1000); timer.start(); } // end Shapes constructor // draws a cascade of shapes starting from the top-left corner public void paintComponent( Graphics g ) { super.paintComponent( g ); for ( int i = 0; i < shapesShown; i++ ) { // pick the shape based on the user's choice switch ( choice ) { case 1: // draw rectangles g.drawRect( 10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10 ); break; case 2: // draw ovals g.drawOval( 10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10 ); break; case 3: //draw rounded rectangles g.drawRoundRect(10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10, 10, 10 ); break; } // end switch } // end for } // end method paintComponent @Override public void actionPerformed(ActionEvent evt) { repaint(); //Repaint the JPanel } //This method will be called when we should show a new shape } // end class Shapes
-
Re: Swing Timer issue for Drawing Shapes
Don't use a for loop in the paintComponent method as that doesn't make sense. Why not instead increment a counter in the Swing Timer and use that counter in paintComponent to decide what to display.
- 10-10-2012, 04:15 AM #3
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
Re: Swing Timer issue for Drawing Shapes
I am still looking for help on this!!!! Please, I have looked all over the internet and cant figure this out.... HELP!
-
Re: Swing Timer issue for Drawing Shapes
Consider showing your latest code attempt, describing how exactly it is not working, and what specifically you're stuck on.
- 10-10-2012, 04:35 AM #5
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
Re: Swing Timer issue for Drawing Shapes
still going off the same code as in my original post, I sadly didn't save my failed attempts. I spoke with my professor and he says im missing something in my actionPerformed method. I've tried using if statements like this
Java Code:public void actionPerformed(ActionEvent evt) { if(evt.getSource() == choice1){ drawRect=true; drawOval=false; drawRoundRect=false; repaint();} }
-
Re: Swing Timer issue for Drawing Shapes
As I stated previously, you could simply increment the count int variable in actionPerformed and use that variable in the switch statement in your paintComponent method. But again get rid of the for loop as I don't see it doing anything useful.
A key point that has helped me is to break down what you're trying to do into small steps and then try to solve each step in isolation of the other. This way you have easier problems to solve and don't get to feeling overwhelmed.
- 10-10-2012, 06:02 AM #7
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
Re: Swing Timer issue for Drawing Shapes
could you give me an example of how this would look? I'm currently trying to figure out how to implement the counter properly, a code example would be very helpful.
-
Re: Swing Timer issue for Drawing Shapes
Similar Threads
-
Drawing Shapes on to a Jpanel
By pepsi in forum AWT / SwingReplies: 7Last Post: 03-09-2012, 06:39 PM -
Drawing different shapes with stars.
By Amazineous in forum New To JavaReplies: 23Last Post: 11-14-2011, 04:55 PM -
Drawing issue with Applet/Swing
By The_Capn in forum Java AppletsReplies: 2Last Post: 06-18-2011, 06:30 AM -
drawing shapes in java help
By alphajoseph in forum Java 2DReplies: 2Last Post: 09-29-2009, 07:35 PM -
[SOLVED] Swing Timer issue
By Doctor Cactus in forum New To JavaReplies: 6Last Post: 03-03-2009, 01:25 PM
Bookmarks