Loops with different colors

why doesn’t this code work?

	color[] c = new color [6];

	int x;
int l;

void setup(){
	size(900, 550);

	
	c[0]=color(8,100, 0);   
  c[1]=color(12);  
  c[2]=color(16);  
	c[3]=color(4);
	c[4]=color(4);
	c[5]=color(4);

	
}

void draw() {
	int x = 0;
	int l = 125;
  for (int y = 0; y <= 6; y= y + 1 ) {
	fill(c[x];
		rect(width - 35, l, 50, 50);
		x++;
		l = l + 75;
}
}


1 Like

Hi @Kenga123456 ,

When asking a question on the forum, it’s better to provide more details about what you are trying to solve and what you already did (what doesn’t work…) :yum:

Quick tip : you can press Ctrl+T in the Processing IDE to auto format your code (correct indentation for example)

I your code there’s an error at this line : fill(c[x]; but it may be caused by copy pasting…

The first error that comes out is :

ArrayIndexOutOfBoundsException: 6

when trying to access a color in your array.

If you look at y in the for loop, it goes from 0 to what? :wink:

nvm i solved it

color[] c = new color[0];
int x;

void setup() {
  size(900, 550);
  c[0]=color(255, 0, 0); 
  c[1]=color(0, 255, 0); 
  c[2]=color(255, 255, 0); 
	c[3]=color(0, 0, 255);
  c[4]=color(64, 224, 208); 
  c[5]=color(255); 
}



void draw() {
  x = 0;
	rectMode(CENTER);

  for (int i = 125; i <= 500; i = i+75 ) {
    fill(c[x]);
    rect(width - 35, i, 50, 50);
    x++;
   
  }
}


1 Like