Emma Moreno
Emma Moreno

Reputation: 1

ArrayIndexOutOfBoundsException Appears and highlights " pixels[loc] = c; "

I'm not sure what is causing this issue, because whenever I try to run this code it doesn't work but in a different tab, it is working. What is the difference here?

//Non-functioning Code:

PImage ff1;
PImage tumblr_odmqonG4Qv1v6tw5po1_500;

void setup() {
  size(500,281);
  ff1=loadImage("ff1.jpg");
tumblr_odmqonG4Qv1v6tw5po1_500=loadImage("tumblr_odmqonG4Qv1v6tw5po1_500.gif");
}

void draw() {
  background(tumblr_odmqonG4Qv1v6tw5po1_500);
  loadPixels();
  ff1.loadPixels();

  for (int x=0; x < ff1.width; x++ ) {
    for (int y=0; y< ff1.height; y++ ) {
      int loc = x + y*ff1.width;

      float r = red (ff1.pixels[loc]);
      float g = green(ff1.pixels[loc]);
      float b = blue(ff1.pixels[loc]);

      float distance = dist(x, y, mouseX, mouseY);

      float adjustBrightness = map(distance, 0, 50, 8, 0);
      r *= adjustBrightness;
      g *= adjustBrightness;
      b *= adjustBrightness;

      r = constrain(r, 0, 255);
      g = constrain(g, 0, 255);
      b = constrain(b, 0, 255);

      color c = color(r, g, b);
      pixels[loc] = c;
    }
  }

  updatePixels();
}


//Functioning Code:

PImage sunflower;

void setup() {
  size(200, 200);
  sunflower = loadImage( "sunflower.png" );
}

void draw() {
  loadPixels();
  sunflower.loadPixels();

  for (int x = 0; x < sunflower.width; x++ ) {
    for (int y = 0; y < sunflower.height; y++ ) {
      int loc = x + y*sunflower.width;

      float r = red  (sunflower.pixels[loc]);
      float g = green(sunflower.pixels[loc]);
      float b = blue (sunflower.pixels[loc]);

      float distance = dist(x, y, mouseX, mouseY);

      float adjustBrightness = map(distance, 0, 50, 8, 0);
      r *= adjustBrightness;
      g *= adjustBrightness;
      b *= adjustBrightness;

      r = constrain(r, 0, 255);
      g = constrain(g, 0, 255);
      b = constrain(b, 0, 255);

      color c = color(r, g, b);
      pixels[loc] = c;
    }
  }

  updatePixels();
}

I am not very gifted in code, so I may be completely oblivious to something that may be basic knowledge. If you have any idea of what might be causing the first code to not work please tell me. -Emma

Upvotes: 0

Views: 44

Answers (0)

Related Questions