Mirror video doodles. ArrayIndexOutOfBoundsException

While I’m studying the book ‘Learning processing’, written by Daniel Shiffman, there’s keep error on my code.

import processing.video.*;
Capture video;
float x;
float y;

void setup() {
  size(352, 288);
  x = width/2;
  y = width/2;
  video = new Capture(this, width, height);

void captureEvent(Capture video) {

void draw() { 
  float nex = constrain(x + random(-20, 20), 0, video.width-1);
  float ney = constrain(y + random(-20, 20), 0, video.height-1);

  int midx = int((x+nex)/2);
  int midy = int((y+nex)/2);
  println((video.width - midx - 1) + midy * video.width);
  color c = video.pixels[(video.width - midx - 1) + midy * video.width];

  line(x, y, nex, ney);

  x = nex;
  y = ney;

it just keep telling me a limited array number and also the number keep changes.

could you help me?
you can check this code on the book that I mentioned chapter 16 video part.

1 Like

Welcome Elom,

Haven’t tested your code, but I guess the ArrayIndexOutOfBoundException occurs because you’re asking for a pixel that isn’t there. Your window size is 352 by 288 pixels, meaning there are 101.376 pixels in total.

1 Like

Pretty sure you meant to multiply midy by video.height?

Also not sure you can assume the video pixels are not empty until the first frame is received.

1 Like