Java Shapes Sleep question

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 sescobedo89  Icon User is offline

  • New D.I.C Head

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

Java Shapes Sleep question

Posted 06 October 2012 - 12:33 PM

Hello, I am pretty new to java and am working on a draw shapes code. I need each shape to be drawn one at a time and am having trouble figuring it out.

here is my first code:

//CS1400-005
//Homework3
//Sierra Escobedo
//Version 3
import java.awt.Graphics;
import javax.swing.JPanel;

public class Shapes extends JPanel
{
   private int choice; // user's choice of which shape to draw
   
   // constructor sets the user's choice
   public Shapes( int userChoice )
   {
      choice = userChoice;
   } // 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 < 10; 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
} // end class Shapes 


Here is my second code;
//CS1400-005
//Homework3
//Sierra Escobedo
//Version 3
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class ShapesTest
{
   public static void main( String[] args )
   {
      // obtain user's choice
      String input = JOptionPane.showInputDialog(
         "Enter 1 to Draw Rectangles\n"  +
         "Enter 2 to Draw Ovals\n"	+
			"Enter 3 to Draw Rounded Rectangles\n");
		
      int choice = Integer.parseInt( input ); // convert input to int
      
      // create the panel with the user's input
      Shapes panel = new Shapes( choice );
      
      JFrame application = new JFrame(); // creates a new JFrame
		application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );   
		application.add( panel ); // add the panel to the frame
      application.setSize( 300, 300 ); // set the desired size
   	application.setLocationRelativeTo (null ); // puts frame in center of screen    
	   application.setVisible( true ); // show the frame
   } // end main
	
}// end class ShapesTest


I've tried using sleep timers for example, Thread.sleep(2000). I'm not sure what to do at this point. PLEASE HELP!!

Is This A Good Question/Topic? 0
  • +

Replies To: Java Shapes Sleep question

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1378
  • View blog
  • Posts: 3,038
  • Joined: 05-April 11

Re: Java Shapes Sleep question

Posted 06 October 2012 - 01:03 PM

I need a little more explanation of your problem

Quote

I need each shape to be drawn one at a time

You want a delay between drawing rectangles, ovals, and rounded rectangles, or do you want a delay between drawing 10 of the selected shape?

With swing you should not be using Thread.sleep, but instead use a swing timer
Link to the swing timer class
Was This Post Helpful? 0
  • +
  • -

#3 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 - 01:05 PM

i want a delay between drawing 10 of the selected shape, for example rectangle 1 is drawn, 2 seconds later rectangle 2 is drawn, until all 10 are drawn.
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1378
  • View blog
  • Posts: 3,038
  • Joined: 05-April 11

Re: Java Shapes Sleep question

Posted 06 October 2012 - 01:24 PM

View Postsescobedo89, on 06 October 2012 - 08:05 PM, said:

i want a delay between drawing 10 of the selected shape, for example rectangle 1 is drawn, 2 seconds later rectangle 2 is drawn, until all 10 are drawn.


I will help you setting up the timer :)

You will have to change the way you go out the problem
There should be no logic in your paint method, the shapes that should be drawn should have been chosen beforehand
In every call to the paintComponent you must draw every shape again, and not only the next one

//CS1400-005
//Homework3
//Sierra Escobedo
//Version 3
import java.awt.Graphics;
import javax.swing.JPanel;

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(200, this); //200ms delay and fire an event to this class. The actionPerformed method will be called
   } // 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) {
	//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 post has been edited by CasiOo: 06 October 2012 - 01:24 PM

Was This Post Helpful? 1
  • +
  • -

#5 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 - 01:41 PM

The code you provided comes up with the following errors:


Shapeshelp.java:9: cannot find symbol
symbol: class ActionListener
public class Shapeshelp extends JPanel implements ActionListener
^
Shapeshelp.java:51: cannot find symbol
symbol : class ActionEvent
location: class Shapeshelp
public void actionPerformed(ActionEvent evt) {
^
ShapesTest.java:21: incompatible types
found : Shapes
required: Shapeshelp
Shapeshelp panel = new Shapes( choice );
^
Shapeshelp.java:21: cannot find symbol
symbol : constructor Timer(int,Shapeshelp)
location: class javax.swing.Timer
timer = new Timer(200, this); //200ms delay and fire an event to this class. The actionPerformed method will be called
^
Shapeshelp.java:50: method does not override or implement a method from a supertype
@Override
^
5 errors

any idea on how i can fix these? Also thank you for you help so far :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,466
  • Joined: 27-December 08

Re: Java Shapes Sleep question

Posted 06 October 2012 - 01:47 PM

You have to include the appropriate imports from the javax.swing and java.awt.event packages.
Was This Post Helpful? 0
  • +
  • -

#7 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 - 01:52 PM

I've imported the following

import javax.swing.Timer;
import java.awt.event.ActionListner;
import java.awt.event.ActionEvent;

still getting the same errors, here is a repost of the code:
//CS1400-005
//Homework3
//Sierra Escobedo
//Version 3
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.event.ActionListner;
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(200, this); //200ms delay and fire an event to this class. The actionPerformed method will be called
   } // 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) {
	//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 

