Receiving error but cannot debug: ArrayIndexOutOfBoundsException

int currentScreen;
PImage img;
PImage img0;
PImage img1;
import processing.sound.*;
SoundFile file;

void setup() {
  size (1000, 683);
  noStroke();
  smooth();
  img = loadImage ("batman.jpeg");
  img0 = loadImage ("joker.jpg");
  img1 = loadImage("jolaugh.jpg");
  file = new SoundFile(this, "jlaugh.mp3");
}

void draw() {
  
  switch (currentScreen) {
    case 1: keyPressedOne(); break;
    case 2: keyReleasedOne(); break;
    //case 3: keyReleasedZero(); break;
    default: image(img, 0, 0); break;
  }
}

void mousePressed() {
  currentScreen++;
  if (currentScreen > 2) {currentScreen = 0;}
}

void keyPressedOne() {
  if (keyPressed) {
    if (key == 'a'){
      int x = int(random(img.width));
      int y = int(random(img.height));
      color pix = img.get(x, y);
      fill(pix, 128);
      ellipse(x, y, 50, 50);
    }
} else {
  image(img, 0, 0);
  }
}

void keyReleasedOne() {
  loadPixels();
  if (key == '0'){
    for (int x = 0; x < img1.width; x++ ) {
    for (int y = 0; y < img1.height; y++ ) {
    
    
    int loc = x + y*img1.width;
      float r = red  (img1.pixels[loc]);
      float g = green(img1.pixels[loc]);
      float b = blue (img1.pixels[loc]);
      
      float adjustBrightness = map(mouseX, 0, width, 0, 8); 
      //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);
      if(y > 200)
      c = color(200, g, b);
      pixels[loc] = c;
      //img1.resize(1000,683);
      //image(img1,0 ,0);
      //file.play();
      //noLoop();
      }
    }
    
     
  } else {
    img0.resize(1000, 683);
    image(img0, 0,0);
    }
       updatePixels();
  }
1 Like

Hello and welcome to the forum!

Great to have you here!

Do you mean, the code runs but does not all you want it to?

Or rather does not run? What is the error message then?

1 Like

ArrayIndexOutOfBoundsException: 683520

1 Like

in which line?

Which line is highlighted with the error?

1 Like

pixels[loc] = c; <-- this is the line

1 Like

I change a little bit your code, you need another switch in draw method.
Maybe your error is a problem in path loading outside file

import processing.sound.*;
int currentScreen;
PImage img;
PImage img0;
PImage img1;
color pix;
int x, y;
SoundFile file;
void setup() {
    size (1000, 683);
    noStroke();
    smooth();
    img = loadImage ("batman.jpeg");
    img0 = loadImage ("joker.jpg");
    img1 = loadImage("jolaugh.jpg");
    file = new SoundFile(this, "jlaugh.mp3");
}

void draw() {
    switch (currentScreen) {
    case 1:
        fill(pix, 128);
        ellipse(x, y, 50, 50);
        break;
    case 2:
        image(img1, 0, 0);
        break;
    default :
     image(img, 0, 0); 
        break;
    }
}

void mousePressed() {
    currentScreen++;
    if (currentScreen > 2) {
        currentScreen = 0;
    }
    switch (currentScreen) {
    case 1: 
        keyPressedOne(); 
        break;
    case 2: 
        keyReleasedOne(); 
        break;
        //case 3: keyReleasedZero(); break;
    default: 
        break;
    }
}

void keyPressedOne() {
    if (key == 'a') {
        x = int(random(img.width));
        y = int(random(img.height));
        pix = img.get(x, y);
    }
}

void keyReleasedOne() {
    loadPixels();
    if (key == '0') {
        for (int x = 0; x < img1.width; x++ ) {
            for (int y = 0; y < img1.height; y++ ) {

                int loc = x + y*img1.width;
                float r = red  (img1.pixels[loc]);
                float g = green(img1.pixels[loc]);
                float b = blue (img1.pixels[loc]);

                float adjustBrightness = map(mouseX, 0, width, 0, 8); 
                //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);
                if (y > 200)
                    c = color(200, g, b);
                pixels[loc] = c;
            }
        }
    }  
    updatePixels();
    img1.resize(1000, 683);
    file.play();
}

1 Like

I think you want img1.pixels[loc] = c;

you forgot img1

1 Like

I don’t understand this part of code.
I think if you do this you must also :

