1

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() {
}
Marko
  • 39
  • 5
  • I wanna help you but I need you to explain your need further cause your code doesn't make sense to what you described, where do you want to draw the images? do you want them all at once or with each lick? which of the 11 images loaded you want to draw..etc – YOUSFI Mohamed Walid May 13 '21 at 01:15
  • I just want 3 images(10, 9, and 0) to all show up at once with a click. Sorry for being confusing. I translated them so they all have the same point. Basically what I did is create a copy of the images layered on top, but it'll only flash for a fraction of a second and won't stay. Am I making sense? – Marko May 13 '21 at 01:23
  • show up where? at what coordinates I mean? – YOUSFI Mohamed Walid May 13 '21 at 01:24
  • I want it at (500, 275) – Marko May 13 '21 at 01:27

3 Answers3

2

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

  • Kinda, let me try to explain this again. So images 1-8 flash randomly behind image 0 at the top of the sketch while image 0 remains static and image 9(the dollar) moves up and down on the Y-axis on top of image 0. What I want to do is mousePress and have images 1-8 flashing switch to image 10 while still allowing image 9 to move. I want the sketch to open with the initial images already there. Does that make sense? – Marko May 13 '21 at 01:44
  • Actually a better way of asking I suppose would be, how would I change multiple images to one image when I click my mouse? – Marko May 13 '21 at 02:10
  • @Marko So you want 1-8 to be flashing, with 0 on top of that, and 9 moving up and down on top of everything. Then when the mouse is pressed, show only 10 and 9, 10 being static while 9 continues to move? – Jacob Stuligross May 13 '21 at 02:21
  • I want 1-8 to flash and be replaced with image 10, all other images stay the same because they're already layered properly. sorry for being so confusing. – Marko May 13 '21 at 02:54
1

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.

Jacob Stuligross
  • 1,434
  • 5
  • 18
  • its giving me an error saying I'm mixing active and static modes – Marko May 13 '21 at 03:09
  • @Marko I'm not getting any error messages when I run that exact code, did you perhaps make a mistake copy/pasting? Can you edit your original post to include the exact code you ran? – Jacob Stuligross May 13 '21 at 03:16
  • @Marko you didn't delete some of your code. It should work if you delete lines 30-41. – Jacob Stuligross May 13 '21 at 03:23
  • it ran but its not showing the random images and it doesn't show image 0 and it doesnt execute the mouse press, i switched them around to experiment which gave me the proper layering but no image 10 and heres the code 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); – Marko May 13 '21 at 03:27
  • nevermind I got it thank you so much for your help – Marko May 13 '21 at 03:32
  • 1
    @Marko I'm glad you figured it out! Could you edit my answer or post an answer of your own to give the right solution? It's best to have the answers out here so that others can use them. Also, could you revert your question to how it was before? There should be a rollback button on the edits page. – Jacob Stuligross May 13 '21 at 03:36
0

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;
Marko
  • 39
  • 5
  • I'm really shocked that this works for you. When I run that code, I get all kinds of errors. Why do you have `void setup();` with a semicolon and no body? What is going on here? – Jacob Stuligross May 13 '21 at 06:39