# Receiving error but cannot debug: ArrayIndexOutOfBoundsException

**URL:** <https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415>\
**Category:** Libraries\
**Created:** [March 5, 2020, 12:38pm UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415 "2020-03-05T12:38:12Z")\
**Posts on this page:** 19\
**Page:** 2

<div class="post-metadata">

**Author:** ![firetryer](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@firetryer](https://discourse.processing.org/u/firetryer)\
**Post date:** [March 7, 2020, 8:57am UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/22 "2020-03-07T08:57:27Z")

</div>

do you mind if you can write it for me please.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 7, 2020, 9:28am UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/23 "2020-03-07T09:28:30Z")

</div>

No, sorry, I am not at home

Show your attempt please

---

<div class="post-metadata">

**Author:** ![firetryer](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@firetryer](https://discourse.processing.org/u/firetryer)\
**Post date:** [March 7, 2020, 9:35am UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/24 "2020-03-07T09:35:07Z")

</div>

```auto
int currentScreen;
int col = 15;
int row = 50;
int a, b, x, y, speed;
mark[][] mymark = new mark[col][row];

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(20,10);

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

void draw() {

  switch (currentScreen) {

  case 1: 
    keyPressedOne(); 
    break;

  case 2: 
    keyReleasedOne(); 
    for (int i=0; i<col; i++) {
    for (int j=0; j<row; j++) {
      mymark[i][j] = new mark(color(random(255)), i*40, j*8, random(1,5));
    }
  }
    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') {
      noStroke();
    fill(img.get(mouseX, mouseY), 128);
  //fill(img.get(x, y), 128);
  rect(mouseX, mouseY, 10, 10);
  
  int x = int (random(width));
  int y = int (random(height));
  x+= int (random(-speed, speed));
  y += int (random(-speed, speed));
  color c = img.get(x, y);
  fill(c, 50);
  ellipse(x, y, 20, 20);
    }
  } else {
    background (200);
  }
}

void keyReleasedOne() {
    boolean a = false;
  if (!a) {
      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 > mouseX)
            c = color(200, random(60,100), r, 0);
          if (x > mouseY/2)
            c = color(random(0,150),b, 50, 0);
          if (x > mouseY)
            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;
        }
        img1.updatePixels();
      image(img1, 0, 0);
      }//for 
      

    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;
      

  background(255);
  for (int i=0; i<col; i++) {
    for (int j=0; j<row; j++) {
      mymark[i][j].move();
      mymark[i][j].display();
    }
  }
      } 
      if(!file2.isPlaying()){
      file2.play();
      }
    } else {
    image(img2, 0, 0);
  }
}

```

**I tried this method, no luck**

---

<div class="post-metadata">

**Author:** ![firetryer](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@firetryer](https://discourse.processing.org/u/firetryer)\
**Post date:** [March 7, 2020, 12:50pm UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/25 "2020-03-07T12:50:43Z")

</div>

```auto
int currentScreen;
int col = 10;
int row = 100;
int speed;
mark[][] mymark = new mark[col][row];

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(20, 40);

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

  for (int i=0; i<col; i++) {
    for (int j=0; j<row; j++) {
      mymark[i][j] = new mark(color(random(255)), i*40, j*8, random(1, 5));
    }
  }
}

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 == '1') {
      int x = int(random(img.width));
      int y = int(random(img.height));
      noStroke();
      fill(img.get(mouseX, mouseY), 128);
      rect(mouseX, mouseY, 10, 10);
      x+= int (random(-speed, speed));
      y += int (random(-speed, speed));
      color c = img.get(x, y);
      fill(c, 50);
      ellipse(x, y, 20, 20);
    }
  } else {
    background (200);
  }
}

void keyReleasedOne() {

  if (mousePressed) {
    if (!mousePressed) {
      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;
          print(img1,0,0);
        }
      }//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 == '3') {
      //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;

      background(255);
      for (int i=0; i<col; i++) {
        for (int j=0; j<row; j++) {
          mymark[i][j].move();
          mymark[i][j].display();
        }
      }
    } 
    if (!file2.isPlaying()) {
      file2.play();
    }
  } else {
    image(img2, 0, 0);
  }
}

```

**I even tried mousePressed and Boolean variables but it’s not working as expected. This part is only my problem. Just want to fix it ASAP**

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 7, 2020, 12:55pm UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/26 "2020-03-07T12:55:53Z")

</div>

Do you have something like a = ! a;

This would toggle a

Give „a“ a better name and make it global

---

<div class="post-metadata">

**Author:** ![firetryer](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@firetryer](https://discourse.processing.org/u/firetryer)\
**Post date:** [March 7, 2020, 12:57pm UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/27 "2020-03-07T12:57:17Z")

</div>

I’m sorry, i don’t understand.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 7, 2020, 1:06pm UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/28 "2020-03-07T13:06:10Z")

</div>

> [@Chrisir](#):
>
> when you want it so that the other image is shown only when key hold, okay

You haven‘t comment on that.

What is your problem? That the changed image appears only during you hold the key?

Then you need to implement a boolean variable which you had and named a.

Did you delete it?

before setup boolean screen1bool=false;

Then on keyPressed toggle this variable as I have shown

Then outside keyPressed

if(screen1bool) {

}

---

<div class="post-metadata">

**Author:** ![firetryer](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@firetryer](https://discourse.processing.org/u/firetryer)\
**Post date:** [March 7, 2020, 1:19pm UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/29 "2020-03-07T13:19:23Z")

</div>

```auto
int currentScreen;
int col = 10;
int row = 100;
int speed;
mark[][] mymark = new mark[col][row];

PImage img;
PImage img0;
PImage img1;
PImage img2;
PImage img3;

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

boolean screen1bool = false;

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(20, 40);

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

  for (int i=0; i<col; i++) {
    for (int j=0; j<row; j++) {
      mymark[i][j] = new mark(color(random(255)), i*40, j*8, random(1, 5));
    }
  }
}

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 > 3) {
    currentScreen = 0;
  }
}

void keyPressedOne() {
  if (keyPressed) {
    if (key == '1') {
      int x = int(random(img.width));
      int y = int(random(img.height));
      noStroke();
      fill(img.get(mouseX, mouseY), 128);
      rect(mouseX, mouseY, 10, 10);
      x+= int (random(-speed, speed));
      y += int (random(-speed, speed));
      color c = img.get(x, y);
      fill(c, 50);
      ellipse(x, y, 20, 20);
    }
  } else {
    background (200);
  }
}

void keyReleasedOne() {

  if (screen1bool == false) {
    if (!screen1bool) {
      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;
          print(img1,0,0);
        }
      }//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 == '3') {
      //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;

      background(255);
      for (int i=0; i<col; i++) {
        for (int j=0; j<row; j++) {
          mymark[i][j].move();
          mymark[i][j].display();
        }
      }
    } 
    if (!file2.isPlaying()) {
      file2.play();
    }
  } else {
    image(img2, 0, 0);
  }
}

```

**I have did what you said but the effect on the picture is not smoothly**

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 7, 2020, 1:40pm UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/30 "2020-03-07T13:40:09Z")

</div>

> [@Chrisir](#):
>
> Then on keyPressed toggle this variable as I have shown

Do you have this?

To make it faster you could do a new image in setup and depending on  
screen1bool use either the old or the new.

Since it doesn’t change every time

---

<div class="post-metadata">

**Author:** ![firetryer](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@firetryer](https://discourse.processing.org/u/firetryer)\
**Post date:** [March 7, 2020, 1:41pm UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/31 "2020-03-07T13:41:50Z")

</div>

I’m not getting your meaning. Can you add the lines in my Code.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 7, 2020, 1:55pm UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/32 "2020-03-07T13:55:10Z")

</div>

This would toggle on key b

```auto
if (keyPressed) {
  if ( key == ‘b’) {
    screen1bool= ! screen1bool;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 7, 2020, 1:57pm UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/33 "2020-03-07T13:57:43Z")

</div>

> [@firetryer](#):
>
> img1.loadPixels();  
> for (int x = 0; x \< img1.width; x++ ) {  
> for (int y = 0; y \< img1.height; y++ ) {
> 
> ```auto
> 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;
> 
> ```

This you could do in setup ()

Here you make a new image img1 maybe you can copy it into imgNew and then load img1

Then you have 2 different images

Then you show one or the other depending on your boolean

---

<div class="post-metadata">

**Author:** ![firetryer](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@firetryer](https://discourse.processing.org/u/firetryer)\
**Post date:** [March 7, 2020, 2:09pm UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/34 "2020-03-07T14:09:02Z")

</div>

```auto
int currentScreen;
int col = 10;
int row = 100;
int speed;
mark[][] mymark = new mark[col][row];

PImage img;
PImage img0;
PImage img1;
PImage img2;
PImage img3;

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

boolean screen1bool = false;

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

  img = loadImage ("batman.jpeg");
  img0 = loadImage ("joker.jpg");
  img0.resize(1000, 683);

  img1 = loadImage("jolaugh.jpg");
  img1.resize(1000, 683);
  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+60, 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;
        }
        img1.updatePixels();
      image(img1, 0, 0);
      if (!file.isPlaying()) {
      file.play();
    } else {

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

  img2 = loadImage("riddler.jpeg");
  img2.resize(1000, 683);

  img3 = loadImage("rid.png");
  img3.resize(20, 40);

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

  for (int i=0; i<col; i++) {
    for (int j=0; j<row; j++) {
      mymark[i][j] = new mark(color(random(255)), i*40, j*8, random(1, 5));
    }
  }
}

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 > 3) {
    currentScreen = 0;
  }
}