img1.loadPixels();
//and
img1.updatePixels();
2 Likes
int currentScreen;
PImage img;
PImage img0;
PImage img1;
import processing.sound.*;
SoundFile file;

void setup() {
  size (1000, 683);
  noStroke();
  smooth();
  img = loadImage ("batman.jpeg");
  img0 = loadImage ("joker.jpg");
  img1 = loadImage("jolaugh.jpg");
  file = new SoundFile(this, "jlaugh.mp3");
}

void draw() {
  
  switch (currentScreen) {
    case 1: keyPressedOne(); break;
    case 2: keyReleasedOne(); break;
    //case 3: keyReleasedZero(); break;
    default: image(img, 0, 0); break;
  }
}

void mousePressed() {
  currentScreen++;
  if (currentScreen > 2) {currentScreen = 0;}
}

void keyPressedOne() {
  if (keyPressed) {
    if (key == 'a'){
      int x = int(random(img.width));
      int y = int(random(img.height));
      color pix = img.get(x, y);
      fill(pix, 128);
      ellipse(x, y, 50, 50);
    }
} else {
  background (200);
  }
}

void keyReleasedOne() {
  img1.loadPixels();
  if (keyPressed) {
  if (key == '0'){
    for (int x = 0; x < img1.width; x++ ) {
    for (int y = 0; y < img1.height; y++ ) {
    
    
    int loc = x + y*img1.width;
    
    
      float r = red  (img1.pixels[loc]);
      float g = green(img1.pixels[loc]);
      float b = blue (img1.pixels[loc]);
      
     float adjustBrightness = map(mouseX, 0, width, 0, 8); 
      //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);
      if(y > 200)
      c = color(200, g, b);
      
      
      img1.pixels[loc] = c;
      img1.resize(1000,683);
      image(img1,0 ,0);
    }
      
      }
    }
    
    img1.updatePixels();
    file.play();
    noLoop();
     
  } else { 
    
    img0.resize(1000, 683);
    image(img0, 0,0);
    }
    
  }

I tried this one but then the code gets stuck and not getting error this time. I need both images in if and else. Thank you for trying, really appreciated

1 Like

I tried this one, did not make any difference

1 Like

@Chrisir it did not work

1 Like

this seems to do something.

  • initially currentScreen is 0, so the default part of switch in draw() is called!!!

  • When you click the mouse once, currentScreen is 1 and you can click “a” there. Please note that the change is appears only briefly since it is inside the if-clause.

  • When you click the mouse again, currentScreen is 2 and you can click “0” there

Chrisir


int currentScreen;
PImage img;
PImage img0;
PImage img1;

import processing.sound.*;
SoundFile file;

//-----------------------------------------------------

void setup() {
  size (1000, 683);
  noStroke();
  smooth();

  img = loadImage ("batman.jpeg");
  img0 = loadImage ("joker.jpg");
  img1 = loadImage("jolaugh.jpg");

  file = new SoundFile(this, "jlaugh.mp3");
}

void draw() {

  switch (currentScreen) {

  case 1: 
    keyPressedOne(); 
    break;

  case 2: 
    keyReleasedOne(); 
    break;

    //case 3: keyReleasedZero(); break;

  default: 
    image(img, 0, 0); 
    break;
  }//switch

  fill(255); 
  text(currentScreen, width-23, 23);
}//func 

void mousePressed() {
  currentScreen++;
  if (currentScreen > 2) {
    currentScreen = 0;
  }
}

void keyPressedOne() {
  if (keyPressed) {
    if (key == 'a') {
      int x = int(random(img.width));
      int y = int(random(img.height));
      color pix = img.get(x, y);
      fill(pix, 128);
      ellipse(x, y, 50, 50);
    }
  } else {
    background (200);
  }
}

void keyReleasedOne() {

  if (keyPressed) {
    if (key == '0') {
      img1.loadPixels();
      for (int x = 0; x < img1.width; x++ ) {
        for (int y = 0; y < img1.height; y++ ) {

          int loc = x + y*img1.width;


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

          float adjustBrightness = map(mouseX, 0, width, 0, 8); 
          //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);
          if (y > 200)
            c = color(200, g, b);


          img1.pixels[loc] = c;
        }
      }//for 
      img1.updatePixels();
      img1.resize(1000, 683);
      image(img1, 0, 0);
    }


    //file.play();
    //noLoop();
  } else {

    img0.resize(1000, 683);
    image(img0, 0, 0);
  }
}
1 Like

