0

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

  • Have you tried [debugging your code](https://happycoding.io/tutorials/processing/debugging)? What is the value of the variables you're using on the line that throws the exception? What do you expect to happen? What happens instead? – Kevin Workman Nov 20 '19 at 18:22
  • The only reason for an ArrayIndexOutOfBounds exception to be raised is that your index is going out of bounds (either before the lowest valid index or after the highest valid index). You're the only one in a position to debug why `loc` is exceeding the valid limits of `pixels[]`. Use the debugger to step through the code to see what's making it go past the ends of the array. (I see you working everywhere else with `ff1.pixels`, where `loc` is valid, but not in `pixels[loc]` (note the difference). Where do you initialize `pixels`?) – Ken White Nov 20 '19 at 18:32

0 Answers0