Receiving error but cannot debug: ArrayIndexOutOfBoundsException

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

2 Likes

No, sorry, I am not at home

Show your attempt please

1 Like
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

1 Like
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

1 Like

Do you have something like a = ! a;

This would toggle a

Give „a“ a better name and make it global

1 Like

I’m sorry, i don’t understand.

1 Like

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) {

}

1 Like
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

1 Like

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

1 Like

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

1 Like

This would toggle on key b

if (keyPressed) {
  if ( key == ‘b’) {
    screen1bool= ! screen1bool;
  }
}
1 Like

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

1 Like
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()

1 Like
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?

1 Like

What do you mean by reset?

1 Like

like it renews after pressing instead running the code again

1 Like

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

1 Like

@firetryer – When you post, please help us help you by formatting your code with the </> button or by putting ``` above and below your code. I’ll help clean up your past posts now.

alright thanks. Will take care in future

2 Likes