Results 1 to 8 of 8
  1. #1
    angelusofnex is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default 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
    The output is currently nothing since i have private int shapesShown set to 0, I'm assuming my issue is with my timer. Please help?!?!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    angelusofnex is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default 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!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  5. #5
    angelusofnex is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default 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();}
    }
    and various others. I just can't figure out what is wrong.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  7. #7
    angelusofnex is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default 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.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Swing Timer issue for Drawing Shapes

    Quote Originally Posted by angelusofnex View Post
    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.
    I think that you'll learn more if you try to implement my suggestions, and then show us what you've got and any error messages you may see. The more effort you put into this the more you'll get out of it.

Similar Threads

  1. Drawing Shapes on to a Jpanel
    By pepsi in forum AWT / Swing
    Replies: 7
    Last Post: 03-09-2012, 06:39 PM
  2. Drawing different shapes with stars.
    By Amazineous in forum New To Java
    Replies: 23
    Last Post: 11-14-2011, 04:55 PM
  3. Drawing issue with Applet/Swing
    By The_Capn in forum Java Applets
    Replies: 2
    Last Post: 06-18-2011, 06:30 AM
  4. drawing shapes in java help
    By alphajoseph in forum Java 2D
    Replies: 2
    Last Post: 09-29-2009, 07:35 PM
  5. [SOLVED] Swing Timer issue
    By Doctor Cactus in forum New To Java
    Replies: 6
    Last Post: 03-03-2009, 01:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •