We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › save mousecoordinates in an array
Page Index Toggle Pages: 1
save mousecoordinates in an array (Read 792 times)
save mousecoordinates in an array
Nov 6th, 2008, 8:54pm
 
hello

when i click i want to save the mousex and mousey coordinates in an array.

how do I implement it?

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

float x = 0;

void draw() {
   if (mousePressed == true) {
       String[] x = { "mouseX", "mouseY"};
       x = x + 1;

       arraycopy(x);
  }
 
print(1);  
}
Re: save mousecoordinates in an array
Reply #1 - Nov 7th, 2008, 12:30am
 
heres a way, limited and simple.
create an array of ints for x and y positions. allocate memory for them and use the method mousePressed() to get mouse pressed event. it saves only once.

if you would do it inside the mainloop (draw) i believe it would keep saving the mouse point until u released the mouse button. give it a try and you will see for yourself.

// limited size to 100 clicks
int[] xpos;
int[] ypos;
int clickCount;

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

 xpos = new int[ 100 ];
 ypos = new int[ 100 ];
 clickCount = 0;
}

void draw()
{
 // empty
}

void mousePressed()
{
 // if reached limits, just save no more
 if( clickCount >= 100 )
   return;

 xpos[clickCount] = mouseX;
 ypos[clickCount] = mouseY;
 clickCount++;
}
Re: save mousecoordinates in an array
Reply #2 - Nov 7th, 2008, 4:39pm
 
ok thanks. but why is the print function doesn´t work.

// limited size to 100 clicks
int[] xpos;
int[] ypos;
int clickCount;

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

 xpos = new int[ 100 ];
 ypos = new int[ 100 ];
 clickCount = 0;
}

void draw()  
{
 if (mousePressed == true) {
     if( clickCount >= 100 )
     return;

     xpos[clickCount] = mouseX;
     ypos[clickCount] = mouseY;
     clickCount++;


   }
   for (int i = 0; i < 100; i++) {
       println(xpos[i]);
       
   }  
   
}


Re: save mousecoordinates in an array
Reply #3 - Jan 17th, 2009, 9:22pm
 
is it possible to set the limit higher then 100 or without a limit.

100 positions are too little
Re: save mousecoordinates in an array
Reply #4 - Jan 17th, 2009, 9:38pm
 
Here's one way to do it without a limit:


Quote:
ArrayList vectors;

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

  vectors = new ArrayList();
}

void draw()  
{

  for (int i = 0; i < vectors.size(); i++) {
    PVector v= (PVector)vectors.get(i);
    print(" " + (int)v.x);  
  }  
  println("");

}

void mousePressed(){
  vectors.add( new PVector(mouseX, mouseY));
}



Re: save mousecoordinates in an array
Reply #5 - Jan 17th, 2009, 10:28pm
 
thanks.

i want to save the mouse positions to create ellipses.
tehn i use the ellipses for a mask.

is the same possible with your answer?

Code:

int[] xpos;
int[] ypos;
int clickCount;

import JMyron.*;
PGraphics pg;

PGraphics back;
PImage img;
JMyron m;//a camera object

void setup(){
size(320,240);
m = new JMyron();//make a new instance of the object
m.start(width,height);//start a capture at 320x240
loadPixels();

xpos = new int[ 100 ];
ypos = new int[ 100 ];
clickCount = 0;
}

void draw(){
m.update();//update the camera view
m.imageCopy(pixels);//draw image to stage
updatePixels();

//*******************************
// Es wird ein Kreis mit dem Radius 60 x 60px erstellt
// Um den Kreis befindet sich eine Fläche von 320 x 240px mit der Farbe weiß
//*******************************
pg = createGraphics(320, 320, P3D);
pg.beginDraw();
pg.background(255);
pg.fill(0);
pg.noStroke();
pg.ellipse(mouseX, mouseY, 60, 60);
//*******************************
// Wenn die Maus gedrück wird, werden weitere Ellipsen erstellt.
// Momenan liegt das Limit bei 100, da ansonsten das Array zu voll wird
//*******************************
if (mousePressed == true) {
if( clickCount >= 100 )
return;
xpos[clickCount] = mouseX;
ypos[clickCount] = mouseY;
clickCount++;
}

for (int i = 0; i < 100; i++) {
pg.ellipse(xpos[i], ypos[i], 60, 60);
}

pg.endDraw();

//*******************************
// Es wird ein Quadrat mit der Fläche 320 x 240px erstellt
// Das Quadrat hat standartmäßig die farbe weiß
// mit mask() wird eine Maske erstellt
// daraus folgt, dass die bereiche mit unterschiedlichen farben vom quadrat und vom kreis transparent werden
//*******************************
back = createGraphics(320, 320, P3D);
back.rect(0, 0, 320, 240);
back.mask(pg);
image(back,0,0);
}

