• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Random Generation of Shapes

 
Tomas Elpacio
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been toying around with the Shapes code in the code barn, and have run into a bit of a snag. In the ShowShapes class I am trying to generate and use random numbers for at least one of the Box objects and have been getting errors. I am using Math.random to generate the random double and then using intValue to downcast it to an integer. Any guidance someone can provide would be most helpful. Thanks.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the ShowShapes class I am trying to generate and use random numbers for at least one of the Box objects and have been getting errors.
What kind of errors? Can you be a little more specific?

 
Tomas Elpacio
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marilyn,
I apologize for not having better specifics for you as I have gone through multiple -- albeit unfruitful -- iterations, since I posted my last message. I was receiving an error message about not being able to "de-reference" a double. The crux of my problem is that I am trying to find a way to randomize any of the Box, Circle, or Poly objects in the Shapes sample code, and cannot do it. I was hoping to make a sort of kaleidoscope with random Shape patterns --- kind of like a poor man's version of a Pink Floyd laser light show, but to no avail.
 
Nura Horne
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried using round?
int myInt
double myDouble = 5.6565
myInt = Math.round(myDouble)
// Should result in myInt = 6
 
Chad McGowan
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should use Random.nextInt() instead of Math.random(). This way you won't have to cast from double to int.
Chad
 
Tomas Elpacio
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your responses Arun and Chad. I tried your suggestions but could not get it to work. Below is the code I currently have, along with the Shape and Box class from the Shapes example in the Code Barn. In the line where I create a new Box object, I currently draw the shapes by incrementing the variable 'i'. I'd like to be able to insert random values in here for the boxes' x,y,wide,high variables and run through 100 iterations of drawing random boxes. Seems like it should be easy to do, but my application of Java has left a bit to be desired thus far. Here is the code:

Any help would be much appreciated.
 
Sajee Joseph
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
I've modified ur code as below and it works fine for me. Check it out.
import java.awt.*;
import java.util.*;
public class BoxStart extends Frame
{
BoxStart()
{
setBounds( 200 ,150 , 400 , 250 );
setVisible( true );
}
public void paint( Graphics g )
{
Random r = new Random();
for( int i = 0 ; i < 100 ; i++ )
{
int j = r.nextInt(100);
System.out.println("j:"+j);
new Box(j+20,j+10,j+20,j+35,Color.cyan).draw( g );
}
}
public static void main (String[]args)
{
new BoxStart();
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic