MAYDAY MAYDAY / < ArrayIndexOutOfBoundsException: 3>

please format code with </> button * homework policy * asking questions

int amount = 3;
PImage img1;
int[][] state = new int[amount][amount];
IntList n;
int z;
int b =int(sq(amount)-1);
int x, y;
int tileW = width/amount;
int tileH = height/amount;
int dx, dy;



/void setup() {

  size(900, 900);
  img1 = loadImage("me.jpg");
  img1.resize(width, height);

  n = new IntList();
  for ( int d=1; d<10; d++) {
    n.append(d);
    //println(n);
  }



  for ( x = 0; x< state.length; x++) {
    for ( y = 0; y <state[x].length; y++) {

      b--;
      z =int( random(b));

      state[x][y] =  n.get(z);
      n.remove(z);n.append(z);
      //println(state[x][y]);
      // println(n);
    }
  }
}

void diff() {
  for (int num =0; num<sq(amount); num++) {
    for (int num2 =0; num2< amount; num2++) {
      for (int num3 =0; num3< amount; num3++) {
        if (state[x][y]==num) {
           dx = int(num2*tileW);
           dy = int(num3*tileH);
        }
      }
    }
  }
}


void  draw() {
  background(0);
  //imageMode(CORNER);



  for (int x = 0; x< state.length; x++) {
    for (int y = 0; y <state[x].length; y++) {
      int sh = int(tileW);
      int sw = int(tileH);
      int sx = int(tileW*x);
      int sy = int(tileH*y);
      
      diff();

      int dh = sw;
      int dw = sh;
      copy(img1, sx, sy, sw, sh, dx, dy, dw, dh);
    }
  }
}

//I learn this for a shot time . i want make a picture with 9 part form the same sourse picture . they are in different and specfic place randomly.But now i got < ArrayIndexOutOfBoundsException: 3>
here if (state[y]==num) {
at 46 lines

strong text

Hi,

Welcome to the forum! :wink:

Just before we can help you, can you please format your code by using the </> button or put backticks around your code like this : ```code here``` → code here

Also you can press Ctrl+T in the Processing IDE to auto indent your code.

1 Like

do you mean the line in the function diff()?

x,y is not known there, maybe use num2 and num3?

Hey, Welcome to the forum!

Chrisir

here is your code better formatted (code not working really)




int amount = 3;
PImage img1;
int[][] state = new int[amount][amount];
IntList n;
int z;
int b = int(sq(amount)-1);
int x, y;
int tileW;
int tileH;
int dx, dy;

void setup() {
  size(900, 900);

  tileW = width/amount;
  tileH = height/amount;

  img1 = loadImage("me.jpg");
  img1.resize(width, height);

  n = new IntList();
  for ( int d=1; d<10; d++) {
    n.append(d);
    //println(n);
  }

  for ( x = 0; x< state.length; x++) {
    for ( y = 0; y <state[x].length; y++) {

      b--;
      z =int( random(b));

      state[x][y] =  n.get(z);
      n.remove(z);
      n.append(z);
      //println(state[x][y]);
      // println(n);
    }
  }
}

void draw() {
  background(0);
  //imageMode(CORNER);

  for (int x = 0; x< state.length; x++) {
    for (int y = 0; y <state[x].length; y++) {
      int sh = int(tileW);
      int sw = int(tileH);
      int sx = int(tileW*x);
      int sy = int(tileH*y);

      diff();

      int dh = sw;
      int dw = sh;
      copy(img1, sx, sy, sw, sh, dx, dy, dw, dh);
    }
  }
}

void diff() {
  for (int num =0; num<sq(amount); num++) {
    for (int num2 =0; num2< amount; num2++) {
      for (int num3 =0; num3< amount; num3++) {
        if (state[num2][num3]==num) {
          dx = int(num2*tileW);
          dy = int(num3*tileH);
        }
      }
    }
  }
}
//

OK i think i understand what you mean,My English is kind of bad…

1 Like

thanks for helping me , i see the problem,i will think about it for a while

1 Like

Most browsers allow you to translate languages.

@josephh was politely asking you to format your code.

I am asking also…

Please format the code in your original post:
https://discourse.processing.org/faq#format-your-code

You can use the edit button (looks like a pencil):
image

:)

i tried everyting you told me but nothing happened

nothing is impossible

1 Like

ok done! thats much easier to understand. :rofl:

1 Like