# Fill function in an array of objects

**URL:** <https://discourse.processing.org/t/fill-function-in-an-array-of-objects/15925>\
**Category:** Coding Questions\
**Created:** [November 28, 2019, 9:38pm UTC](https://discourse.processing.org/t/fill-function-in-an-array-of-objects/15925 "2019-11-28T21:38:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kasdal](https://avatars.discourse-cdn.com/v4/letter/k/839c29/32.png) [@Kasdal](https://discourse.processing.org/u/Kasdal)\
**Post date:** [November 28, 2019, 9:38pm UTC](https://discourse.processing.org/t/fill-function-in-an-array-of-objects/15925/1 "2019-11-28T21:38:17Z")

</div>

```auto
import javax.swing.*; 

Ball [] balls;

PVector wind = new PVector(0.03, 3);
PVector gravity = new PVector(0.1, 0.1);

void setup() {
  size(800, 600);
  smooth();
  background(255);

  int ballCount = Integer.parseInt(
    JOptionPane.showInputDialog("Enter a number of balls between 1 and 200", "30" ));

  String str = JOptionPane.showInputDialog("Who will be using this program?:");
  JOptionPane.showMessageDialog(null, "The name you entered is:\n\n " + str
    + "\n\nLength: " + str.length() + " characters"
    + "\nCapital: " + str.toUpperCase());

  // limit to sane number of balls
  if ((ballCount<0) || (ballCount>200)) {
    ballCount = 30;
  }

  balls = new Ball[ballCount];

  for (int i = 0; i < balls.length; i++) {
    balls[i] = new Ball(random(0.1, 5), 0, 0);
  }
}

//Draw method to display the ball on screen.

void draw() {

  //gray fill function with alpha mask

  fill(190, 30);
  rect(0, 0, width, height);

  for (int i = 0; i < balls.length; i++) {
    balls[i].applyForce(wind);
    balls[i].applyForce(gravity);

    balls[i].update();
    balls[i].display();
    balls[i].wallBounce();
  }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public class Ball {

  //Private fields.
  private PVector location;
  private PVector velocity;
  private PVector acceleration;
  private float mass;
  private float diameter;
  
  
// Ball parameters 
  public Ball(float m, float x, float y) {
    mass = m;
    location = new PVector(x, y);
    velocity = new PVector(0, 0);
    acceleration = new PVector(0, 0);
    diameter = 20;
  }

  public void applyForce(PVector force) {
    PVector f = PVector.div(force, mass);

    acceleration.add(f);
  }

  public void update() {
    velocity.add(acceleration);
    location.add(velocity);
    acceleration.mult(0);
  }
  
  //Method to draw a ball on the canvas.
  public void display() {
    stroke(0);
    strokeWeight(5);
    fill(0, 255, 150);
    strokeWeight(1.3);
    ellipse(location.x, location.y, mass*diameter, mass*diameter);
  }
  
  // Method to update due to ball-wall collisions
  public void wallBounce() {
    
    // Accessing individual components of PVector
    if (location.x > width) {
      location.x = width;
      velocity.x *= -1;
    } else if (location.x < 0) {
      velocity.x *= -1;
      location.x = 0;
    }

    if (location.y > height) {
      velocity.y *= -1;
      location.y = height;
    }
  }
  

  public PVector location()
  {
    return location;
  }

  public PVector velocity()
  {
    return velocity;
  }

  public PVector acceleration()
  {
    return acceleration;
  }

  public float mass()
  {
    return mass;
  }

  public float getDiameter()
  {
    return diameter;
  } 

  public void setDiameter(float diameter)
  {

    if (diameter >= 0) {
      this.diameter = 11;
    } else {
 
      this.diameter = 20;
    }
  }
}

```

Hello guys. First let me warn you that i am a total noob.  
It took me ages to get to here but i got a working animation with the use of array.  
I would need some help with coloring each ball in different color.  
I did try random fill in RGB but that just creates a flashing disco.  
What can i do to make this?

Thank you.

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [November 28, 2019, 10:05pm UTC](https://discourse.processing.org/t/fill-function-in-an-array-of-objects/15925/2 "2019-11-28T22:05:40Z")

</div>

In you Ball class add in :

```auto
public class Ball {
   color ballFill;

   Ball (color bF) {
      ballFill = bF;
   }

   void display() {
      fill(ballFill);
   }
}

// and when creating a Ball
balls[i] = new Ball(random(0.1, 5), 0, 0, color(random(255), random(255), random(255));

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 29, 2019, 12:02am UTC](https://discourse.processing.org/t/fill-function-in-an-array-of-objects/15925/3 "2019-11-29T00:02:40Z")

</div>

> [@Kasdal](#):
>
> just creates a flashing disco.

Yeah, use random but store it in the constructor so it doesn’t change.

---

<div class="post-metadata">

**Author:** ![Kasdal](https://avatars.discourse-cdn.com/v4/letter/k/839c29/32.png) [@Kasdal](https://discourse.processing.org/u/Kasdal)\
**Post date:** [November 29, 2019, 9:19pm UTC](https://discourse.processing.org/t/fill-function-in-an-array-of-objects/15925/4 "2019-11-29T21:19:14Z")

</div>

Thank you. Much appreciated.
