Fill function in an array of objects

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.

1 Like

In you Ball class add in :

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));
2 Likes

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

1 Like

Thank you. Much appreciated.

2 Likes