Java Shapes Sleep question

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 1903 Views - Last Post: 11 October 2012 - 02:47 PM Rate Topic: -----

#16 sescobedo89  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 06-October 12

Re: Java Shapes Sleep question

Posted 06 October 2012 - 04:15 PM

 @Override
   public void actionPerformed(ActionEvent evt) {
	//Timer tick
	showShape();
    //Should we show a new shape again?
	
	repaint(); //Repaint the JPanel  
	}
   
   //This method will be called when we should show a new shape
   private void showShape() {

	//Implement logic to show a new shape
   }
} // end class Shapes 



this is what im confused about, you said i need to add an event to my actionPerformed method, can you give me and example of what that would look like?

Also the "private void showShape()" method, can you give me an example of what the logic might look like?
Was This Post Helpful? 0
  • +
  • -

#17 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,097
  • Joined: 05-April 11

Re: Java Shapes Sleep question

Posted 06 October 2012 - 04:18 PM

View Postsescobedo89, on 06 October 2012 - 11:15 PM, said:

 @Override
   public void actionPerformed(ActionEvent evt) {
	//Timer tick
	showShape();
    //Should we show a new shape again?
	
	repaint(); //Repaint the JPanel  
	}
   
   //This method will be called when we should show a new shape
   private void showShape() {

	//Implement logic to show a new shape
   }
} // end class Shapes 



this is what im confused about, you said i need to add an event to my actionPerformed method, can you give me and example of what that would look like?

Also the "private void showShape()" method, can you give me an example of what the logic might look like?


You don't need to add an event, I hope I didn't write that you should
The actionPerformed method is called by the timer. The timer calling the actionPerformed method is the event im talking about

I could give you an example of the logic in the showShape method, but then I would have made your homework for you =o ?
Was This Post Helpful? 0
  • +
  • -

#18 sescobedo89  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 06-October 12

Re: Java Shapes Sleep question

Posted 06 October 2012 - 04:26 PM

I will continue to look for tutorials on swing timers, if you have any helpful links off the top of your head then that'd be great. I'll do more research on the showShape method.
Was This Post Helpful? 0
  • +
  • -

#19 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,097
  • Joined: 05-April 11

Re: Java Shapes Sleep question

Posted 06 October 2012 - 04:29 PM

the showShape method is one I added. You can delete it if you want.

As I said before, you can just keep the timer implementation and remove the rest if you want :)
Was This Post Helpful? 0
  • +
  • -

#20 sescobedo89  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 06-October 12

Re: Java Shapes Sleep question

Posted 06 October 2012 - 07:03 PM

Still can't figure out whats wrong with my timer :sad2:
Was This Post Helpful? 0
  • +
  • -

#21 sescobedo89  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 06-October 12

Re: Java Shapes Sleep question

Posted 11 October 2012 - 02:28 PM

I have finally got my program to draw the shapes one at a time, only problem is it is not stopping after drawing 10 of the specified shape. Here is the code in question:
//CS1400-005
//Homework3
//Sierra Escobedo
//Version 3
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
   private int shapesCount = 0;
   // constructor sets the user's choice
   public Shapes( int userChoice )
   {
     choice = userChoice;
	  timer = new Timer(1000, this); //200ms delay and fire an event to this class. The actionPerformed method will be called
 	  timer.setInitialDelay(500); 
	  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)
	 {
	 	if (shapesCount < shapesToShow)
		{

			++shapesShown;
			repaint();
			}
			
		}
		
   
   //This method will be called when we should show a new shape
} // end class Shapes 



Anyone know what i need to add and where to make it stop drawing after 10 shapes?

Never mind i got it! just needed to add

if (shapesShown == 10)
		{	timer.stop();	
			}



to my actionPerformed method. Thanks for all the help!!
Was This Post Helpful? 1
  • +
  • -

#22 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,097
  • Joined: 05-April 11

Re: Java Shapes Sleep question

Posted 11 October 2012 - 02:47 PM

What is shapesCount? I thought that was what shapesShown was for.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2