Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Creating multiple objects with a for loop

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Creating multiple objects with a for loop

    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;
     hue = shade;
     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.drawOval(x+(int)((diameter/2)*0.14), y, (int)(diameter*0.14), (int)(diameter*0.14));//The Head
     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 setColor (Color shade)
     {
     hue = shade;
     }
     
    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);
     }
     }
     }
    Last edited by helloworld922; November 20th, 2011 at 05:26 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Creating multiple objects with a for loop

    For future reference, Java != Javascript.

    A loop does exactly what it sounds like: it loops through a section of code.

    Did you want the people to change locations with each call to the paintComponent method? Or to randomly place the people, then don't move them through the life of the SplatPanel?

    Think about how you would accomplish these two similar (but different) tasks.

    Hints:

    1. Where should your Man Object(s) be constructed?
    2. Should you only have one Man object, or does having multiple make more sense?

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating multiple objects with a for loop

    Yeah, the people should randomly change with each run of the SplatPanel.. I think perhaps I should try constructing the Object within the for loop? But I don't understand how implement the "randomness", if that makes sense

Similar Threads

  1. Creating random objects
    By JackCannon15 in forum Object Oriented Programming
    Replies: 2
    Last Post: November 18th, 2011, 08:50 AM
  2. [SOLVED] Creating new objects per key event
    By sp11k3t3ht3rd in forum Java Theory & Questions
    Replies: 2
    Last Post: October 12th, 2011, 09:46 AM
  3. Creating multiple arraylists with a loop?
    By DLX in forum Collections and Generics
    Replies: 4
    Last Post: December 28th, 2010, 06:58 PM
  4. Loop not creating objects
    By xecure in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2010, 10:48 PM
  5. Replies: 6
    Last Post: May 15th, 2009, 05:06 PM