Need to use data stored on an array only once using random choice of index! Arraylist?
in
Programming Questions
•
2 years ago
Hey guys.
I'm trying to make something that is chosing random colors from an array of colors but once it chooses one it will remove it from the array to not be chosen again.
From what I know there is no simple way of removing indexes from arrays but with arraylists is possible. The problem I'm having is that I don't know how to get the data out of the arraylist properly do be used as colors.
Where I have cor = mycolours[int(rand1)]; I'd like to have something like cor = colors.get(int(rand1)); but I get this error " Cannot convert Object to int"
can someone tell me how to access it?
I'm trying to make something that is chosing random colors from an array of colors but once it chooses one it will remove it from the array to not be chosen again.
From what I know there is no simple way of removing indexes from arrays but with arraylists is possible. The problem I'm having is that I don't know how to get the data out of the arraylist properly do be used as colors.
Where I have cor = mycolours[int(rand1)]; I'd like to have something like cor = colors.get(int(rand1)); but I get this error " Cannot convert Object to int"
can someone tell me how to access it?
- color[] mycolours = { #F9EC31, #37B34A, #1B75BB, #652D90, #EB008B, #BE1E2D};
PShape a, b, c, d, e, f, g, r_emote, drawing;
color cor, cor2, cor3, cor4, cor5, cor6;
float rand1;
float rand2;
float rand3;
float rand4;
float rand5;
float rand6;
ArrayList colors;
void setup(){
size(400,600);
smooth();
noStroke();
drawing = loadShape("r-emote.svg");
a = drawing.getChild("a");
b = drawing.getChild("b");
c = drawing.getChild("c");
d = drawing.getChild("d");
e = drawing.getChild("e");
f = drawing.getChild("f");
r_emote = drawing.getChild("r");
drawing.disableStyle();
shapeMode(CENTER);
background(255);
colors = new ArrayList();
colors.add(mycolours[0]);
colors.add(mycolours[1]);
colors.add(mycolours[2]);
colors.add(mycolours[3]);
colors.add(mycolours[4]);
colors.add(mycolours[5]);
// println(colors.get(3));
}
void draw(){
rand1 = random(6)-1;
// float wdf = (float)colors.get((int)rand1);
// println(colors.getKey(
// cor = colors.getKey([int(rand1)]);
cor = mycolours[int(rand1)];
fill(cor);
shape(a, width/2,height/2);
rand2 = random(5)-1;
cor2 = mycolours[int(rand2)];
fill(cor2);
shape(b, width/2,height/2);
rand3 = random(6)-1;
cor3 = mycolours[int(rand3)];
fill(cor3);
shape(c, width/2,height/2);
rand4 = random(6)-1;
cor4 = mycolours[int(rand4)];
fill(cor4);
shape(d, width/2,height/2);
rand5 = random(6)-1;
cor5 = mycolours[int(rand5)];
fill(cor5);
shape(e, width/2,height/2);
cor6 = mycolours[int(rand6)];
fill(cor6);
shape(f, width/2,height/2);
fill(0);
shape(r_emote, width/2,height/2);
// cor = mycolours[int(random(mycolours.length))];
//print(int(rand1));
//print("\t");
//print(int(rand2));
//print("\t");
//print(int(rand3));
//print("\t");
//print(int(rand4));
//print("\t");
//print(int(rand5));
//print("\t");
//print(int(rand6));
//print("\t");
noLoop();
}
1