# Store the coordinates of the mouse clicks

**URL:** <https://discourse.processing.org/t/store-the-coordinates-of-the-mouse-clicks/11745>\
**Category:** Coding Questions\
**Created:** [May 31, 2019, 7:13pm UTC](https://discourse.processing.org/t/store-the-coordinates-of-the-mouse-clicks/11745 "2019-05-31T19:13:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![BallazX](https://avatars.discourse-cdn.com/v4/letter/b/ecae2f/32.png) [@BallazX](https://discourse.processing.org/u/BallazX)\
**Post date:** [May 31, 2019, 7:13pm UTC](https://discourse.processing.org/t/store-the-coordinates-of-the-mouse-clicks/11745/1 "2019-05-31T19:13:58Z")

</div>

Hello All,

I try to draw some shapes and lines. What I’d like to do is draw the shape by clicking on the mouse. E.g: the coordinates of a line should be the mouseX and mouseY. Is there any solution to store the values of the mouse-clicks, so I could put these values into the line() function and draw lines a multiple times. I’m thinking something like this: line(first-clickX-value,first-clickY-value,second-clickX-value,second-clickY-value)

Thanks for your help!

---

<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:** [May 31, 2019, 7:16pm UTC](https://discourse.processing.org/t/store-the-coordinates-of-the-mouse-clicks/11745/2 "2019-05-31T19:16:47Z")

</div>

You could make an Arraylist of type PVector and store the mouse click there

list.add(new PVector(mouseX, mouseY));

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [May 31, 2019, 7:17pm UTC](https://discourse.processing.org/t/store-the-coordinates-of-the-mouse-clicks/11745/3 "2019-05-31T19:17:03Z")

</div>

[Studio.ProcessingTogether.com/sp/pad/export/ro.9c9DCUznTwot6](http://Studio.ProcessingTogether.com/sp/pad/export/ro.9c9DCUznTwot6)

---

<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:** [May 31, 2019, 7:41pm UTC](https://discourse.processing.org/t/store-the-coordinates-of-the-mouse-clicks/11745/4 "2019-05-31T19:41:48Z")

</div>

```auto

// the coordinates of the lines: each line goes "from" a point "to" a point (PVector).

// The lists are parallel in that sense that the elements of both lists together make a line: 
// the 0th element of both lists make one line, the 1st element of both lists make one line, the 2nd etc. 
ArrayList<PVector> from = new ArrayList(); 
ArrayList<PVector> to = new ArrayList();

// necessary to know if the mouse enters a "from" or a "to" point of a line
int situation=0;

// -------------------------------------------------------------------------
// processing core functions  

void setup() {
  size(600, 1000);
  stroke(255);
}

void draw() {
  background (0); 

  stroke(255);

  // show all stored lines 
  for (int i = 0; i < to.size(); i++) {
    PVector currentFrom = from.get(i);
    PVector currentTo = to.get(i);
    // the line 
    linePV ( currentFrom, 
      currentTo );
  }//for

  if (situation==1) {
    stroke(0, 255, 0); 
    linePV(from.get(from.size()-1), 
      new PVector(mouseX, mouseY));
  }
}//func 

// -------------------------------------------------------------------------
// Inputs 

void mousePressed() {
  // depending on situation we store mouse-position as from or as to 
  if (situation==0) 
    from.add (new PVector (mouseX, mouseY));
  else if (situation==1)
    to.add (new PVector (mouseX, mouseY));

  // manage situation variable  
  situation++;
  if (situation>1)
    situation=0;
}

// -------------------------------------------------------------------------
// Tools 

void linePV ( PVector pv1, PVector pv2) {
  line(pv1.x, pv1.y, 
    pv2.x, pv2.y);
}//func 
//

```

---

<div class="post-metadata">

**Author:** ![BallazX](https://avatars.discourse-cdn.com/v4/letter/b/ecae2f/32.png) [@BallazX](https://discourse.processing.org/u/BallazX)\
**Post date:** [May 31, 2019, 7:53pm UTC](https://discourse.processing.org/t/store-the-coordinates-of-the-mouse-clicks/11745/5 "2019-05-31T19:53:28Z")

</div>

Wow! Great job! Thanks!

---

<div class="post-metadata">

**Author:** ![BallazX](https://avatars.discourse-cdn.com/v4/letter/b/ecae2f/32.png) [@BallazX](https://discourse.processing.org/u/BallazX)\
**Post date:** [May 31, 2019, 7:54pm UTC](https://discourse.processing.org/t/store-the-coordinates-of-the-mouse-clicks/11745/6 "2019-05-31T19:54:17Z")

</div>

Thanks a lot! I will start working according to it!
