Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 30

Thread: Random Shape Generator

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default Random Shape Generator

    I am trying to get random shapes to generate automatically. I was able to get just three shapes to generate but nothing random. When I added the random code now only the frame shows up and no shapes. I also keep getting an error with frame.setVisible(true). It says identifier expected. Thanks for the help. Here is what I have so far:
    Main program
    //Fundamentals of Java, Lab 4, Part 2

    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;


    public class ShapeGen {

    public static void main(String [] args) {

    //Create window and set title
    draw panel = new Draw();
    JFrame frame = new JFrame();
    frame.setTitle("Random Shape Generator");

    //Close the window, add to panel, set size and visibility
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.add( panel );
    frame.setSize( 600, 300);
    frame.setVisible( true );

    }

    }

    This is the draw program to generate random shapes:
    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;


    public class draw extends JPanel {


    public draw(Color backColor) {
    setBackground(backColor);
    }

    //variable to hold the shape
    int shape;

    private static final Random gen = new Random();


    public void paintComponent(Graphics g) {

    super.paintComponent(g);
    //g.setColor(Color.BLACK);
    //g.fillOval(20, 50, 100, 50);
    //g.setColor(Color.BLUE);
    //g.fillOval(150, 25, 100, 100);
    //g.setColor(Color.RED);
    //g.fillRect(275, 25, 100, 100);


    for(int i = 0; i < 4; i++) {

    switch (shape) {

    case '1':
    g.setColor(Color.BLACK);
    g.fillOval(20, 50, 100, 50);
    break;
    case '2':
    g.setColor(Color.BLUE);
    g.fillOval(150, 25, 100, 100);
    break;
    case '3':
    g.setColor(Color.RED);
    g.fillRect(275, 25, 100, 100);
    break;
    }

    }
    }
    public static int chooseShape() {
    int theShape = 1 + gen.nextInt(3);
    return theShape;

    }

    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Random Shape Generator

    I also keep getting an error
    Please copy the full text of the error message and paste it here.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    sjdganc (August 31st, 2014)

  4. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default Re: Random Shape Generator

    Ok, thank you. I have some code commented out in the main program so that I did not lose what I had working at one point.
    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
     
     
    public class ShapeGen {
     
    	public static void main(String [] args) {
     
    		//Create window and set title
    		draw panel = new Draw();
    		JFrame frame = new JFrame();
    		frame.setTitle("Random Shape Generator");
     
    		//Close the window, add to panel, set size and visibility
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.add( panel );
    		frame.setSize( 600, 300);
    		frame.setVisible( true );
     
    		//Container pane = frame.getContentPane();
    		//pane.setLayout(new GridLayout(5, 5));
     
     
    	}
     
     
    	//Color backColor = new Color(red, green, blue);
    	//Panel panel = new Panel();
    	//panel.setBackground(backColor);
    	//pane.add(panel);		
     
     
    	//set window to visible
    	frame.setVisible( true );
     
    	//draw object = new draw();
    	//frame.add(object);
     
    	//object.drawing();	
    	}
     
    }
    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
     
     
    public class draw extends JPanel {
     
     
    	public draw(Color backColor) {
    		setBackground(backColor);
    	}
     
    	//variable to hold the shape
    	int shape;
     
    	private static final Random gen = new Random();
     
     
    	public void paintComponent(Graphics g) {				
     
    		super.paintComponent(g);
    		//g.setColor(Color.BLACK);
    		//g.fillOval(20, 50, 100, 50);
    		//g.setColor(Color.BLUE);
    		//g.fillOval(150, 25, 100, 100);
    		//g.setColor(Color.RED);
    		//g.fillRect(275, 25, 100, 100);
     
     
    		for(int i = 0; i < 4; i++) {
     
    			switch (shape) {
     
    				case '1':
    					g.setColor(Color.BLACK);
    					g.fillOval(20, 50, 100, 50);
    					break;
    				case '2':
    					g.setColor(Color.BLUE);
    					g.fillOval(150, 25, 100, 100);
    					break;
    				case '3':
    					g.setColor(Color.RED);
    					g.fillRect(275, 25, 100, 100);
    					break;
    			}			
     
    		}
    	}
    	public static int chooseShape() {
    		int theShape = 1 + gen.nextInt(3);
    		return theShape;	
     
    	}
    }

    Here are there errors:
    ShapeGen.java:37: error: <identifier> expected
    frame.setVisible( true );
    ShapeGen.java:37: errorL illegal start of type
    frame.setVisible( true );
    ShapeGen.java:45: error: class, interface, or enum expected }

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Random Shape Generator

    ShapeGen.java:37: error: <identifier> expected
    Is the code on line 37 inside of a method?
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    sjdganc (August 31st, 2014)

  7. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default Re: Random Shape Generator

    No, I had it outside of the brackets. Thank you for pointing that out. When I fixed that I realized the third error expecting the } so I fixed that. Now I am getting this error:
    ShapeGen.java:13: error: cannot find symbol
    draw panel = new Draw();
    symbol: class Draw
    location: class ShapeGen

    --- Update ---

    I've played with this code some more and I don't get any errors but I only get a yellow window. No shapes.
    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
     
     
    public class ShapeGen {
     
    	public static void main(String [] args) {
     
    		//Create window and set title
    		JFrame frame = new JFrame();
    		frame.setTitle("Random Shape Generator");
    		frame.setSize(600, 300);
     
    		//Close the window 
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		//Create panel and add to window pane
    		JPanel panel = new JPanel();
    		panel.setBackground(Color.orange);
    		Container pane = frame.getContentPane();
    		pane.add(panel);
     
    		//make window visible
    		frame.setVisible(true);
     
    	}
     
    }
    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
     
     
    public class draw extends JPanel {
     
     
    	public draw(Color backColor) {
    		setBackground(backColor);
    	}
     
    	//variable to hold the shape
    	int shape;
     
    	private static final Random gen = new Random();
     
     
    	public void paintComponent(Graphics g) {				
     
    		super.paintComponent(g);
    		//g.setColor(Color.BLACK);
    		//g.fillOval(20, 50, 100, 50);
    		//g.setColor(Color.BLUE);
    		//g.fillOval(150, 25, 100, 100);
    		//g.setColor(Color.RED);
    		//g.fillRect(275, 25, 100, 100);
     
     
    		for(int i = 0; i < 4; i++) {
     
    			switch (shape) {
     
    				case '1':
    					g.setColor(Color.BLACK);
    					g.fillOval(20, 50, 100, 50);
    					break;
    				case '2':
    					g.setColor(Color.BLUE);
    					g.fillOval(150, 25, 100, 100);
    					break;
    				case '3':
    					g.setColor(Color.RED);
    					g.fillRect(275, 25, 100, 100);
    					break;
    			}			
     
    		}
    	}
    	public static int chooseShape() {
    		int theShape = 1 + gen.nextInt(3);
    		return theShape;	
     
    	}
    }
    Last edited by sjdganc; August 31st, 2014 at 12:49 PM. Reason: Forgot to enclose the code.

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Random Shape Generator

    ShapeGen.java:13: error: cannot find symbol
    draw panel = new Draw();
    symbol: class Draw
    The compiler can not find a class named: Draw. Java is case-sensitive so draw is not the same as Draw
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    sjdganc (August 31st, 2014)

  10. #7
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default Re: Random Shape Generator

    Do I need that in the draw program? I changed it to the below code. I get just a yellow window to display, but no shapes.
    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
     
     
    public class ShapeGen {
     
    	public static void main(String [] args) {
     
    		//Create window and set title
    		JFrame frame = new JFrame();
    		frame.setTitle("Random Shape Generator");
    		frame.setSize(600, 300);
     
    		//Close the window 
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		//Create panel and add to window pane
    		JPanel panel = new JPanel();
    		panel.setBackground(Color.orange);
    		Container pane = frame.getContentPane();
    		pane.add(panel);
     
    		//make window visible
    		frame.setVisible(true);
     
    	}
     
    }

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Random Shape Generator

    the below code. I get just a yellow window to display, but no shapes.
    Where does that code create any shapes?
    If the shapes are drawn in another class, that other class needs to be included.
    If you don't understand my answer, don't ignore it, ask a question.

  12. The Following User Says Thank You to Norm For This Useful Post:

    sjdganc (August 31st, 2014)

  13. #9
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default Re: Random Shape Generator

    This code is supposed to run the shapes in the draw program. I added a draw object to the ShapeGen code but get the below errors:
    ShapeGen.java:29: error: constructor draw in class draw cannot be applied to given types:
    draw object = new draw();
    required: Color
    found: no arguments
    reason: actual and formal argument lists differ in length

    ShapeGen.java:31: error: cannot find symbol
    object.draw();
    symbol: method draw()
    location: variable object of type draw

    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
     
     
    public class ShapeGen {
     
    	public static void main(String [] args) {
     
    		//Create window and set title
    		JFrame frame = new JFrame();
    		frame.setTitle("Random Shape Generator");
    		frame.setSize(600, 300);
     
    		//Close the window 
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		//Create panel and add to window pane
    		JPanel panel = new JPanel();
    		panel.setBackground(Color.orange);
    		Container pane = frame.getContentPane();
    		pane.add(panel);
     
    		//make window visible
    		frame.setVisible(true);
     
    		draw object = new draw();
    		frame.add(object);
    		object.draw();		
     
    	}
     
     
    }

    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
     
     
    public class draw extends JPanel {
     
     
    	public draw(Color backColor) {
    		setBackground(backColor);
    	}
     
    	//variable to hold the shape
    	int shape;
     
    	private static final Random gen = new Random();
     
     
    	public void paintComponent(Graphics g) {				
     
    		super.paintComponent(g);
    		//g.setColor(Color.BLACK);
    		//g.fillOval(20, 50, 100, 50);
    		//g.setColor(Color.BLUE);
    		//g.fillOval(150, 25, 100, 100);
    		//g.setColor(Color.RED);
    		//g.fillRect(275, 25, 100, 100);
     
     
    		for(int i = 0; i < 4; i++) {
     
    			switch (shape) {
     
    				case '1':
    					g.setColor(Color.BLACK);
    					g.fillOval(20, 50, 100, 50);
    					break;
    				case '2':
    					g.setColor(Color.BLUE);
    					g.fillOval(150, 25, 100, 100);
    					break;
    				case '3':
    					g.setColor(Color.RED);
    					g.fillRect(275, 25, 100, 100);
    					break;
    			}			
     
    		}
    	}
    	public static int chooseShape() {
    		int theShape = 1 + gen.nextInt(3);
    		return theShape;	
     
    	}
    }

  14. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Random Shape Generator

    draw object = new draw();
    required: Color
    found: no arguments
    reason: actual and formal argument lists differ in length
    The compiler could not find a no-arg constructor in the Draw class.
    It did find a constructor that takes a Color object.

    To make the compiler happy:
    either add a constructor to Draw that has no arguments
    or change the new statement that calls the Draw's constructor to pass it a Color

    BTW Java coding conventions want class names to start with uppercase letters: Draw vs draw
    If you don't understand my answer, don't ignore it, ask a question.

  15. #11
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default Re: Random Shape Generator

    I'm not sure what you mean by constructor. In the draw program do I change it to this
    public draw() {
    }

  16. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Random Shape Generator

    That code looks like a no-arg constructor. Did you try it? What happened?

    Take a look at the tutorial: http://docs.oracle.com/javase/tutori...structors.html
    If you don't understand my answer, don't ignore it, ask a question.

  17. The Following User Says Thank You to Norm For This Useful Post:

    sjdganc (August 31st, 2014)

  18. #13
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default Re: Random Shape Generator

    I tried it and see from the tutorial you provided that it is a no-arg constructor, so I'm not telling it what I want to do, right? But in the ShapeGen program I get this error:
    ShapeGen.java:32: error: cannot find symbol
    object.draw();

    symbol: method draw()
    location: variable object of type draw
    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
     
     
    public class ShapeGen {
     
    	public static void main(String [] args) {
     
     
    		//Create window and set title
    		JFrame frame = new JFrame();
    		frame.setTitle("Random Shape Generator");
    		frame.setSize(600, 300);
     
    		//Close the window 
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		//Create panel and add to window pane
    		JPanel panel = new JPanel();
    		panel.setBackground(Color.orange);
    		Container pane = frame.getContentPane();
    		pane.add(panel);
     
    		//make window visible
    		frame.setVisible(true);
     
    		draw object = new draw();
    		frame.add(object);
    		object.draw();		
     
    	}
     
     
    }

  19. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Random Shape Generator

    ShapeGen.java:32: error: cannot find symbol
    object.draw();

    symbol: method draw()
    location: variable object of type draw
    Where is the draw() method defined in the draw class? The compiler can not find it.

    That is an example of why the naming conventions are helpful.
    There is draw a class and draw a method.
    It should be Draw the class and draw the method
    If you don't understand my answer, don't ignore it, ask a question.

  20. #15
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default Re: Random Shape Generator

    I'm trying to get this but I'm not quite understanding. So I changed the name of the draw program to Draw and left the constructor as public draw. I get this error on the Draw program
    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
     
     
    public class Draw extends JPanel {
     
     
    	public draw() {
     
    	}
     
    	//variable to hold the shape
    	int shape;
     
    	private static final Random gen = new Random();
     
     
    	public void paintComponent(Graphics g) {				
     
    		super.paintComponent(g);
    		//g.setColor(Color.BLACK);
    		//g.fillOval(20, 50, 100, 50);
    		//g.setColor(Color.BLUE);
    		//g.fillOval(150, 25, 100, 100);
    		//g.setColor(Color.RED);
    		//g.fillRect(275, 25, 100, 100);
     
     
    		for(int i = 0; i < 4; i++) {
     
    			switch (shape) {
     
    				case '1':
    					g.setColor(Color.BLACK);
    					g.fillOval(20, 50, 100, 50);
    					break;
    				case '2':
    					g.setColor(Color.BLUE);
    					g.fillOval(150, 25, 100, 100);
    					break;
    				case '3':
    					g.setColor(Color.RED);
    					g.fillRect(275, 25, 100, 100);
    					break;
    			}			
     
    		}
    	}
    	public static int chooseShape() {
    		int theShape = 1 + gen.nextInt(3);
    		return theShape;	
     
    	}
    }

    The error is:
    Draw.java:9: error: invalid method declaration; return type required
    public draw() {

  21. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Random Shape Generator

    public draw() {
    The spelling of the constructor's name must match the class's name.

    What happened to the constructor that takes a color? A class can have many different constructors.
    If you don't understand my answer, don't ignore it, ask a question.

  22. The Following User Says Thank You to Norm For This Useful Post:

    sjdganc (August 31st, 2014)

  23. #17
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default Re: Random Shape Generator

    Ok, I see that. Thank you. So the Draw program new code compiles without error but I get a new error in the ShapeGen program.
    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
     
     
    public class Draw extends JPanel {
     
     
    	public Draw(Color backColor) {
    		setBackground(backColor);
    	}
     
     
     
    	//variable to hold the shape
    	int shape;
     
    	private static final Random gen = new Random();
     
     
    	public void paintComponent(Graphics g) {				
     
    		super.paintComponent(g);
    		//g.setColor(Color.BLACK);
    		//g.fillOval(20, 50, 100, 50);
    		//g.setColor(Color.BLUE);
    		//g.fillOval(150, 25, 100, 100);
    		//g.setColor(Color.RED);
    		//g.fillRect(275, 25, 100, 100);
     
     
    		for(int i = 0; i < 4; i++) {
     
    			switch (shape) {
     
    				case '1':
    					g.setColor(Color.BLACK);
    					g.fillOval(20, 50, 100, 50);
    					break;
    				case '2':
    					g.setColor(Color.BLUE);
    					g.fillOval(150, 25, 100, 100);
    					break;
    				case '3':
    					g.setColor(Color.RED);
    					g.fillRect(275, 25, 100, 100);
    					break;
    			}			
     
    		}
    	}
    	public static int chooseShape() {
    		int theShape = 1 + gen.nextInt(3);
    		return theShape;	
     
    	}
    }
    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
     
     
    public class ShapeGen {
     
    	public static void main(String [] args) {
     
     
    		//Create window and set title
    		JFrame frame = new JFrame();
    		frame.setTitle("Random Shape Generator");
    		frame.setSize(600, 300);
     
    		//Close the window 
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		//Create panel and add to window pane
    		JPanel panel = new JPanel();
    		panel.setBackground(Color.orange);
    		Container pane = frame.getContentPane();
    		pane.add(panel);
     
    		//make window visible
    		frame.setVisible(true);
     
    		Draw object = new Draw();
    		frame.add(object);
    		object.Draw();		
     
    	}
     
     
    }
    I thought what I have at the end is drawing the object but these are the errors:
    ShapeGen.java:30: error: cannot find symbol
    Draw object = new Draw();
    symbol: class Draw
    location: class ShapeGen
    ShapeGen.java:30: error: cannot find symbol
    Draw object = new Draw();
    symbol: class Draw
    location: class ShapeGen

    Do I have the last 3 lines of code in the ShapeGen in the wrong program?

  24. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Random Shape Generator

    ShapeGen.java:30: error: cannot find symbol
    Draw object = new Draw();
    symbol: class Draw
    Where is the no-arg constructor for the Draw class?
    If you don't understand my answer, don't ignore it, ask a question.

  25. The Following User Says Thank You to Norm For This Useful Post:

    sjdganc (August 31st, 2014)

  26. #19
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default Re: Random Shape Generator

    I put that back in in the Draw class and it compiles with no errors. But still get the same above errors:
    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
     
     
    public class Draw extends JPanel {
     
     
    	public Draw() {
    	}
     
     
    	public Draw(Color backColor) {
    		setBackground(backColor);
    	}
     
     
    	//variable to hold the shape
    	int shape;
     
    	private static final Random gen = new Random();
     
     
    	public void paintComponent(Graphics g) {				
     
    		super.paintComponent(g);
    		//g.setColor(Color.BLACK);
    		//g.fillOval(20, 50, 100, 50);
    		//g.setColor(Color.BLUE);
    		//g.fillOval(150, 25, 100, 100);
    		//g.setColor(Color.RED);
    		//g.fillRect(275, 25, 100, 100);
     
     
    		for(int i = 0; i < 4; i++) {
     
    			switch (shape) {
     
    				case '1':
    					g.setColor(Color.BLACK);
    					g.fillOval(20, 50, 100, 50);
    					break;
    				case '2':
    					g.setColor(Color.BLUE);
    					g.fillOval(150, 25, 100, 100);
    					break;
    				case '3':
    					g.setColor(Color.RED);
    					g.fillRect(275, 25, 100, 100);
    					break;
    			}			
     
    		}
    	}
    	public static int chooseShape() {
    		int theShape = 1 + gen.nextInt(3);
    		return theShape;	
     
    	}
    }

  27. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Random Shape Generator

    Please copy and paste the current errors. There have been lots of them posted and I'm not sure which youy are talking about.

    ALSO: There should be a default: case in the switch statement that prints a message if none of the other case statements were true.
    If you don't understand my answer, don't ignore it, ask a question.

  28. The Following User Says Thank You to Norm For This Useful Post:

    sjdganc (August 31st, 2014)

  29. #21
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default Re: Random Shape Generator

    When I comment these three lines out I get the orange box but still no shapes. In other examples I have seen where the for loop should be in the main method. Is this part of my problem or am I jumping ahead?
    //Draw object = new Draw();
    //frame.add(object);
    //object.Draw();

    --- Update ---

    Here are the current errors:
    ShapeGen.java:30: error: cannot find symbol
    Draw object = new Draw();
    symbol: class Draw
    location: class ShapeGen

    ShapeGen.java:30: error: cannot find symbol
    Draw object = new Draw();
    symbol: class Draw
    location: class ShapeGen

    I am not that familiar with switch...case. If I can do it in a for loop instead can you suggest how?

  30. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Random Shape Generator

    ShapeGen.java:30: error: cannot find symbol
    Draw object = new Draw();
    symbol: class Draw
    Does the Draw class have a no-arg constructor?

    I am not that familiar with switch...case.
    See the tutorial: http://docs.oracle.com/javase/tutori...ts/switch.html

    can do it
    Please describe what "it" is supposed to mean.
    If you don't understand my answer, don't ignore it, ask a question.

  31. The Following User Says Thank You to Norm For This Useful Post:

    sjdganc (August 31st, 2014)

  32. #23
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default Re: Random Shape Generator

    I thought
    public Draw() {
    }
    is the no-arg constructor. But I see I don't have it doing anything, so I added:
    public Draw() {
        shape = 4;
    }
    hoping this would create 4 shapes. I declared the variable int shape; above the constructor.

    By "it" I was referring to create the shapes by using a for loop in the main program instead of in the draw program.

  33. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Random Shape Generator

    Yes, that looks like a no-arg constructor.

    create the shapes by using a for loop in the main program instead of in the draw program.
    The drawing of the shapes is done in the paintComponent() method. I'm not sure what you mean by "create" the shapes. The switch statement in the paintComponent() method will draw one of 4 shapes depending on the switch value.
    I don't know what the for loop is for. I don't see what is being changed inside the loop as the loop goes around. Why have a loop if nothing changes inside of the loop?

    One problem I just saw is the adding of the Draw object AFTER the frame is setVisible. The new addition won't be shown until something makes the frame be drawn again. Move setVisible() call to AFTER everything to be show has been added to the frame.
    If you don't understand my answer, don't ignore it, ask a question.

  34. The Following User Says Thank You to Norm For This Useful Post:

    sjdganc (August 31st, 2014)

  35. #25
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default Re: Random Shape Generator

    I moved setVisible() to after everything as you suggested but still get the same errors as most recently stated above. I also see your points about the for loop so I commented those out for now and made some changes to the Draw program:
    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
     
     
    public class Draw extends JPanel {
     
    	int shape;
     
    	public Draw() {
     
    		shape = 4;
     
    	}
     
     
    	public Draw(Color backColor) {
    		setBackground(backColor);
    	}
     
     
    	//variable to hold the shape
    	//int shape;
     
    	private static final Random gen = new Random();
     
     
    	public void paintComponent(Graphics g) {				
     
    		super.paintComponent(g);
    		//g.setColor(Color.BLACK);
    		//g.fillOval(20, 50, 100, 50);
    		//g.setColor(Color.BLUE);
    		//g.fillOval(150, 25, 100, 100);
    		//g.setColor(Color.RED);
    		//g.fillRect(275, 25, 100, 100);
     
     
    		//for(int i = 0; i < 4; i++) {
     
    		shape = chooseShape();   //added this
    		switch (shape) {
     
    			case '1':
    				g.setColor(Color.BLACK);
    				g.fillOval(20, 50, 100, 50);
    				break;
    			case '2':
    				g.setColor(Color.BLUE);
    				g.fillOval(150, 25, 100, 100);
    				break;
    			case '3':
    				g.setColor(Color.RED);
    				g.fillRect(275, 25, 100, 100);
    				break;
    			}			
     
    		//}
    	}
    	public static int chooseShape() {
    		int theShape = 1 + gen.nextInt(3);
    		return theShape;	
     
    	}
    }

Page 1 of 2 12 LastLast

Similar Threads

  1. Random Number Generator
    By Rugby_Thompson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 5th, 2013, 12:58 AM
  2. Random Number Generator Always gives 0
    By tlckl3m3elmo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 11th, 2012, 03:09 PM
  3. Random math problem generator?
    By CamCompetes in forum Member Introductions
    Replies: 1
    Last Post: March 9th, 2012, 02:42 PM
  4. random number generator
    By java3 in forum Loops & Control Statements
    Replies: 4
    Last Post: February 21st, 2011, 12:00 PM
  5. [SOLVED] Random number method implementation
    By big_c in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2009, 01:10 PM