How do I assign 2 specific colors to an array of objects

#1
  • I would like to assign 2 colors to this array.

  • However, I do not want each color confined to an area within the sketch (as it appears currently).

  • How do rewrite so that half of the jellybeans are dark red and half are bright red and the 2 colors intermingle across the entire screen.

Any suggestions greatly appreciated!

Jellybean [] jellybeans = new Jellybean [100];

void setup() {
  size(640, 360);

  for (int i = 0; i<jellybeans.length; i++) {
    jellybeans [i] = new Jellybean (random(width), random(height), random(20, 25), random(.15, 2.05), random(.75, 1.95));
  }
}
void draw() {

  background(255);

  for (int i = 0; i < jellybeans.length; i++) {

    jellybeans[i].display();
    jellybeans[i].spin();
    jellybeans[i].move();
  }
}
class Jellybean {

  float x;
  float y;
  float r;
  float angle = random(0.95);
  float angleSpeed = random(.01, .2);
  float xSpeed;
  float ySpeed;

  color red = #E80F07;
  color dk_red = #A71C18;
  
Jellybean(float tempX, float tempY, float tempR, float tempXspeed, float tempYspeed) {
    x = tempX;
    y = tempY;
    r = tempR;
    xSpeed = tempXspeed;
    ySpeed = tempYspeed;
    
  }
void display() {
    noStroke();
    smooth();

    if (x < width/2){
      fill (dk_red);
    }else{
      fill (red);
    }
    pushMatrix();
    translate(x, y);
    rotate(angle);
    ellipse(0, 0, r*2, r);
    popMatrix();
  }
  void spin() {

    angle = angle + angleSpeed;
    angle %= TWO_PI;
  }

  void move() {

    x = x + xSpeed;
    y = y + ySpeed;

    if ((x > width) || (x < 0)) {
      xSpeed = xSpeed * -1;
    }
    if ((y > height) || (y<0)) {
      ySpeed = ySpeed * -1;
    }
  }
}
1 Like
#2

Hello,
This seems like a cool project.
If by intermingle you mean 50 red and 50 dark-red randomly positioned across the entire screen.
you could use something similar to having a while loop using a counter till it reaches 50 and inside store a random value ranging from 0-100,There can be duplicate values so only increment the counter value if the color of the object is not red(assuming you choose to fill red first).this way you can be sure that you will have exactly 50 jelly beans which are red and are randomly located , filling the remaining 50 is easy, you just need to check if the object has a color already.This might help you.

1 Like
#3

Thank you @Capt.Sloth! I will try this. :wink:

#4

Hello everyone!
I tried implementing the while loop suggestion from @Capt.Sloth above by placing it in the display() function in the class. But this didn’t work. Or rather I couldn’t figure out how to get this to work.

So next I am trying to divide and initialize my jellybean array into 2 halves. Then adding a color variable to the constructor for each half. This still is not working. But am I on the right train of thought?


Jellybean [] jellybeans = new Jellybean [100];