void keyPressedOne() {
  if (keyPressed) {
    if (key == '1') {
      int x = int(random(img.width));
      int y = int(random(img.height));
      noStroke();
      fill(img.get(mouseX, mouseY), 128);
      rect(mouseX, mouseY, 10, 10);
      x+= int (random(-speed, speed));
      y += int (random(-speed, speed));
      color c = img.get(x, y);
      fill(c, 50);
      ellipse(x, y, 20, 20);
    }
  } else {
    background (200);
  }
}

void keyReleasedOne() {

  if (keyPressed) {
    if (key == '2') {
      screen1bool = !screen1bool;
    }
}
}

void keyReleasedTwo() {
  if (keyPressed) {
    if ( key == '3') {
      //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;

      background(255);
      for (int i=0; i<col; i++) {
        for (int j=0; j<row; j++) {
          mymark[i][j].move();
          mymark[i][j].display();
        }
      }
    } 
    if (!file2.isPlaying()) {
      file2.play();
    }
  } else {
    image(img2, 0, 0);
  }
}

```

**it stopped responding after adding to setup()**

---

<div class="post-metadata">

**Author:** ![firetryer](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@firetryer](https://discourse.processing.org/u/firetryer)\
**Post date:** [March 7, 2020, 3:23pm UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/35 "2020-03-07T15:23:47Z")

</div>

```auto
int currentScreen;
int col = 10;
int row = 100;
int speed;
mark[][] mymark = new mark[col][row];

