# If mousePressed, each click results in different event

**URL:** <https://discourse.processing.org/t/if-mousepressed-each-click-results-in-different-event/6285>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [December 4, 2018, 3:30pm UTC](https://discourse.processing.org/t/if-mousepressed-each-click-results-in-different-event/6285 "2018-12-04T15:30:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Missmew72](https://avatars.discourse-cdn.com/v4/letter/m/41988e/32.png) [@Missmew72](https://discourse.processing.org/u/Missmew72)\
**Post date:** [December 4, 2018, 3:30pm UTC](https://discourse.processing.org/t/if-mousepressed-each-click-results-in-different-event/6285/1 "2018-12-04T15:30:46Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![bryan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bryan/32/2538_2.png) [@bryan](https://discourse.processing.org/u/bryan)\
**Post date:** [December 4, 2018, 3:43pm UTC](https://discourse.processing.org/t/if-mousepressed-each-click-results-in-different-event/6285/2 "2018-12-04T15:43:03Z")

</div>

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

```auto
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 this`if(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!

---

<div class="post-metadata">

**Author:** ![Missmew72](https://avatars.discourse-cdn.com/v4/letter/m/41988e/32.png) [@Missmew72](https://discourse.processing.org/u/Missmew72)\
**Post date:** [December 4, 2018, 4:24pm UTC](https://discourse.processing.org/t/if-mousepressed-each-click-results-in-different-event/6285/3 "2018-12-04T16:24:15Z")

</div>

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?

```auto
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;
  }
}

```

---

<div class="post-metadata">

**Author:** ![bryan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bryan/32/2538_2.png) [@bryan](https://discourse.processing.org/u/bryan)\
**Post date:** [December 5, 2018, 2:53am UTC](https://discourse.processing.org/t/if-mousepressed-each-click-results-in-different-event/6285/4 "2018-12-05T02:53:58Z")

</div>

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

```auto
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;
  }
}

```
