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

**URL:** https://discourse.processing.org/t/i-need-your-help-please-image-on-off/26755
**Category:** Coding Questions
**Created:** [January 4, 2021, 12:50am UTC](https://discourse.processing.org/t/i-need-your-help-please-image-on-off/26755 "2021-01-04T00:50:59Z")
**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 4, 2021, 12:50am UTC](https://discourse.processing.org/t/i-need-your-help-please-image-on-off/26755/1 "2021-01-04T00:50:59Z")

</div>

Hello guys,  
I need help please, The thing would be, when I press a key an image appears and when I press it again it disappears.  
i’ve made it into my code and the image doesn’t 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: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [January 4, 2021, 7:01am UTC](https://discourse.processing.org/t/i-need-your-help-please-image-on-off/26755/2 "2021-01-04T07:01:08Z")

</div>

```auto
boolean toggle = false;
void setup() { size(600,600); }
void draw() { 
  background(0);
  if(toggle) {
    rect(200,200,300,300);
  }
}
void keyPressed() {
  if(key == ' ') toggle = !toggle;
}

```

And I suggest you use Ctrl + t to tidy up the code. It is far easier to read that way

---

<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, 7:33am UTC](https://discourse.processing.org/t/i-need-your-help-please-image-on-off/26755/3 "2021-01-04T07:33:35Z")

</div>

> [@PIlw](#):
>
> ```auto
> if (showImage) {
> showImage = !showImage; }
> }
> 
> ```

Not correct

Leave out the if !!!

the assignment is enough: `showImage = ! showImage;`

(I had an if in your last thread but with OTHER assignments…)

Chrisir

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [January 4, 2021, 12:40pm UTC](https://discourse.processing.org/t/i-need-your-help-please-image-on-off/26755/4 "2021-01-04T12:40:21Z")

</div>

Putting together the replies you have so far this code shows how to use the spacebar to toggle and _image_ (I have used a green rectangle but you can change the code)

```auto
boolean showImage = false;

void setup() {
  size(400, 200);
}

void draw() {
  background(255);
  fill(100, 200, 100);
  noStroke();
  if (showImage) {
    // replace this with your own code
    rect(50, 50, 200, 85);
  }
}

void keyTyped() {
  // Toggle 'image' using spacebar
  if (key == ' ') showImage = !showImage;
}

```
