# Help with Project on Placing shapes where mouse is at

**URL:** https://discourse.processing.org/t/help-with-project-on-placing-shapes-where-mouse-is-at/2093
**Category:** Coding Questions
**Created:** [July 26, 2018, 3:18am UTC](https://discourse.processing.org/t/help-with-project-on-placing-shapes-where-mouse-is-at/2093 "2018-07-26T03:18:08Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![MrDancingKitty](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@MrDancingKitty](https://discourse.processing.org/u/MrDancingKitty)
#### Post date: [July 26, 2018, 3:18am UTC](https://discourse.processing.org/t/help-with-project-on-placing-shapes-where-mouse-is-at/2093/1 "2018-07-26T03:18:08Z")

</div>

I’m working on a project but for I can’t for the life of me understand why nothing is showing up on the canvas and am running out of patience. Can anybody tell me what’s wrong? The overall project needs buttons on the canvas to click on for a shape. Then clicking anywhere on the canvas will draw said shape. Attached is a drive folder of my code

[https://drive.google.com/open?id=1xHS4C7kPiDO0yATHce0n-PxWPfkMBn2O](https://drive.google.com/open?id=1xHS4C7kPiDO0yATHce0n-PxWPfkMBn2O)

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [July 26, 2018, 3:56am UTC](https://discourse.processing.org/t/help-with-project-on-placing-shapes-where-mouse-is-at/2093/2 "2018-07-26T03:56:44Z")

</div>

This is one of those issues that you can easily overlook, and when I tell you what is wrong you are going to kick yourself.

Your main `draw()` function is mistakenly called `drawShape()`.

---

<div class="post-metadata">

### Author: ![MrDancingKitty](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@MrDancingKitty](https://discourse.processing.org/u/MrDancingKitty)
#### Post date: [July 26, 2018, 4:19am UTC](https://discourse.processing.org/t/help-with-project-on-placing-shapes-where-mouse-is-at/2093/3 "2018-07-26T04:19:26Z")

</div>

OH MY GOD I CANNOT BELIEVE THIS! Thank you so much!

If you don’t mind also helping, the shapes are not being placed where the mouse is at? I’ve had a problem with that and haven’t been able to figure it out now.  
Also, the clear() doesn’t seem to be clearing the canvas.

Thanks for the help! Greatly appreciate it!

---

<div class="post-metadata">

### Author: ![boyad](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/boyad/32/898_2.png) [@boyad](https://discourse.processing.org/u/boyad)
#### Post date: [July 26, 2018, 4:26am UTC](https://discourse.processing.org/t/help-with-project-on-placing-shapes-where-mouse-is-at/2093/4 "2018-07-26T04:26:01Z")

</div>

Use background([color]) to clear the canvas, this can be done every frame or, what would more likely be useful in your case, you could say background(255) when a certain key is pressed to clear all drawn shapes.

---

<div class="post-metadata">

### Author: ![MrDancingKitty](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@MrDancingKitty](https://discourse.processing.org/u/MrDancingKitty)
#### Post date: [July 26, 2018, 4:38am UTC](https://discourse.processing.org/t/help-with-project-on-placing-shapes-where-mouse-is-at/2093/5 "2018-07-26T04:38:44Z")

</div>

I seem to be doing something wrong? No matter where I add the backgroud(255), the shapes don’t clear up. I tried putting it in the ClearAllButton class, the switch case 6, and on the clearShapes()

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [July 26, 2018, 4:40am UTC](https://discourse.processing.org/t/help-with-project-on-placing-shapes-where-mouse-is-at/2093/6 "2018-07-26T04:40:41Z")

</div>

To clear your sketch, you’ll need to remove all the shapes you’ve added to your ArrayList. if you don’t remove them, they’ll just be redrawn again immediately! Essentially, you aren’t clearing the screen so much as removing all the things that you are drawing.

---

<div class="post-metadata">

### Author: ![boyad](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/boyad/32/898_2.png) [@boyad](https://discourse.processing.org/u/boyad)
#### Post date: [July 26, 2018, 4:43am UTC](https://discourse.processing.org/t/help-with-project-on-placing-shapes-where-mouse-is-at/2093/7 "2018-07-26T04:43:33Z")

</div>

Just downloaded and looked at your code (should’ve done it first instead of jumping to conclusions haha). TfGuy is right, clear the array and the shapes won’t get drawn.

EDIT: i looked more at your code, you should store the buttons in a separate array, otherwise every time you empty it you will have to re-add all the buttons.

---

<div class="post-metadata">

### Author: ![MrDancingKitty](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@MrDancingKitty](https://discourse.processing.org/u/MrDancingKitty)
#### Post date: [July 26, 2018, 4:54am UTC](https://discourse.processing.org/t/help-with-project-on-placing-shapes-where-mouse-is-at/2093/8 "2018-07-26T04:54:25Z")

</div>

Thank you! All I need now is to figure out why the shapes aren’t being placed on my mouse position!

---

<div class="post-metadata">

### Author: ![boyad](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/boyad/32/898_2.png) [@boyad](https://discourse.processing.org/u/boyad)
#### Post date: [July 26, 2018, 5:03am UTC](https://discourse.processing.org/t/help-with-project-on-placing-shapes-where-mouse-is-at/2093/9 "2018-07-26T05:03:23Z")

</div>

So, looking at Shape1, you need to add back in the push and pop matrix functions so that the location on your screen where you click is the same as where the vector points to. For example, if you have the canvas rotated 180 degrees and you click at (50, 50) your shape will actually get drawn at (-50, -50).

Next problem I noticed is that you have the abstract class that keeps track of xpos and ypos. Since these are kept track of there you don’t have to reinitialize them in the child class. Infact, becuase of the way processing works it will use the child’s version of xpos instead of the parent’s, which is another contributing factor to the wrong location of the shapes.

If you go through all your shapes and just double check that you’re pushing and popping whenever you are doing a transformation and that you aren’t overriding the variables stores in AbstractShape, everything should work. Best of luck.

---

<div class="post-metadata">

### Author: ![MrDancingKitty](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@MrDancingKitty](https://discourse.processing.org/u/MrDancingKitty)
#### Post date: [July 26, 2018, 5:22am UTC](https://discourse.processing.org/t/help-with-project-on-placing-shapes-where-mouse-is-at/2093/10 "2018-07-26T05:22:28Z")

</div>

Sorry to bother again, but I am having trouble putting hte buttons in a separate array since when I do, they don’t call the shapes to be drawn? I’m pretty sure i’m missing something simple again

---

<div class="post-metadata">

### Author: ![boyad](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/boyad/32/898_2.png) [@boyad](https://discourse.processing.org/u/boyad)
#### Post date: [July 26, 2018, 5:45am UTC](https://discourse.processing.org/t/help-with-project-on-placing-shapes-where-mouse-is-at/2093/11 "2018-07-26T05:45:22Z")

</div>

This is what your main looks like after splitting the shapes and buttons.

```auto
ArrayList<AbstractShape> components = new ArrayList();
ArrayList<AbstractShape> buttons = new ArrayList(); // seperate array for buttons

int selectedButton;

void setup() {
  //fill(255);
  size(600, 600, P3D);
  
  buttons.add(new Shape1Button(10, 10)); // add buttons to new array
  buttons.add(new Shape2Button(10, 30));
  buttons.add(new Shape3Button(10, 50));
  buttons.add(new Shape4Button(10, 70));
  buttons.add(new Shape5Button(10, 90));
  buttons.add(new ClearAllButton(10, 110));
  

}

void draw() {
  background(255);

 // println(components.size());
   
  // Iterate over the list and draw each shape object. 
  for (AbstractShape component : components) {
    component.drawShape();
  }
  
  for (AbstractShape button : buttons) { // draw the buttons
    button.drawShape();
  }
}

/** 
 ^ Add Shape when key is pressed where mouse is positioned
 */
void mousePressed() {

  for (AbstractShape i : buttons) { // removed if statement
      AbstractButton button = (AbstractButton)i;
      if (button.isMouseWithin()) {
        button.onPress();
        return;
      }
  }
  
  AbstractShape as = null;
  switch(selectedButton) {
  case 1:
    as = new Shape1(mouseX, mouseY);
    components.add(as);
    break;
  case 2:
    as = new Shape2(mouseX, mouseY);
    components.add(as);
    break;
  case 3:
    as = new Shape3(mouseX, mouseY);
    components.add(as);
    break;
  case 4:
    as = new Shape4(mouseX, mouseY);
    components.add(as);
    break;
  case 5:
    as = new Shape5(mouseX, mouseY);
    components.add(as);
    break;
  case 6:
    clear();
    break;
  }
}

void clearShapes() {
 clear(); 
}

```

I went through and fixed each object too. Here’s the link with everything working correctly(?) [https://drive.google.com/open?id=1HM2Cudd3YqN6NJvuTd4vLHYFMYvGR\_f5](https://drive.google.com/open?id=1HM2Cudd3YqN6NJvuTd4vLHYFMYvGR_f5)

---

<div class="post-metadata">

### Author: ![MrDancingKitty](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@MrDancingKitty](https://discourse.processing.org/u/MrDancingKitty)
#### Post date: [July 26, 2018, 6:11am UTC](https://discourse.processing.org/t/help-with-project-on-placing-shapes-where-mouse-is-at/2093/12 "2018-07-26T06:11:38Z")

</div>

I managed to fix each object as well with no struggle, it was just the separate array confusing me! But thank you so much for you help, I really appreciate it!