PImage img;
PImage img0;
PImage img1;
PImage img2;
PImage img3;

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

boolean screen1bool = false;

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

  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(20, 40);

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

  for (int i=0; i<col; i++) {
    for (int j=0; j<row; j++) {
      mymark[i][j] = new mark(color(random(255)), i*40, j*8, random(1, 5));
    }
  }
}

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 > 3) {
    currentScreen = 0;
  }
}

void keyPressedOne() {
  if (keyPressed) {
    if (key == '1') {
      int x = int(random(img.width));
      int y = int(random(img.height));
      noStroke();
      fill(img.get(mouseX, mouseY), 128);
      rect(mouseX, mouseY, 10, 10);
      x+= int (random(-speed, speed));
      y += int (random(-speed, speed));
      color c = img.get(x, y);
      fill(c, 50);
      ellipse(x, y, 20, 20);
    }
  } else {
    background (200);
  }
}

void keyReleasedOne() {

  if (keyPressed) {
    if (key == '2') {
      screen1bool = !screen1bool;
      img1.loadPixels();
      for (int x = 0; x < img1.width; x++ ) {
        for (int y = 0; y < img1.height; y++ ) {
          img1.pixels[x + img1.width] = img2.pixels[x];
          img1.pixels[y + img1.height] = img2.pixels[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);
          if (x < mouseX/2)
            c = color(random(0, 255), b, 50);
          if (x < mouseX)
            c = color(r/10+2, b*1.5, g);

          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;
        }
      }
      img1.updatePixels();
      image(img1, 0, 0);
      
    }

    if (!file.isPlaying()) {
      file.play();
    }
  } else {
    image(img0, 0, 0);
  }
}

void keyReleasedTwo() {
  if (keyPressed) {
    if ( key == '3') {
      //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;

      background(255);
      for (int i=0; i<col; i++) {
        for (int j=0; j<row; j++) {
          mymark[i][j].move();
          mymark[i][j].display();
        }
      }
    } 
    if (!file2.isPlaying()) {
      file2.play();
    }
  } else {
    image(img2, 0, 0);
  }
}

```

**It’s working better now, but still not as fine, is there any way I can reset keypresses function?**

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 7, 2020, 4:05pm UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/36 "2020-03-07T16:05:37Z")

</div>

What do you mean by reset?

---

<div class="post-metadata">

**Author:** ![firetryer](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@firetryer](https://discourse.processing.org/u/firetryer)\
**Post date:** [March 7, 2020, 4:06pm UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/37 "2020-03-07T16:06:41Z")

</div>

like it renews after pressing instead running the code again

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 7, 2020, 4:49pm UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/38 "2020-03-07T16:49:24Z")

</div>

Is this something that you can solve when you change your boolean?

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [March 8, 2020, 9:10pm UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/39 "2020-03-08T21:10:33Z")

</div>

@firetryer – When you post, please help us help you by [formatting your code](https://discourse.processing.org/faq#format-your-code) with the `</>` button or by putting ``` above and below your code. I’ll help clean up your past posts now.

---

<div class="post-metadata">

**Author:** ![firetryer](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@firetryer](https://discourse.processing.org/u/firetryer)\
**Post date:** [March 9, 2020, 4:45am UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/40 "2020-03-09T04:45:08Z")

</div>

alright thanks. Will take care in future

[Previous page](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415.md?page=1)
