Processing mousePressed()

582 views Asked by At

I'm new to coding and currently I'm in a class that has us use Processing 3 with java. I'm working on a project trying to set up a mousePressed() action, so that 3 static images appear but it's not showing up. (sorry if this is a stupid question).

Here's the code

PImage [] pics = new PImage [11];

int base=0;
int top=10;
int dollar=9;

boolean notPressed = true;

void setup() {
  size(1200, 750);
  background(255);
  imageMode(CENTER);

  for (int i=0; i<11; i++) {
    pics[i] = loadImage("pic"+i+".png");
  }
}

void draw() {

  translate(500, 275);
  if (notPressed) {
    image(pics[int(random(1, 8))], 100, 100);
  } else {
    image(pics[base], 100, 100);
  }
  image(pics[top], 100, 100);
  image(pics[dollar], 100, mouseY);
}

pushMatrix();
translate(500, 275);
image(pics[int(random(pics.length))], 100, 100);
popMatrix();

pushMatrix(); //moves dollar up and down 
translate(500, 275);
image(pics[0], 100, 100);//base
image(pics[9], 100, mouseY);//dollar
popMatrix();
}

void mousePressed() {  
  notPressed=false;
}

void keyPressed() {
}
3

There are 3 answers

6
Jacob Stuligross On

I suggest using a condition which changes when the mouse is pressed. You can do an if statement, slightly differently from what @YOUSFI Mohamet Walid suggested:

PImage [] pics = new PImage [11];
int base=0;
int top=10;
int dollar=9;

boolean mouseHasNotBeenPressed = true;

void setup() {
  size(1200, 750);
  background(255);
  imageMode(CENTER);

  for (int i=0; i<11; i++) {
    pics[i] = loadImage("pic"+i+".png");
  }
}

void draw() {
  background(255); //reset background after each draw

  translate(500, 275);
  if (mouseHasNotBeenPressed) {//check if mouse has been pressed yet
    image(pics[int(random(1, 8))], 100, 100);
  } else {
    image(pics[base], 100, 100);
  }
  image(pics[top], 100, 100);
  image(pics[dollar], 100, mouseY);
}

void mousePressed() {
  mouseHasNotBeenPressed=false;
}

In this version, there are basically three layers:

  1. The bottom layer shows a random picture with index somewhere between 1 and 8.
  2. The middle layer shows pics[0].
  3. The top layer shows the dollar (pics[9]) at the mouse level.

After the mouse is clicked, the bottom layer no longer picks a random picture to show. Instead, it shows pics[10] every single frame.

4
YOUSFI Mohamed Walid On

from what I understand, here is what you are looking for :

PImage [] pics = new PImage [11];
int base=0;
int top=10;
int dollar=9;

boolean show = false;//new variable to show/hide the images

void setup(){
 size(1200, 750);
 background(255);
 imageMode(CENTER);

for (int i=0; i<11; i++){
  pics[i] = loadImage("pic"+i+".png");
  }
 }
 
void draw() {
 background(255); //reset background after each draw
  
 if(show)//check if we should draw or not
 {
   image(pics[base], 100, 100);
   image(pics[top], 100, 100);
   image(pics[dollar], 100, mouseY);
 }
}

 void mousePressed(){
  show=true;
 }

I added a variable to show/hide the images and removed all the translation/matrix push/pop that had nothing with what you explained

1
Marko On

This was the correct code

boolean notPressed = false;

void setup();
//

void draw();

 if (notPressed) {
    image(pics[int(random(1, 8))], 100, 100);
  } else {
    image(pics[top], 100, 100);
  }
  image(pics[dollar], 100, mouseY);
  image(pics[base], 100, 100);

  void mousePressed();
  notPressed=false;