# Reset by pressing key

**URL:** https://discourse.processing.org/t/reset-by-pressing-key/35953
**Category:** Coding Questions
**Created:** [March 26, 2022, 5:18pm UTC](https://discourse.processing.org/t/reset-by-pressing-key/35953 "2022-03-26T17:18:12Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![DomH](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@DomH](https://discourse.processing.org/u/DomH)
#### Post date: [March 26, 2022, 5:18pm UTC](https://discourse.processing.org/t/reset-by-pressing-key/35953/1 "2022-03-26T17:18:12Z")

</div>

Hello,

I am trying to reset my canvas if pressing a key but cannot get it to work. Any ideas?

````auto
int boxsize = 80;
int cols, rows;
ArrayList<Box> boxes = new ArrayList<Box>();
Box active;

public enum Shape {
  TRIANGLE,
    ELLIPSE,
    RECT,
    RESET
}

void mouseClicked() {
  for (Box box : boxes) {
    int x = box.getX()*boxsize;
    int y = box.getY()*boxsize;
    if (mouseX > x && mouseX < (x + boxsize) && mouseY > y && mouseY < (y + boxsize)) {
      switch(box.getShape()) {
      case RESET:
        box.setShape(Shape.RECT);
        break;
      case RECT:
        box.setShape(Shape.TRIANGLE);
        break;
      case TRIANGLE:
        box.setShape(Shape.ELLIPSE);
        break;
      case ELLIPSE:
        box.setShape(Shape.RESET);

        break;
      }
    }
  }
}

void mouseDragged() {
  for (Box box : boxes) {
    int x = box.getX()*boxsize;
    int y = box.getY()*boxsize;
    if (mouseX > x && mouseX < (x + boxsize) && mouseY > y && mouseY < (y + boxsize)) {
      if (active != box) {
        switch(box.getShape()) {
        case RESET:
          box.setShape(Shape.RECT);
          active = box;
          break;
        case RECT:
          box.setShape(Shape.TRIANGLE);
          active = box;
          break;
        case TRIANGLE:
          box.setShape(Shape.ELLIPSE);
          active = box;
          break;
        case ELLIPSE:
          box.setShape(Shape.RESET);
          active = box;
          break;
        }
      }
    }
  }
}

void setup() {
  size(1300, 600);
  cols = width/boxsize;
  rows = height/boxsize;
  for (int i=0; i<cols; i++) {
    for (int j=0; j<rows; j++) {
      boxes.add(new Box(i, j, Shape.RESET));
    }
  }
}

void draw() {
  background(255);
  for (Box box : boxes) {
    int x = box.getX()*boxsize;
    int y = box.getY()*boxsize;
    fill(color(0));
    noStroke();
    switch(box.getShape()) {
    case RESET:
      fill(color(255));
      rect(x, y, boxsize, boxsize);
      break;
    case RECT:
      rect(x, y, boxsize, boxsize);
      break;
    case TRIANGLE:
      //triangle(x+(boxsize/2), y, x+boxsize, y+boxsize, x, boxsize);
      triangle(x, y+boxsize, x+(boxsize/2), y, x+boxsize, y+boxsize);
      break;
    case ELLIPSE:
      ellipse(x + (boxsize/2), y + (boxsize/2), boxsize, boxsize);
      break;
    }
  }
}```
````

---

<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: [March 26, 2022, 7:21pm UTC](https://discourse.processing.org/t/reset-by-pressing-key/35953/2 "2022-03-26T19:21:04Z")

</div>

You have background which is already good

You can say boxes.clear();

Or you can move the section of setup to a new function defineBoxes and call it from setup() and from keyPressed()

```auto
void keyPressed () {
     defineBoxes();
}

```

---

<div class="post-metadata">

### Author: ![DomH](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@DomH](https://discourse.processing.org/u/DomH)
#### Post date: [April 12, 2022, 3:30pm UTC](https://discourse.processing.org/t/reset-by-pressing-key/35953/3 "2022-04-12T15:30:38Z")

</div>

Hey @Chrisir, thanks a lot! It helped me greatly!