void setup() {
  size(640, 360);

  for (int i = 0; i < jellybeans.length/2; i++) {
    jellybeans [i] = new Jellybean (random(width), random(height), random(20, 25), random(.15, 2.05), random(.75, 1.95), #E80F07);
  }

  for (int i = 50; i >= jellybeans.length/2 && i < jellybeans.length; i++) {
   jellybeans [i] = new Jellybean (random(width), random(height), random(20, 25), random(.15, 2.05), random(.75, 1.95), #A71C18);
  }
}

void draw() {

  background(255);

  for (int i = 0; i < jellybeans.length/2; i++) {

    jellybeans[i].display();
    jellybeans[i].spin();
    jellybeans[i].move();
  }

  for (int i = 50; i >= jellybeans.length/2 && i < jellybeans.length; i++) {

   jellybeans[i].display();
   jellybeans[i].spin();
   jellybeans[i].move();
  }
}
class Jellybean {

  float x;
  float y;
  float r;
  float angle = random(0.95);
  float angleSpeed = random(.01, .2);
  float xSpeed;
  float ySpeed;

  color a;
  


  Jellybean(float tempX, float tempY, float tempR, float tempXspeed, float tempYspeed, color tempA) {
    x = tempX;
    y = tempY;
    r = tempR;
    xSpeed = tempXspeed;
    ySpeed = tempYspeed;
    
    a = tempA;
    
  }


  void display() {
    noStroke();
    smooth();
    //fill(red);
    
   
    pushMatrix();
    translate(x, y);
    rotate(angle);
    ellipse(0, 0, r*2, r);
    popMatrix();
  }
  void spin() {

    angle = angle + angleSpeed;
    angle %= TWO_PI;
  }

  void move() {

    x = x + xSpeed;
    y = y + ySpeed;

    if ((x > width) || (x < 0)) {
      xSpeed = xSpeed * -1;
    }
    if ((y > height) || (y<0)) {
      ySpeed = ySpeed * -1;
    }
  }
}
1 Like
#5

No, you don’t want to split your array.

You just want to make sure the colors are different for half of the objects.

AND: You didn’t use the color a in the class!!!


Jellybean [] jellybeans = new Jellybean [100];

void setup() {
  size(640, 360);

  for (int i = 0; i < jellybeans.length; i++) {

    // we use this color // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    color col1; 

    // Let the random decide
    if (random(100) > 50) 
      col1=#E80F07;
    else
      col1=#A71C18;

    jellybeans [i] = new Jellybean (random(width), random(height), 
      random(20, 25), 
      random(.15, 2.05), random(.75, 1.95), 
      col1);              // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  }//for
}

void draw() {

  background(255);

  for (int i = 0; i < jellybeans.length; i++) {
    jellybeans[i].display();
    jellybeans[i].spin();
    jellybeans[i].move();
  }//for
}

// ============================================================================

class Jellybean {

  float x;
  float y;
  float r;
  float angle = random(0.95);
  float angleSpeed = random(.01, .2);
  float xSpeed;
  float ySpeed;

  color a;

  // constr
  Jellybean(float tempX, float tempY, 
    float tempR, 
    float tempXspeed, float tempYspeed, 
    color tempColor) {
    x = tempX;
    y = tempY;
    r = tempR;
    xSpeed = tempXspeed;
    ySpeed = tempYspeed;

    a = tempColor;
  }// constr

  void display() {
    noStroke();
    smooth();
    fill(a);             // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    pushMatrix();
    translate(x, y);
    rotate(angle);
    ellipse(0, 0, r*2, r);
    popMatrix();
  }
  void spin() {

    angle = angle + angleSpeed;
    angle %= TWO_PI;
  }

  void move() {

    x = x + xSpeed;
    y = y + ySpeed;

    if ((x > width) || (x < 0)) {
      xSpeed = xSpeed * -1;
    }
    if ((y > height) || (y<0)) {
      ySpeed = ySpeed * -1;
    }
  }
}//class
//
#6

the method above of course doesn’t give exact 50 of each color

new solution below:

(this addresses also the problem that the speed was initially positive for all objects)


Jellybean [] jellybeans = new Jellybean [100];

void setup() {
  size(640, 360);

  IntList inventory = new IntList();
  int counter=0;

  for (int i = 0; i < jellybeans.length; i++) {
    // fill with first col and 2nd col
    if (i<50) {
      inventory.append(#E80F07);
    } else {
      inventory.append(#A71C18);
      counter++;
    }//else
  }//for

  println(counter); 

  // shuffle colors 
  inventory.shuffle();

  for (int i = 0; i < jellybeans.length; i++) {

    // we use this color  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    color col1 = inventory.get(i); 

    float speedX=random(.15, 2.05);
    float speedY=random(.75, 1.95);

    if (random(100)<50)
      speedX*=-1;
    if (random(100)<50)
      speedY*=-1;

    jellybeans [i] = new Jellybean (random(width), random(height), 
      random(20, 25), 
      speedX, speedY, 
      col1);   // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  }//for
}

void draw() {

  background(255);

  for (int i = 0; i < jellybeans.length; i++) {
    jellybeans[i].display();
    jellybeans[i].spin();
    jellybeans[i].move();
  }//for
}

// ============================================================================

class Jellybean {

  float x;
  float y;
  float r;
  float angle = random(0.95);
  float angleSpeed = random(.01, .2);
  float xSpeed;
  float ySpeed;

  color a;

  // constr
  Jellybean(float tempX, float tempY, 
    float tempR, 
    float tempXspeed, float tempYspeed, 
    color tempColor) {
    x = tempX;
    y = tempY;
    r = tempR;
    xSpeed = tempXspeed;
    ySpeed = tempYspeed;

    a = tempColor;
  }// constr

  void display() {
    noStroke();
    smooth();
    fill(a); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    pushMatrix();
    translate(x, y);
    rotate(angle);
    ellipse(0, 0, r*2, r);
    popMatrix();
  }

  void spin() {
    angle = angle + angleSpeed;
    angle %= TWO_PI;
  }

  void move() {

    x = x + xSpeed;
    y = y + ySpeed;

    if ((x > width-r) || (x < r)) {
      xSpeed = xSpeed * -1;
    }
    if ((y > height-r) || (y<r)) {
      ySpeed = ySpeed * -1;
    }
  }
}//class
//
1 Like
#7

function move() in the class:

improved bouncing that avoids stuttering of objects on the screen borders

abs() gives the value of the parameter but always positive (without the minus - sign)

    if (x > width-r) {
      xSpeed = abs(xSpeed) * -1;
    }

    if (x < r) {
      xSpeed = abs(xSpeed);
    }

    // -----

    if (y > height-r) {
      ySpeed = abs(ySpeed) * -1;
    }

    if (y<r) {
      ySpeed = abs(ySpeed);
    }
    //
#8

Thank you @Chrisir!
The if/else random solution is good for now. Though it’s good to know there’s a more precise solution to use when I’m further along in my coding knowledge.
Much appreciated!
:slight_smile:

1 Like
#9

addresses the problem that the speed was initially positive for all objects

    float speedX=random(.15, 2.05);
    float speedY=random(.75, 1.95);

    if (random(100)<50)
      speedX*=-1;
    if (random(100)<50)
      speedY*=-1;

    jellybeans [i] = new Jellybean (random(width), random(height), 
      random(20, 25), 
      speedX, speedY, 
      col1);   // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1 Like
#10

Just to focus in on the shuffled color arrays issue (separate from the Jellybeans) – here is the basic idea of how to use shuffling on an ArrayList or IntList and convert it (if needed) to an array.

size(200, 200);

// make a shuffled list of 10 red, 10 blue
IntList colors = new IntList();
for (int i=0; i<10; i++) {
  colors.append(color(255, 0, 0));
}
for (int i=0; i<10; i++) {
  colors.append(color(0, 0, 255));
}

// draw from an array copy
int[] colorArray = colors.array();
for (int i=0; i<colorArray.length; i++) {
  fill(colorArray[i]);
  ellipse(i*10, width/2, 10, 10);
}

// draw from the original IntList, shuffled first
translate(0, 20);
colors.shuffle();
for (int i=0; i<colors.size(); i++) {
  fill(colors.get(i));
  ellipse(i*10, width/2, 10, 10);
}
1 Like
#11

Thank you @jeremydouglass!
As someone new to coding It’s interesting to see the different solutions. Much appreciated.
:grinning: