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

**URL:** <https://discourse.processing.org/t/how-do-i-assign-2-specific-colors-to-an-array-of-objects/10984>\
**Category:** Coding Questions\
**Created:** [May 7, 2019, 3:05am UTC](https://discourse.processing.org/t/how-do-i-assign-2-specific-colors-to-an-array-of-objects/10984 "2019-05-07T03:05:28Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [May 7, 2019, 3:05am UTC](https://discourse.processing.org/t/how-do-i-assign-2-specific-colors-to-an-array-of-objects/10984/1 "2019-05-07T03:05:28Z")

</div>

- **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!

```auto
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;
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Capt.Sloth](https://avatars.discourse-cdn.com/v4/letter/c/4af34b/32.png) [@Capt.Sloth](https://discourse.processing.org/u/Capt.Sloth)\
**Post date:** [May 7, 2019, 3:27am UTC](https://discourse.processing.org/t/how-do-i-assign-2-specific-colors-to-an-array-of-objects/10984/2 "2019-05-07T03:27:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [May 7, 2019, 4:13pm UTC](https://discourse.processing.org/t/how-do-i-assign-2-specific-colors-to-an-array-of-objects/10984/3 "2019-05-07T16:13:40Z")

</div>

Thank you @Capt.Sloth! I will try this. 😉

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [May 7, 2019, 5:49pm UTC](https://discourse.processing.org/t/how-do-i-assign-2-specific-colors-to-an-array-of-objects/10984/4 "2019-05-07T17:49:44Z")

</div>

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?

* * *

```auto
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;
    }
  }
}

```

---

<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:** [May 7, 2019, 6:26pm UTC](https://discourse.processing.org/t/how-do-i-assign-2-specific-colors-to-an-array-of-objects/10984/5 "2019-05-07T18:26:37Z")

</div>

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!!!

```auto

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
//

```

---

<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:** [May 7, 2019, 7:18pm UTC](https://discourse.processing.org/t/how-do-i-assign-2-specific-colors-to-an-array-of-objects/10984/6 "2019-05-07T19:18:31Z")

</div>

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)

```auto

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
//

```

---

<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:** [May 7, 2019, 7:27pm UTC](https://discourse.processing.org/t/how-do-i-assign-2-specific-colors-to-an-array-of-objects/10984/7 "2019-05-07T19:27:21Z")

</div>

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)

```auto
    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);
    }
    //

```

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [May 7, 2019, 9:55pm UTC](https://discourse.processing.org/t/how-do-i-assign-2-specific-colors-to-an-array-of-objects/10984/8 "2019-05-07T21:55:45Z")

</div>

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!  
🙂

---

<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:** [May 8, 2019, 3:14pm UTC](https://discourse.processing.org/t/how-do-i-assign-2-specific-colors-to-an-array-of-objects/10984/9 "2019-05-08T15:14:16Z")

</div>

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

```auto
    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); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [May 11, 2019, 6:23pm UTC](https://discourse.processing.org/t/how-do-i-assign-2-specific-colors-to-an-array-of-objects/10984/10 "2019-05-11T18:23:29Z")

</div>

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.

```auto
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);
}

```

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [May 12, 2019, 2:30am UTC](https://discourse.processing.org/t/how-do-i-assign-2-specific-colors-to-an-array-of-objects/10984/11 "2019-05-12T02:30:10Z")

</div>

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