Store the coordinates of the mouse clicks

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!


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

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

1 Like

1 Like

// 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);

void draw() {
  background (0); 


  // 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 );

  if (situation==1) {
    stroke(0, 255, 0); 
      new PVector(mouseX, mouseY));

// -------------------------------------------------------------------------
// 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  
  if (situation>1)

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

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

Wow! Great job! Thanks!

1 Like

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