Random Shape Generator For Drawing
#1
- New D.I.C Head
Reputation: 0
- Posts: 40
- Joined: 22-March 11
Help with Random Shape Generator
Posted 04 April 2011 - 09:50 PM
Nevermind...saw the question posted in another thread...sorry.
This post has been edited by CorruptionInc: 04 April 2011 - 10:03 PM
Is This A Good Question/Topic? 0
#2 CorruptionInc
- New D.I.C Head
Reputation: 0
- Posts: 40
- Joined: 22-March 11
Re: Help with Random Shape Generator
Posted 04 April 2011 - 10:54 PM
OK, it didn't totally answer my question... Assignment: Write a method randomShape that randomly generates objects implementing the Shape interface: some mixture of rectangles, ellipses, and lines, with random positions. Call it 10 times and draw all of them. So I have the program working sorta, but it draws the shapes in exactly the same place on the screen, but I need it to be random, and I have no clue how to do that. Can anyone point me in the right direction?
import javax.swing.JFrame; public class RandomShapeViewer { public static void main(String[] args) { JFrame frame = new JFrame(); final int FRAME_WIDTH = 300; final int FRAME_HEIGHT = 400; frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setTitle("RandomShapeViewer"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); RandomShapesComponent component = new RandomShapesComponent(); frame.add(component); frame.setVisible(true); } } import javax.swing.JComponent; import java.awt.Graphics; import java.awt.Graphics2D; public class RandomShapesComponent extends JComponent { public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; RandomShapeGenerator r = new RandomShapeGenerator(getWidth(), getHeight()); for (int i = 1; i <= 10; i++) g2.draw(r.randomShape()); } } import java.awt.Shape; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.awt.geom.Rectangle2D; import java.util.Random; public class RandomShapeGenerator { int width, height; Random ran = new Random(); public RandomShapeGenerator(int i, int j) { int width = i; int height = j; } public Shape randomShape() { switch(ran.nextInt(10)) { default: case 0: return new Ellipse2D.Double(10, 20, 10, 20); case 1: return new Line2D.Double(100, 100, 150, 150); case 2: return new Rectangle2D.Double(30,40,30,40); } } }
#3 pbl
Reputation: 8381
- Posts: 31,956
- Joined: 06-March 08
Re: Help with Random Shape Generator
Posted 05 April 2011 - 12:20 AM
You will have to put Random values as paramaters to your Shape constructor
switch(ran.nextInt(10)) { default: case 0: return new Ellipse2D.Double(10, 20, 10, 20); <--- here case 1: return new Line2D.Double(100, 100, 150, 150); <--- here case 2: return new Rectangle2D.Double(30,40,30,40); <--- here
#4 yaykittyeee
- D.I.C Head
Reputation: 2
- Posts: 59
- Joined: 16-September 10
Re: Help with Random Shape Generator
Posted 05 April 2011 - 12:26 AM
Instead of passing your x and y through the RandomShapeGenerator object and defining it in the constructor of RandomShapeGenerator you could set your x and y in the randomshape() method and use the random number generator ran.nextInt() to get your x and y. When you define the object RandomShapeGenerator you only have one instance of it(I'm not sure that I'm saying this right), so no matter how many times you call r.randomshape() you are always going to get the same x and y. I think that is one of the reasons you would get the same position. Also, when you call the shapes such as Ellipse2D.Double(10, 20, 10, 20); make sure to use your variables. If you just put 10 and 20 in the x,y positions the ellipse will always show up at 10 and 20.
#5 CorruptionInc
- New D.I.C Head
Reputation: 0
- Posts: 40
- Joined: 22-March 11
Re: Help with Random Shape Generator
Posted 05 April 2011 - 02:20 AM
yaykittyeee, on 05 April 2011 - 12:26 AM, said:
Instead of passing your x and y through the RandomShapeGenerator object and defining it in the constructor of RandomShapeGenerator you could set your x and y in the randomshape() method and use the random number generator ran.nextInt() to get your x and y. When you define the object RandomShapeGenerator you only have one instance of it(I'm not sure that I'm saying this right), so no matter how many times you call r.randomshape() you are always going to get the same x and y. I think that is one of the reasons you would get the same position. Also, when you call the shapes such as Ellipse2D.Double(10, 20, 10, 20); make sure to use your variables. If you just put 10 and 20 in the x,y positions the ellipse will always show up at 10 and 20.
I semi-understand what you are saying. I just don't understand how to use random number generator in my parameters... I'm horrible with variables. Plus, if I randomly generated all my parameters, I'd have some weird looking shapes I'm sure. This class is kicking my butt.
#6 macosxnerd101
Reputation: 12800
- Posts: 45,992
- Joined: 27-December 08
Re: Help with Random Shape Generator
Posted 05 April 2011 - 04:24 AM
As an example:
int x = rand.nextInt(); int y = rand.nextInt(); int width = rand.nextInt(); int height = rand.nextInt(); Ellipse2D.Double doub = new Ellipse2D.Double(x,y,width,height);
This post has been edited by macosxnerd101: 05 April 2011 - 09:16 AM
Reason for edit:: Fixed variable name
#7 CorruptionInc
- New D.I.C Head
Reputation: 0
- Posts: 40
- Joined: 22-March 11
Re: Help with Random Shape Generator
Posted 05 April 2011 - 09:13 AM
macosxnerd101, on 05 April 2011 - 04:24 AM, said:
As an example:
int x = rand.nextInt(); int y = rand.nextInt(); int width = rand.nextInt(); int height = rand.nextInt(); Ellipse2D.Double doub = new Ellipse2D.Double(x,y,width,height);
Says I can't assign an Ellipse2D as a variable? I can't get this to work. Admittedly, I don't know what the heck I'm doing.
public Shape randomShape() { int x = rand.nextInt(); int y = rand.nextInt(); int width = rand.nextInt(); int height = rand.nextInt(); switch(ran.nextInt(10)) { default: case 0: return new Ellipse2D.Double(x,y,width,height); case 1: return new Line2D.Double(x,y,width,height); case 2: return new Rectangle2D.Double(x,y,width,height); } }
#8 macosxnerd101
Reputation: 12800
- Posts: 45,992
- Joined: 27-December 08
Re: Help with Random Shape Generator
Posted 05 April 2011 - 09:18 AM
Post your exact error message from your compiler.
#9 CorruptionInc
- New D.I.C Head
Reputation: 0
- Posts: 40
- Joined: 22-March 11
Re: Help with Random Shape Generator
Posted 05 April 2011 - 09:22 AM
macosxnerd101, on 05 April 2011 - 09:18 AM, said:
Post your exact error message from your compiler.
No error, just doesn't display any shapes.
#10 macosxnerd101
Reputation: 12800
- Posts: 45,992
- Joined: 27-December 08
Re: Help with Random Shape Generator
Posted 05 April 2011 - 09:24 AM
You have cases 0, 1, and 2 in your switch block, but your random numbers can go from 0-9, based on this switch(ran.nextInt(10)). That doesn't make sense to me, unless you had a default statement in your switch block.
#11 CorruptionInc
- New D.I.C Head
Reputation: 0
- Posts: 40
- Joined: 22-March 11
Re: Help with Random Shape Generator
Posted 05 April 2011 - 09:27 AM
macosxnerd101, on 05 April 2011 - 09:24 AM, said:
You have cases 0, 1, and 2 in your switch block, but your random numbers can go from 0-9, based on this switch(ran.nextInt(10)). That doesn't make sense to me, unless you had a default statement in your switch block.
That's a different random... that random specifies how many shapes I can create (I think). There's ran and rand. Don't worry, it doesn't make sense to me either...
#12 macosxnerd101
Reputation: 12800
- Posts: 45,992
- Joined: 27-December 08
Re: Help with Random Shape Generator
Posted 05 April 2011 - 09:34 AM
That's in the switch block, which tells it which case to go to. Like if statements.
#13 CorruptionInc
- New D.I.C Head
Reputation: 0
- Posts: 40
- Joined: 22-March 11
Re: Help with Random Shape Generator
Posted 05 April 2011 - 09:48 AM
macosxnerd101, on 05 April 2011 - 09:34 AM, said:
That's in the switch block, which tells it which case to go to. Like if statements.
Well, I'm totally lost on this one.
#14 macosxnerd101
Reputation: 12800
- Posts: 45,992
- Joined: 27-December 08
Re: Help with Random Shape Generator
Posted 05 April 2011 - 10:11 AM
What happens if the random.nextInt(9) method returns 4? You have case 0. Since 4 != 0, case 0 will be skipped. Cases 1 and 2 will be skipped for the same reason. Since you do not account for case 4, nothing will occur. You have to either account for more cases, or reduce the range of randoms nextInt() can generate from.
Also a switch block can be written equivently w/if statements. As an example:
switch(x){ case 0: //code break; case 1: //code break; case 2: //code break; default: break; } //can be written as if(x == 0){} else if(x == 1){} else if(x == 2){} else{}
#15 yaykittyeee
- D.I.C Head
Reputation: 2
- Posts: 59
- Joined: 16-September 10
Re: Help with Random Shape Generator
Posted 05 April 2011 - 03:39 PM
CorruptionInc, on 05 April 2011 - 02:20 AM, said:
yaykittyeee, on 05 April 2011 - 12:26 AM, said:
Instead of passing your x and y through the RandomShapeGenerator object and defining it in the constructor of RandomShapeGenerator you could set your x and y in the randomshape() method and use the random number generator ran.nextInt() to get your x and y. When you define the object RandomShapeGenerator you only have one instance of it(I'm not sure that I'm saying this right), so no matter how many times you call r.randomshape() you are always going to get the same x and y. I think that is one of the reasons you would get the same position. Also, when you call the shapes such as Ellipse2D.Double(10, 20, 10, 20); make sure to use your variables. If you just put 10 and 20 in the x,y positions the ellipse will always show up at 10 and 20.
I semi-understand what you are saying. I just don't understand how to use random number generator in my parameters... I'm horrible with variables. Plus, if I randomly generated all my parameters, I'd have some weird looking shapes I'm sure. This class is kicking my butt.
You don't generate all the parameters with variables, just x and y. So Ellipse2D.Double(x, y, width, height); are the parameters. You only need to generate x and y. Make the width and height constants(like 10 and 20) so it will come out the same size every time.
for (int i = 1; i <= 10; i++) g2.draw(r.randomShape());
the code above is what makes it do ten shapes. In the switch block, ran.nextInt(3) is what you need to make it generate numbers 0-2 so that you will get case 0-2.
This post has been edited by yaykittyeee: 05 April 2011 - 03:42 PM
Random Shape Generator For Drawing
Source: https://www.dreamincode.net/forums/topic/226076-help-with-random-shape-generator/
Posted by: onealyouds1985.blogspot.com
0 Response to "Random Shape Generator For Drawing"
Post a Comment