# I need your help please (image on / off)

**URL:** <https://discourse.processing.org/t/i-need-your-help-please-image-on-off/26741>\
**Category:** Coding Questions\
**Created:** [January 3, 2021, 9:46pm UTC](https://discourse.processing.org/t/i-need-your-help-please-image-on-off/26741 "2021-01-03T21:46:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PIlw](https://avatars.discourse-cdn.com/v4/letter/p/c5a1d2/32.png) [@PIlw](https://discourse.processing.org/u/PIlw)\
**Post date:** [January 3, 2021, 9:46pm UTC](https://discourse.processing.org/t/i-need-your-help-please-image-on-off/26741/1 "2021-01-03T21:46:01Z")

</div>

Hello guys,  
I would like to do something but I don’t know how :  
The thing would be, when I press a key an image appears and when I press it again it disappears.  
I know that it must be something whit keyPressed, but i don’t know how to combine keypressed and PImage…  
So if someone understood what I am trying to do and can help me I would be very grateful

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 3, 2021, 9:54pm UTC](https://discourse.processing.org/t/i-need-your-help-please-image-on-off/26741/2 "2021-01-03T21:54:48Z")

</div>

use the function keyPressed() please

* * *

set a global variable showImage initially to false. So before setup()

`boolean showImage = false;`

* * *

in the function keyPressed() say something like

```auto
void keyPressed() {
  //toggle
  if (showImage) 
    showImage=false;
  else showImage=true;
}

```

* * *

In draw(): When you display the image say if(showImage) image(…);

Use background(0); in draw()

---

<div class="post-metadata">

**Author:** ![PIlw](https://avatars.discourse-cdn.com/v4/letter/p/c5a1d2/32.png) [@PIlw](https://discourse.processing.org/u/PIlw)\
**Post date:** [January 3, 2021, 11:23pm UTC](https://discourse.processing.org/t/i-need-your-help-please-image-on-off/26741/3 "2021-01-03T23:23:07Z")

</div>

Well i have no doubt about your answer but i’ve made it into my code and the image does’nt shows up  
I put my code here and if someone sees something wrong about it tell me pls ^^

```auto
PImage chaines;
PImage dvd;
float b;
float d = 1;
int speedX = 3;
int speedY = 3;
float x = random(width);
float y = random(height);
bug[] bugs = new bug[3000];
boolean freeze = false;
boolean slow = false;
boolean showImage = false;

color white = (255);
void setup (){
  fullScreen();
  chaines = loadImage ("chaine2.png");

  dvd = loadImage ("dvd.png");

  for (int i = 0; i < bugs.length; i++) {
    bugs[i] = new bug();
  }
}
void draw () { 
background (white);
if (showImage) {
  image (chaines,width/2,height/2); }
if (freeze) { 
  for (int i = 0; i < bugs.length; i++) {
    bugs[i].fall();
    bugs[i].show();
}
    if (slow) {   
  frameRate(30); }
}
     
bouger(); 
couleurs();
}

void couleurs() {
  
b = b + d;
tint (b, 255-b, b);

if (b > 255 || b < 0) { d = d * -1;}
}

void bouger() {

  x = x + speedX;
 y = y + speedY;

if (x < 0 || x > width - 339) { speedX = speedX * -1;} 
if (y < 0 || y > height - 149) { speedY = speedY * -1;}

image (dvd,x,y);
}

void keyPressed() { 

if (key == 'b' || key == 'B') {
  if (freeze) { 
  freeze = false;}
  else { freeze = true;}
 if (slow) {
  slow = !slow; }
}
if (key == 'v') {
  if (showImage) {
    showImage = !showImage; }
}
}

```

And on another outlet

```auto
class bug {
  float x = random(width);
  float y = random (-100,100);
  float speedY = random(5,10);
  float taille = random (50, 70);
  void fall() {
    y = y+speedY;
    
    if (y > height) {
      y = random (-100,-100);
    }
  }
  void show() {
    fill(0);
    line(x,y,x,y+taille);
  }
  }

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 4, 2021, 11:41am UTC](https://discourse.processing.org/t/i-need-your-help-please-image-on-off/26741/5 "2021-01-04T11:41:05Z")

</div>

continued in his next thread
