# Question about mousePressed

**URL:** <https://discourse.processing.org/t/question-about-mousepressed/16142>\
**Category:** Coding Questions\
**Created:** [December 5, 2019, 7:35am UTC](https://discourse.processing.org/t/question-about-mousepressed/16142 "2019-12-05T07:35:46Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![pw00](https://avatars.discourse-cdn.com/v4/letter/p/d9b06d/32.png) [@pw00](https://discourse.processing.org/u/pw00)\
**Post date:** [December 5, 2019, 7:35am UTC](https://discourse.processing.org/t/question-about-mousepressed/16142/1 "2019-12-05T07:35:46Z")

</div>

So I’m trying to figure out how to make one of my images appear and stay ON SCREEN with mousePressed, but it always goes away. Is there a way I can make it stay on screen? (I’ve also tried to do the same with mouseClicked function, but that didn’t work either…)  
Here’s my code…

```auto
PImage img1;
PImage img2;
void setup() {
  size(1000, 800);
  img1=loadImage("img1.png");
  img2=loadImage("img2.png");
}
void draw() {
  background(255);
  imageMode(CENTER);
  image(img1, 500, 600);
}
void mousePressed() {
  image(img2, 450, 550);
}

```

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [December 5, 2019, 7:44am UTC](https://discourse.processing.org/t/question-about-mousepressed/16142/2 "2019-12-05T07:44:40Z")

</div>

Have a boolean that tracks which image should be displayed.

Initially this boolean should be True, meaning you’re showing the first image.

When the mouse is clicked, assign the value of False to the boolean.

Then draw your image dependening on the boolean’s value.

---

<div class="post-metadata">

**Author:** ![pw00](https://avatars.discourse-cdn.com/v4/letter/p/d9b06d/32.png) [@pw00](https://discourse.processing.org/u/pw00)\
**Post date:** [December 5, 2019, 7:52am UTC](https://discourse.processing.org/t/question-about-mousepressed/16142/3 "2019-12-05T07:52:26Z")

</div>

Thank you for the answer!  
EDIT: Is there a way I can make this happen multiple times?

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [December 5, 2019, 8:17am UTC](https://discourse.processing.org/t/question-about-mousepressed/16142/4 "2019-12-05T08:17:34Z")

</div>

Yes. Instead of using a boolean, you can put your images in an array and have an int that is the index of the image you want to display.

```auto
PImage[] images = new PImge[3];
int cur_index = 0;

void setup() {
  size(1000, 800);
  for( int i=0; etc... ){
    images[i] = loadImage( ... );
  }
}
void draw() {
  background(255);
  imageMode(CENTER);
  image(images[cur_index], 500, 600);
}
void mousePressed() {
  cur_index++;
}

```

---

<div class="post-metadata">

**Author:** ![pw00](https://avatars.discourse-cdn.com/v4/letter/p/d9b06d/32.png) [@pw00](https://discourse.processing.org/u/pw00)\
**Post date:** [December 5, 2019, 8:30am UTC](https://discourse.processing.org/t/question-about-mousepressed/16142/5 "2019-12-05T08:30:40Z")

</div>

This is probably a dumb question, but what goes in the ‘etc’ area in:  
`for( int i=0; etc... ){` ?  
Is it

```auto
for( int i=0; i < 3; i++){ 

```

?

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [December 5, 2019, 10:24am UTC](https://discourse.processing.org/t/question-about-mousepressed/16142/6 "2019-12-05T10:24:58Z")

</div>

Could be. Depends on how many images you have!

for( int i = 0; i \< images.length; i++){

Would be a better line.

---

<div class="post-metadata">

**Author:** ![pw00](https://avatars.discourse-cdn.com/v4/letter/p/d9b06d/32.png) [@pw00](https://discourse.processing.org/u/pw00)\
**Post date:** [December 5, 2019, 10:48am UTC](https://discourse.processing.org/t/question-about-mousepressed/16142/7 "2019-12-05T10:48:21Z")

</div>

Thanks for the answers! 👍 I’ve tried it out, but it keeps saying ‘cannot convert int to boolean’

```auto
PImage[] images = new PImage[3];
int cur_index = 0;
void setup() {
  size(1000, 800);
  for( int i=0; images.length; i++ ){
    images[i] = loadImage( "bot.png" );
  }
}
void draw() {
  background(255);
  imageMode(CENTER);
  image(images[cur_index], 500, 600);
}
void mouseClicked() {
  cur_index++;
}

```

What could be the problem?

---

<div class="post-metadata">

**Author:** ![Tiemen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiemen/32/5477_2.png) [@Tiemen](https://discourse.processing.org/u/Tiemen)\
**Post date:** [December 5, 2019, 12:03pm UTC](https://discourse.processing.org/t/question-about-mousepressed/16142/8 "2019-12-05T12:03:14Z")

</div>

The basic for loop structure has three parts, and something crucial is missing in the _test_ part. The answer is well hidden on the [reference page](https://processing.org/reference/for.html) 😉

Did you consider to load three separate images, rather than filling the array with the same image “bot.png”? You can achieve it by adding one line of code inside your for loop and slightly adjusting the other. (ps. let us know if you need a little bit more than that, but it’s good exercise if you try to solve it on your own).

---

<div class="post-metadata">

**Author:** ![pw00](https://avatars.discourse-cdn.com/v4/letter/p/d9b06d/32.png) [@pw00](https://discourse.processing.org/u/pw00)\
**Post date:** [December 5, 2019, 12:24pm UTC](https://discourse.processing.org/t/question-about-mousepressed/16142/9 "2019-12-05T12:24:08Z")

</div>

Embarrassing to say this, but I think I need a little bit more than that…though, I kind of assumed that the array would automatically format the three images since I named it “bot.png, bot1.png, bot2.png”

So far, I have this…

```auto
PImage img=("bot.png","bot1.png","bot2.png");
PImage[] images = new PImage[3];
int cur_index = 0;
void setup() {
  size(1000, 800);
  for( int i=0; i<2; i++ ){
    images[i] = loadImage("bot.png" );
  }
}
void draw() {
  background(255);
  imageMode(CENTER);
  image(images[cur_index], 500, 600);
}
void mouseClicked() {
  cur_index++;
}

```

Though, I’ve also made this as well (even though it’s messier since I didn’t use arrays)

```auto
PImage img1, img2, img3, img4, img5;
boolean image=false;
void setup() {
  size(1000, 800);
  textSize(36);
  img1=loadImage("bag1.png");
  img2=loadImage("bag.png");
  img3=loadImage("bot.png");
  img4=loadImage("bot1.png");
  img5=loadImage("bot2.png");
}
void draw() {
  background(255);
  imageMode(CENTER);
  image(img1, 500, 600);
  image(img3, 300, 600);
  if (image) {
    image(img2, 450, 550);
    image(img4, 250, 550);
    image(img5, 300, 540);
  }
}
void mouseClicked() {
  image=true;
}

```

---

<div class="post-metadata">

**Author:** ![Tiemen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiemen/32/5477_2.png) [@Tiemen](https://discourse.processing.org/u/Tiemen)\
**Post date:** [December 5, 2019, 12:35pm UTC](https://discourse.processing.org/t/question-about-mousepressed/16142/10 "2019-12-05T12:35:14Z")

</div>

> [@pw00](#):
>
> Embarrassing to say this, but I think I need a little bit more than that

Not a problem, and no reason to be embarrassed! I’ll help you get there. You’re already somewhat close with your assumption that it would load “bot1.png” and “bot2.png”. The question now is how to make good use of the for loop, so it loads those exact file names.

Try the following code in a new Processing sketch and observe the output in the command line:

```auto
for (int i = 0; i < 3; i++) {
  println("this is loop number " + i);
}

```

You notice how I use the for loop to construct the output? In a similar manner you could construct file names– use a variable to construct the right file name, and use that variable to load a specific image.

---

<div class="post-metadata">

**Author:** ![pw00](https://avatars.discourse-cdn.com/v4/letter/p/d9b06d/32.png) [@pw00](https://discourse.processing.org/u/pw00)\
**Post date:** [December 7, 2019, 12:10pm UTC](https://discourse.processing.org/t/question-about-mousepressed/16142/11 "2019-12-07T12:10:33Z")

</div>

Sweet! Thanks for this
