If mousePressed, each click results in different event

Hi, so I’m new to processing and I have to create a school project. What I want to do is when I click the first time, an image appears. When I click a second time, the first image is replaces with the second image. I need this to go on for about 7 images/ 7 clicks. How do I do this?

Hi, you can achieve it using a switch function.
You declare the variables which will correspond to your images.

Int image0=0;
Int image1=1;
Int image2=2

Int currenImage =0;
...

Void setup(){}

Void draw(){
Switch (currentImage){
case image0:
//Display you image
break;
Case image1:
//Display image 1
Break;
 }
}

And in your function of mousePressed every time that you pressed the mouse you add 1 to the currentImage like thisif(mousePresed){currentImage+=1; If currentImage > 7){currentImage=0}
Sorry for the bad implementation, I’m on the mobile but this is the main idea, in more hours if you can’t figure out I’ll available, luck!

1 Like

Hi, thanks sooo much! I tried applying what you gave me to the current code I have, but I can’t seem to get it to work. This is the code I have so far. I want the hue behind the images, maybe that’s creating issues with the other code?

PImage flr; //importing image drawn in photoshop
PImage flr2;
PImage flr3;
PImage flr4;
PImage flr5;
PImage flr6;
PImage flr7;
int barWidth = 10;//thinkness of colour bar
int lastBar = 20;

void setup () {
  size (1000, 700);// size of cancvas
  flr = loadImage ("flr.png");// placing image on canvas
  flr2 = loadImage ("flr2.png");
  flr3 = loadImage ("flr3.png");
  flr4 = loadImage ("flr4.png");
  flr5 = loadImage ("flr5.png");
  flr6 = loadImage ("flr6.png");
  flr7 = loadImage ("flr7.png");
  colorMode (RGB, 600, 400, 3);//hue, 
  noStroke ();
  background(0);
}

void draw() {
  int whichBar = mouseX / barWidth;
  if (whichBar != lastBar) {
    int barX = whichBar * barWidth;
    fill(mouseY, height, height);
    rect(barX, 0, barWidth, height);
    lastBar = whichBar;
  }
}
2 Likes

Hi again, i don’t know what you want to do, but here i post what i understand

PImage flr; //importing image drawn in photoshop
PImage flr2;
PImage flr3;
PImage flr4;
PImage flr5;
PImage flr6;
PImage flr7;

final int image0 = 0;
final int image1 = 1;
final int image2 = 2;
final int image3 = 3;
final int image4 = 4;
final int image5 = 5;
final int image6 = 6;

int currentImage = 0;

int barWidth = 10;//thinkness of colour bar
int lastBar = 20;

void setup () {
  size (1000, 700);// size of cancvas
  flr = loadImage ("flr.png");// placing image on canvas
  flr2 = loadImage ("flr1.png");
  /* flr3 = loadImage (“flr3.png”);
   flr4 = loadImage (“flr4.png”);
   flr5 = loadImage (“flr5.png”);
   flr6 = loadImage (“flr6.png”);
   flr7 = loadImage (“flr7.png”);
   */
  colorMode (RGB, 600, 400, 3);//hue,
  noStroke ();
  background(0);
}

void draw() {

  int whichBar = mouseX / barWidth;
  if (whichBar != lastBar) {
    int barX = whichBar * barWidth;
    fill(mouseY, height, height);
    rect(barX, 0, barWidth, height);
    lastBar = whichBar;
  }

  switch(currentImage) {
  case image0:
    image(flr, width/2, height/2);
    break;

  case image1:
    image(flr2, width/2, height/2);
    break;

    //here you put all the 7 cases
  }
  println(currentImage);
}

void mousePressed() {
  currentImage += 1;
  if (currentImage > 6) {
    currentImage = 0;
  }
}

1 Like