error arrayindexoutofboundsexception
in
Programming Questions
•
3 years ago
I tried to posterize a picture but when i clicked the mouse for the effect to happen. the error appears: arrayindexoutofboundsexception
the program is like this:
int i;
float total, r, g, b;
PImage img;
void setup(){
size(427,640);
background(255);
img=loadImage("Hey.jpg");
image(img, 0, 0);
}
void draw(){
}
void mouseClicked(){
img.loadPixels();
for(i=0; i<img.pixels.length; i++){
r=red(img.pixels[i]);
g=green(img.pixels[i]);
b=blue(img.pixels[i]);}
total=r+g+b;
if (total<182)
img.pixels[i]=color(0, 51, 76);
else if (total<364)
img.pixels[i]=color(217, 26, 33);
else if (total<546)
img.pixels[i]=color(112,150,158);
else
img.pixels[i]=color(252, 227, 166);
img.updatePixels();}
the program is like this:
int i;
float total, r, g, b;
PImage img;
void setup(){
size(427,640);
background(255);
img=loadImage("Hey.jpg");
image(img, 0, 0);
}
void draw(){
}
void mouseClicked(){
img.loadPixels();
for(i=0; i<img.pixels.length; i++){
r=red(img.pixels[i]);
g=green(img.pixels[i]);
b=blue(img.pixels[i]);}
total=r+g+b;
if (total<182)
img.pixels[i]=color(0, 51, 76);
else if (total<364)
img.pixels[i]=color(217, 26, 33);
else if (total<546)
img.pixels[i]=color(112,150,158);
else
img.pixels[i]=color(252, 227, 166);
img.updatePixels();}
1