works better, but if you enable sound it get’s stuck on “img1”

1 Like

did you comment out those 2 lines?

1 Like

when I uncomment it, it got stuck again

1 Like

So noLoop stops entire program. Do you want this? No.

And what error do you get at which line?

Sure that the audio file has the right name when you load it? I haven’t tried.

1 Like
int currentScreen;
PImage img;
PImage img0;
PImage img1;
PImage img2;
PImage img3;

import processing.sound.*;
SoundFile file, file2;

void setup() {
  size (1000, 683);
  //noStroke();

  img = loadImage ("batman.jpeg");
  img0 = loadImage ("joker.jpg");
  img0.resize(1000, 683);
  
  img1 = loadImage("jolaugh.jpg");
  img1.resize(1000, 683);
  
  img2 = loadImage("riddler.jpeg");
  img2.resize(1000, 683);
  
  img3 = loadImage("rid.png");
  img3.resize(200,300);

  file = new SoundFile(this, "jlaugh.mp3");
  file2 = new SoundFile(this, "rmt.mp3");
}

void draw() {

  switch (currentScreen) {

  case 1: 
    keyPressedOne(); 
    break;

  case 2: 
    keyReleasedOne(); 
    break;

    case 3: 
    keyReleasedTwo(); 
    break;

  default: 
    image(img, 0, 0); 
    break;
  }

  fill(255); 
  text(currentScreen, width-23, 23);
}

void mousePressed() {
  currentScreen++;
  if (currentScreen > 4) {
    currentScreen = 0;
  }
}

void keyPressedOne() {
  if (keyPressed) {
    if (key == 'a') {
      int x = int(random(img.width));
      int y = int(random(img.height));
      color pix = img.get(x, y);
      fill(pix, 128);
      ellipse(x, y, 50, 50);
    }
  } else {
    background (200);
  }
}

void keyReleasedOne() {

  if (keyPressed) {
    if (key == '1') {
      img1.loadPixels();
      for (int x = 0; x < img1.width; x++ ) {
        for (int y = 0; y < img1.height; y++ ) {

          int loc = x + y*img1.width;

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

          //float adjustBrightness = map(mouseX, 0, width, 0, 8); 
          ////r *= adjustBrightness;
          ////g *= adjustBrightness;
          ////b *= adjustBrightness;
          
          color c = color(r, g, b);
          if (y > mouseY)
            c = color(200, random(60,100), r, 0);
          if (x > mouseX/2)
            c = color(random(0,150),b, 50, 0);
          if (x > mouseX)
            c = color(r/2+62, b*1.5, g/2+50, 0);
            
          r = constrain(r, 0, 255); 
          g = constrain(g, 0, 255);
          b = constrain(b, 0, 255);
          
          //noStroke();
          //fill(c);
          //ellipse(x,y,10,10);

         img1.pixels[loc] = c;
         
        }
      }//for 
      img1.updatePixels();
      image(img1, 0, 0);
    }

    if(!file.isPlaying()){
    file.play();
    }
  } else {

    img0.resize(1000, 683);
    image(img0, 0, 0);
  }
}

void keyReleasedTwo() {
  if (keyPressed){
    if ( key == '2') {
      background(img2);
      //blend(img3, 0, 0, 33, 100, 67, 0, 33, 100, ADD);
      //blend(img3, 0, 0, 33, 100, 67, 0, 33, 100, ADD);
      
      int i = 1;
      while (i < 100) {
        image(img3, i*10, i*5);
          i = i + 20;
      } 
      if(!file2.isPlaying()){
      file2.play();
      }
    }
  } else {
    image(img2, 0, 0);
  }
}

NEED HELP WITH ‘IMG1’ PIXEL DISPLAY

1 Like

You have to give a longer description:

What happens now falsely, what do you want to happen instead?

Please note that some of the stuff is inside if-clauses. So this stuff shows up only while you hold down the key.

When you don’t want this, do the changes of pixels [] still inside the if, but image() and ellipse outside it

1 Like

what do you suggest? As I cannot achieve the goal as the image hides behind the effect but the effect is not transparent and hides the picture.

1 Like

Ah, okay, when you want it so that the other image is shown only when key hold, okay

Alternatively, use key 0 to toggle between one view and the other - press key shortly, view gets changed permanently. Use a boolean variable viewChangedImage

1 Like