Was This Post Helpful? 0
  • +
  • -

#8 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1378
  • View blog
  • Posts: 3,038
  • Joined: 05-April 11

Re: Java Shapes Sleep question

Posted 06 October 2012 - 01:54 PM

Sorry I misspelled ActionListener, so of course it could not find the class
That is the down side of using notepad++ :P

//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
   
   // constructor sets the user's choice
   public Shapes( int userChoice )
   {
      choice = userChoice;
	  timer = new Timer(200, this); //200ms delay and fire an event to this class. The actionPerformed method will be called
   } // 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) {
	//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 



Was This Post Helpful? 0
  • +
  • -

#9 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 - 01:59 PM

HaHa i looked and didn't catch the misspelling either. :whistling:

The code now compiles, however when i run the ShapesTest code below:

//CS1400-005
//Homework3
//Sierra Escobedo
//Version 3
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class ShapesTest
{
   public static void main( String[] args )
   {
      // obtain user's choice
      String input = JOptionPane.showInputDialog(
         "Enter 1 to Draw Rectangles\n"  +
         "Enter 2 to Draw Ovals\n"	+
			"Enter 3 to Draw Rounded Rectangles\n");
		
      int choice = Integer.parseInt( input ); // convert input to int
      
      // create the panel with the user's input
      Shapes panel = new Shapes( choice );
      
      JFrame application = new JFrame(); // creates a new JFrame
		application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );   
		application.add( panel ); // add the panel to the frame
      application.setSize( 300, 300 ); // set the desired size
   	application.setLocationRelativeTo (null ); // puts frame in center of screen    
	   application.setVisible( true ); // show the frame
   } // end main
	
}// end class ShapesTest 


It still draws all 10 shapes at the same time... Is there something in the test code that I'm just not seeing that needs to be changed? Or is there still something wrong with the Shapes code itself?
Was This Post Helpful? 0
  • +
  • -

#10 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1378
  • View blog
  • Posts: 3,038
  • Joined: 05-April 11

Re: Java Shapes Sleep question

Posted 06 October 2012 - 02:17 PM

If you ran my code, then there would be shown 0 shapes because shapesShown is 0 and the timer isn't started :)
Was This Post Helpful? 0
  • +
  • -

#11 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 - 02:19 PM

Was just about to post, now it's not showing any shapes at all but you beat me to it. I will try and get the timer started and the shapesShown 0 fixed. Thanks again for all your help. I'll post again once i get the code working or need more help. :bananaman:
Was This Post Helpful? 0
  • +
  • -

#12 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 - 02:46 PM

Okay so, i changed shapes shown to 1, so the display obviously now shows one shape. I also implemented
 	  timer.setInitialDelay(1000); 
	  timer.start(); 


Still nothing is being added to the original shape.

Here's the full code again:
//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 = 1; //Currently shown shapes
   
   // constructor sets the user's choice
   public Shapes( int userChoice )
   {
      choice = userChoice;
	  timer = new Timer(100, 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) {
	//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 



What's missing? I'll continue to on this myself and make sure I understand all the code you added to get this going before turning in my assignment.
Was This Post Helpful? 0
  • +
  • -

#13 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1378
  • View blog
  • Posts: 3,038
  • Joined: 05-April 11

Re: Java Shapes Sleep question

Posted 06 October 2012 - 03:06 PM

If you want information about the timer, then there are lots of tutorials on the swing timer :)

You have to understand that when you start the timer, it will keep firing an event at the delay you told it to
The event will be handled by the actionPerformed method, so when this method is called, you know the timer has fired

In your actionPerformed method you will have to add some logic to show a new shape. I made it call a method for you, which you can use to change the amount of shapes shown.

I made an uncompleted example of how you can solve your problem using a timer, but you can of course replace it with your own. Keep the timer implementation if you want and modify the rest :)

The repaint call will make the JPanel draw itself from scratch again, and you will have to call repaint every time you want to show a new shape on the screen.
Was This Post Helpful? 0
  • +
  • -

#14 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 - 03:58 PM

I'm at a loss here, I can't for the life of me figure out what I need to add to my actionPerformed method or my showshape() method. This is getting to be frustrating :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#15 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1378
  • View blog
  • Posts: 3,038
  • Joined: 05-April 11

Re: Java Shapes Sleep question

Posted 06 October 2012 - 04:07 PM

View Postsescobedo89, on 06 October 2012 - 10:58 PM, said:

I'm at a loss here, I can't for the life of me figure out what I need to add to my actionPerformed method or my showshape() method. This is getting to be frustrating :helpsmilie:


You already tried changing shapesShown to 1 and it showed 1 shape
With the value of 0 it showed 0 shapes
You will want to modify this variable to show the correct number of shapes

Is it the timer you don't know how works? I can only suggest reading tutorials about the swing timer
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2