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.
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
posted
0
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.
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
posted
0
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:
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(); } }