aspose file tools*
The moose likes Beginning Java and the fly likes Random Generation of Shapes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Random Generation of Shapes" Watch "Random Generation of Shapes" New topic
Author

Random Generation of Shapes

Tomas Elpacio
Greenhorn

Joined: Jul 26, 2001
Posts: 5
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

Joined: Jul 22, 2000
Posts: 9046
    
  10
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?


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Tomas Elpacio
Greenhorn

Joined: Jul 26, 2001
Posts: 5
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

Joined: Jul 26, 2001
Posts: 40
Have you tried using round?
int myInt
double myDouble = 5.6565
myInt = Math.round(myDouble)
// Should result in myInt = 6
Chad McGowan
Ranch Hand

Joined: May 10, 2001
Posts: 265
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

Joined: Jul 26, 2001
Posts: 5
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

Joined: Jan 17, 2001
Posts: 200
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();
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Random Generation of Shapes