Making images appear under mousePressed() function

Hi! Very new to processing for my design class, but I’m trying to make an image appear under mousePressed() function. I have six total and I’ve gotten them to load when the mouse is pressed but I want to know how to make them stay when the mouse is released/appear one after the other and not simultaneously. Thank you for any help!

1 Like

Welcome to the forum :smile:

I assume that you are using Java mode so the solution for one image is threefold

  1. create a boolean global variable
boolean showImage = false;
  1. in the draw method test this boolean and display the image if true
if(showImage){ 
   image(img,0,0);
}
  1. in the mouseRelased method swap the state of showImage
void mouseReleased(){
  showImage = !showimage;  // true > false  :::   false >> true
}

Hi

Look at this library

Tips loading image

Welcome to the forum!

It’s always good to show your code so we can spot errors.

My approach

So in the mousePressed () function it makes no sense to show an image since this function is only called once briefly when the mouse is pressed.

So we need to set an integer variable and increase it in mousePressed ().

Before setup () write int imageNumber=0;// def

In mousePressed (): imageNumber++;//inc

in draw() we show the images so they are displayed permanently.

We do so by evaluating imageNumber:

if(imageNumber==0) image(...); // eval 
else if(imageNumber==1) image(...);

etc.

Tip

In mousePressed when imageNumber >5 say
imageNumber=0; //reset

Remark

There is also a variable mousePressed that is inbuilt and is not a function.

Confusingly both have the same name but behaving differently.
In our case we want to use the function. In the reference you can recognize the function by the () because they show you it’s a function and not a variable.

So mousePressed() is the function and
mousePressed is the variable.

Hello @bwowie,

Lots or resources (tutorials, references, examples, etc.) here:

This one is useful and explains events such as mousePressed():
https://processing.org/tutorials/interactivity

I encourage you to look up the reference and an example for each element of your code:

Can you share a snippet of code to show this?
I do not need to see your entire code.

:)