public void stop(){m.stop(); super.stop();}//stop the camera object

Re: save mousecoordinates in an array
Reply #6 - Jan 17th, 2009, 11:30pm
 
The code is almost equivalent sans 100 point limit, so yes. See if this works:
Quote:
ArrayList vectors;

import JMyron.*;
PGraphics pg;

PGraphics back;
PImage img;
JMyron m;//a camera object

void setup(){
  size(320,240);
  m = new JMyron();//make a new instance of the object
  m.start(width,height);//start a capture at 320x240
  loadPixels();

}

void draw(){
  m.update();//update the camera view
  m.imageCopy(pixels);//draw image to stage
  updatePixels();

  //*******************************
  // Es wird ein Kreis mit dem Radius 60 x 60px erstellt
  // Um den Kreis befindet sich eine FlÃ?che von 320 x 240px mit der Farbe weiß
  //*******************************
  pg = createGraphics(320, 320, P3D);
  pg.beginDraw();
  pg.background(255);
  pg.fill(0);
  pg.noStroke();
  pg.ellipse(mouseX, mouseY, 60, 60);
  //*******************************
  // Wenn die Maus gedrÃ?ck wird, werden weitere Ellipsen erstellt.
  // Momenan liegt das Limit bei 100, da ansonsten das Array zu voll wird
  //*******************************

  for (int i = 0; i < vectors.size(); i++) {
    PVector v= (PVector)vectors.get(i);
    pg.ellipse((int)v.x, (int)v.y, 60, 60);
  }  


  pg.endDraw();

  //*******************************
  // Es wird ein Quadrat mit der FlÃ?che 320 x 240px erstellt
  // Das Quadrat hat standartmÃ?ßig die farbe weiß
  // mit mask() wird eine Maske erstellt
  // daraus folgt, dass die bereiche mit unterschiedlichen farben vom quadrat und vom kreis transparent werden
  //*******************************
  back = createGraphics(320, 320, P3D);
  back.rect(0, 0, 320, 240);
  back.mask(pg);  
  image(back,0,0);
}


void mousePressed(){
  vectors.add( new PVector(mouseX, mouseY));
}


public void stop(){
  m.stop(); 
  super.stop();
}//stop the camera object  





Re: save mousecoordinates in an array
Reply #7 - May 10th, 2009, 1:34pm
 
sw01 wrote on Jan 17th, 2009, 9:38pm:
Here's one way to do it without a limit:


Quote:
ArrayList vectors;

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

  vectors = new ArrayList();
}

void draw()  
{

  for (int i = 0; i < vectors.size(); i++) {
    PVector v= (PVector)vectors.get(i);
    print(" " + (int)v.x);  
  }  
  println("");

}

void mousePressed(){
  vectors.add( new PVector(mouseX, mouseY));
}




thanks

instead of a mouse i use the wii controler via glovepie an osc.
now i search for an alterntive of "void mousePressed".

Code:
 if (b==1) {
vectors.add( new PVector(wii1x, wii1y));
}
doesn´t works

//OSC
import oscP5.*;
import netP5.*;
int wii1x, wii1y, b;
OscP5 oscP5;
NetAddress myRemoteLocation;

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

 vectors = new ArrayList();
   //OSC
 oscP5 = new OscP5(this,3000);
 myRemoteLocation = new NetAddress("127.0.0.1",3000);
}
void oscEvent(OscMessage theOscMessage) {

wii1x = int(map(theOscMessage.get(0).intValue(),0,1000,width,0));
wii1y = int(map(theOscMessage.get(1).intValue(),0,800,0,height));
b = theOscMessage.get(2).intValue();

}

void draw()  
{
background(0);
 for (int i = 0; i < vectors.size(); i++) {
   PVector v= (PVector)vectors.get(i);
   ellipse(v.x,v.y,10,10);  
 }  
 println("");


}

void mousePressed(){
 vectors.add( new PVector(wii1x, wii1y));
} [/code]
Page Index Toggle Pages: 1