Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Random Generation of Shapes

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9103
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
 
And then we all jump out and yell "surprise! we got you this tiny ad!"
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic