How to pick random value from array?
in
Programming Questions
•
10 months ago
Hi,
I am trying to get my program to pick a new random fill color from an array after the mouse is clicked. The section of code I am trying to implement it in currently is in the display section of the class "Master".
Here is my code so far:
- int c1 = (int)random(0, 4);
- int c2 = (int)random(0, 4);
- int c3 = (int)random(0, 4);
- int c4 = (int)random(0, 4);
- int c5 = (int)random(0, 4);
- int R[] = {
- 0, 224, 255, 35
- };
- int G[] = {
- 173, 0, 240, 31
- };
- int B[] = {
- 239, 139, 0, 32
- };
- int scr = 0;
- int a = 1;
- int[] fillColor; //Declare array
- color r1 = color(R[c1], G[c1], B[c1]);
- color r2 = color(R[c2], G[c2], B[c2]);
- color r3 = color(R[c3], G[c3], B[c3]);
- color r4 = color(R[c4], G[c4], B[c4]);
- color r5 = color(R[c5], G[c5], B[c5]);
- color m1 = color(R[c1], G[c1], B[c1]);
- color m2 = color(R[c2], G[c2], B[c2]);
- color m3 = color(R[c3], G[c3], B[c3]);
- color m4 = color(R[c4], G[c4], B[c4]);
- color m5 = color(R[c5], G[c5], B[c5]);
- color pixelColor1;
- color masterColor1;
- //color fill1;
- Score1 scr1; //declare the object
- Circle crcl;
- Master mstr;
- void setup() {
- //frameRate(200);
- size(500, 500);
- crcl = new Circle(250, 350, 85);
- mstr = new Master(250, 200, 85);
- scr1= new Score1();
- pixelColor1= get(mouseX, mouseY);
- masterColor1= get(250, 200);
- fillColor = new int[5];
- fillColor[0] = m1;
- fillColor[1] = m2;
- fillColor[2] = m3;
- fillColor[3] = m4;
- fillColor[4] = m5;
- }
- void draw() {
- background(255);
- crcl.display();
- mstr.display();
- scr1.display();
- }
- void mousePressed() {
- //scr.select();
- if (mousePressed == true) {
- pixelColor1 = get(mouseX, mouseY);
- masterColor1 = get(250, 200);
- println(pixelColor1); // get color test
- println(masterColor1); // get color test
- //delay(200);
- if ((masterColor1 == pixelColor1) && (mouseY > 264)) {
- scr++;
- mstr.display();
- }
- else {
- mstr.display();
- }
- }
- }
- String begin(int a) {
- String Buffer;
- Buffer=nf(a, 1);
- return(Buffer);
- }
- class Circle {
- float x, y, diameter;
- //float diameter;
- Circle(float xpos, float ypos, float dia ) {
- x = xpos;
- y = ypos;
- diameter = dia;
- }
- void display() {
- noStroke();
- fill(r1);
- ellipse(x, y, 85, 85);
- fill(r2);
- ellipse(x+85, y, 85, 85);
- fill(r3);
- ellipse(x-85, y, 85, 85);
- fill(r4);
- ellipse(x-42, y+75, 85, 85);
- fill(r5);
- ellipse(x+43, y+75, 85, 85);
- }
- }
- class Master { //Circle w/ stroke == master circle
- float x, y, diameter;
- Master(float xpos, float ypos, float dia ) {
- x = xpos;
- y = ypos;
- diameter = dia;
- }
- void display() {
- strokeWeight(5);
- stroke(0);
- fill(fillColor[0]); //need to set this up to produce a random fill color from array "fillColor"
- //fill(random(fillColor))
- ellipse(x, y, 85, 85);
- ellipse(x, y, 85, 85);
- }
- }
- class Score1 {
- Score1() {
- }
- void display() {
- noStroke();
- textAlign(CENTER);
- textSize(50);
- fill(237, 28, 36);
- text (""+begin(scr), 250, 100);
- }
- }
- /*
- void select() {
- if (mousePressed == true) {
- pixelColor1 = get(mouseX, mouseY);
- masterColor1 = get(250, 200);
- println(pixelColor1); // get color test
- println(masterColor1); // get color test
- //delay(200);
- if ((masterColor1 == pixelColor1) && (mouseY > 264)) {
- scr++;
- //fill1++;
- }
- else {
- //fill1++;
- }
- }
- }
- String begin(int a) {
- String Buffer;
- Buffer=nf(a, 1);
- return(Buffer);
- }
- }
- */
1