Using mousePressed to load and blend random images from an array

R_BR_B
edited January 2016 in Questions about Code

I am trying to write a code where pressing the mouse shows and blends two random images from an array of 10. If this is possible, I can't really get my head around how to create the array's and call them in draw.

Any pointers would be appreciated!

`//VARIABLES PImage[] photo = new PImage[9]; PImage[] photo2 = new PImage[9];

//SETUP void setup () { size(600,600);

photo[0] = loadImage("Photo1.png"); photo[1] = loadImage("Photo2.png"); photo[2] = loadImage("Photo3.png"); photo[3] = loadImage("Photo4.png"); photo[4] = loadImage("Photo5.png"); photo[5] = loadImage("Photo6.png"); photo[6] = loadImage("Photo7.png"); photo[7] = loadImage("Photo8.png"); photo[8] = loadImage("Photo9.png"); photo[9] = loadImage("Photo10.png"); }

//DRAW void draw() { if(mousePressed) { photo.blend(photo2,0,0,600,600,0,0,600,600,ADD); image(photo,0,0); } }`

Answers

  • Answer ✓
    PImage [] imageArray = new PImage [4];
    
    boolean weShowImage=false;
    int imgIndex1, imgIndex2; 
    PImage photo1; 
    
    void setup() { 
      size(1600, 900); 
      background(150, 150, 150);
    
      imageArray[0] = loadImage("img1.jpg"); 
      imageArray[1] = loadImage("img2.jpg"); 
      imageArray[2] = loadImage("img3.jpg"); 
      imageArray[3] = loadImage("img4.jpg");
    
      frameRate(21);
    }//func
    
    void draw() { 
      background(150, 150, 150);
    
      if (weShowImage) {
        photo1.blend(imageArray[imgIndex2], 0, 0, 600, 600, 0, 0, 600, 600, ADD);
        image(photo1, 0, 0);
      }
    } //func 
    
    void mousePressed() {
      weShowImage=true; 
      imgIndex1 = int(random(imageArray.length));
      imgIndex2 = int(random(imageArray.length));
      photo1=imageArray[imgIndex1].copy();
    }
    
  • Thanks so much Chrisir, don't think I'd have got there alone!

  • you're welcome !

Sign In or Register to comment.