# Creating random objects

• November 17th, 2011, 07:37 PM
JackCannon15
Creating random objects
Hey everyone. So in my Javascript class we have to do create a program that runs a program created by one of our classmates that displays certain objects. This is what I'm working with:

/*
Ryan Coote
4/11/10
Man
*/

import java.util.Random;
import java.awt.*;

public class Man
{
private int diameter, x, y;
private Color hue;
public boolean doubled;

// Constructor
public Man (int size, Color shade, int upperX, int upperY, boolean goodTruth)
{
diameter = size;
x = upperX;
y = upperY;
doubled = goodTruth;
}

public Man (int upperX, int upperY)
{
Random generator = new Random();

diameter = generator.nextInt(50);
hue = new Color (generator.nextInt(255), generator.nextInt(255), generator.nextInt(255));
x = upperX;
y = upperY;
if (generator.nextInt(2) == 1)
{
doubled = true;
}
else
{
doubled = false;
}
}

public void draw (Graphics page)
{
page.setColor(hue);
page.drawLine(x+((int)(diameter/2)), y+((int)(diameter* 0.14)), x+((int)(diameter/2)), y+((int)(diameter*0.7)));//The Body
page.drawLine(x, y+((int)(diameter/2)), x+((int)(diameter/2)), y+((int)(diameter/0.25)));//left arm
page.drawLine(x+diameter, y+((int)(diameter/0.25)), x+((int)(diameter/2)), y+((int)(diameter/2)));//right arm
page.drawLine(x, y+diameter, x+((int)(diameter/2)),y+((int)(diameter*0.7)));//left leg
page.drawLine(x+((int)(diameter/2)),y+((int)(diameter*0.7)),x+diameter, y+diameter);//right leg
page.drawRect(x, y, diameter, diameter);// Square of Preportion

if(doubled == true)
{
page.drawString("Oops, looks like I didn't finish it.", x, y+diameter+10);
}

}

public void setDiameter (int size)
{
diameter = size;
}

{
}

public void setX (int upperX)
{
x = upperX;
}

public void setY (int upperY)
{
y = upperY;
}

public int getDiameter ()
{
return diameter;
}

public Color getColor ()
{
return hue;
}

public int getX ()
{
return x;
}

public int getY ()
{
return y;
}
}

So I need to use a for loop in my driver so that it draws them in random spots. The program above is set up properly, I just need some help with my driver. I don't get how to use the loop. I already have the shell for my driver:

import javax.swing.*;
import java.awt.*;

public class SplatPanel extends JPanel
{
private Man Object;

//-----------------------------------------------------------------
// Constructor: Creates five Pumpkin objects.
//-----------------------------------------------------------------
public SplatPanel()
{
Object = new Man(size, hue, x, y, doubled);

setPreferredSize (new Dimension(300, 200));
setBackground (Color.black);
}

public void paintComponent (Graphics page)
{
super.paintComponent(page);

for (int x = 0;x<10;x++)
{
Object.draw(page);
}
}
}

obviously that doesn't work, I just have no idea how exactly to write the for loop. Again, I need to create 10 random objects. Thanks!
• November 18th, 2011, 06:58 AM
Mr.777
Re: Creating random objects
for loop is similar in java and javascript.
• November 18th, 2011, 07:50 AM
mr.miku
Re: Creating random objects
Hi,
Can you show what exception you are getting in your console or log file.

Core java