4 images in an array in processingwith swaping pixels

Hello, I am trying to make an animation of 4 images in an array, where they will be swaping pixels on some areas intil all pictures get abstract. But I have problems with my code. I am new to processing ( of course ). I have been watching turtorials on Lynda.com and youtube etc. and some books, as well learned a little bit at school, but I am not the best at it. The current problem that I am getting is the Array Index out of Bounds. I have changed my code so in the part where it was complayining about the error I have changed the width and lengh to number of pixels like 300. But I still get an error. And my memory is at around 300. I had it before at 1050MB. I dont know if I should change it back.... There are probably other errors in my code, so if you see them, please point them out. Hope for your response. Here is my code.

PImage[] utahs = new PImage[3];

boolean pixelMode = false;

int copyWidth = 50;

int copyHeight = 3;

void setup() {

size(600, 600);

smooth();

PImage utah0 = loadImage("utah0.jpg");

PImage utah1 = loadImage("utah1.JPG");

PImage utah2 = loadImage("utah2.JPG");

PImage utah3 = loadImage("utah3.JPG");

utahs[0] = utah0.get(0, 0, 300, 300);

utahs[1] = utah1.get(0, 300, 300, 300);

utahs[2] = utah2.get(300, 0, 300, 300);

utahs[3] = utah3.get(300, 300, 300, 300);

}

void draw() { int x1 = floor( random( width ) );

int y1 = floor( random( height ) );

int x2 = floor( random( width ) );

int y2 = floor( random( height ) );

if ( pixelMode == true ) {

color c1 = utahs[3].get( x1, y1 );

color c2 = utahs[3].get( x2, y2 );

utahs[3].set( x1, y1, c2 );

utahs[3].set( x2, y2, c1 );

} else {

PImage crop1 = utahs[3].get( x1, y1, copyWidth, copyHeight );

PImage crop2 = utahs[3].get( x2, y2, copyWidth, copyHeight );

utahs[3].set( x1, y1, crop2 );

utahs[3].set( x2, y2, crop1 );

}

image(utahs[4], 0, 0);

}

Answers

  • The array if formed by only 3 element, so utah[3] should be the fourth element of the array.

    To be short, if you see how arrays works, you will see that the first element of the array is 0, the second is 1, the third is 2, and so on... you need an array of 4 elements (even if i see a utah[4] and the end of the draw function, so you will need an array of 5 elements).

    So declare an array of 5 elements

    PImage[] utahs = new PImage[5];

  • edited September 2016

    thank you for your response, I was thinking that in an array the first element is 0 that is why i only wrote 3, but I changed everything now to 5, and I am still getting the same error... hmmm

    PImage[] utahs = new PImage[5];
    
    
    boolean pixelMode = false;
    int copyWidth = 50;
    int copyHeight = 3;
    
    
    void setup() {
    
     size(600, 600);
    smooth();
    
    
    PImage utah0 = loadImage("utah0.jpg");
    PImage utah1 = loadImage("utah1.JPG");
    PImage utah2 = loadImage("utah2.JPG");
    PImage utah3 = loadImage("utah3.JPG");
    
      utahs[0] = utah0.get(0, 0, 300, 300);
     utahs[1] = utah1.get(0, 300, 300, 300);
    utahs[2] = utah2.get(300, 0, 300, 300);
    utahs[3] = utah3.get(300, 300, 300, 300);
    
    }
    
    void draw()
    {
      int x1 = floor( random( width ) );
    int y1 = floor( random( height ) );
    int x2 = floor( random( width ) );
    int y2 = floor( random( height ) );
    
      if ( pixelMode == true ) {
    
        color c1 = utahs[5].get( x1, y1 );
        color c2 = utahs[5].get( x2, y2 );
    
        utahs[5].set( x1, y1, c2 );
        utahs[5].set( x2, y2, c1 );
    
      } else {
    
        PImage crop1 = utahs[5].get( x1, y1, copyWidth, copyHeight );
        PImage crop2 = utahs[5].get( x2, y2, copyWidth, copyHeight );
    
        utahs[5].set( x1, y1, crop2 );
        utahs[5].set( x2, y2, crop1 );
      }
    
       image(utahs[5], 0, 0);
    
    }
    
This discussion has been closed.