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 › drag frame with mouse
Page Index Toggle Pages: 1
drag frame with mouse (Read 570 times)
drag frame with mouse
Feb 14th, 2010, 4:10am
 
Hi all,

when using sth like "frame.setUndecorated(true);" i don't got nothing to move the p5 frame anymore. so i tried to get arround this in making a code like this:

Code:
int pos_x;
int pos_y;

void setup(){
size(300,300);
noStroke();
fill(0,255,0,100);
}

void draw(){
background(255);
//uncomment this in case the frame goes somewhere off the screen:
//frame.setLocation(100,100);
}

void mouseDragged(){
rect(0,0,300,300);

//get x+y position of the frame
int localX = frame.getLocation().x;
int localY = frame.getLocation().y;

//screen x+y movement of mouse
pos_x = localX+(mouseX - pmouseX);
pos_y = localY+(mouseY - pmouseY);

//set new frame possition
frame.setLocation(pos_x,pos_y);
}


now, when you drag the mouse, the frame should be moved. but somehow it is shaking strangely. actually i tried to get rid of this in taking the screen mouse coordinates, but this doesn't work as well.

somebody got an idea whats wrong here?

thanks!
Timm
Re: drag frame with mouse
Reply #1 - Feb 14th, 2010, 5:06am
 
I think the problem is that mouseX/Y are values in relation to the sketch window, so when you move the window you create some weird feedback loop (sorry I've been coding all morning and my brain is frazzled so that's the best explanation I can come up with).

I'm pretty sure what you need is the mousePosition in relation to the screen; though you say you've tried that

I happen to have come across one method of getting this and documented it here.  Using that approach seems to do what you want:

Code:
int pos_x;
int pos_y;
Point mouse;

void setup(){
 size(300,300);
 noStroke();
 fill(0,255,0,100);
}

void draw(){
 background(255);
 //uncomment this in case the frame goes somewhere off the screen:
 //frame.setLocation(100,100);
}

void mouseDragged(){
 rect(0,0,300,300);
 
 mouse = MouseInfo.getPointerInfo().getLocation();  
 //screen x+y movement of mouse
 // you might want to change the offset based on the position of the mouse in relation to the sketch window so it doesn't 'jump' to the centre
 pos_x = mouse.x - width/2;
 pos_y = mouse.y - height/2;
 

 
 //set new frame possition
 frame.setLocation(pos_x,pos_y);
}
Re: drag frame with mouse
Reply #2 - Feb 14th, 2010, 5:16am
 
This works

Code:
int pos_x, pos_y;
int pMouseX;
int pMouseY;

int global_mouse_x, global_mouse_y;
int p_global_mouse_x, p_global_mouse_y;

void setup(){
 size(300,300);
 noStroke();
 fill(0,255,0,100);
}

void draw(){
 background(255);

 //uncomment this in case the frame goes somewhere off the screen:
 //frame.setLocation(100,100);
}

void mousePressed(){
 //calculate screen mouse positions before we drag
 p_global_mouse_x = frame.getLocation().x + mouseX;
 p_global_mouse_y = frame.getLocation().y + mouseY;
}

void mouseDragged(){
 rect(0,0,300,300);

 //get x+y position of the frame
 pos_x = frame.getLocation().x;
 pos_y = frame.getLocation().y;

 //calculate screen mouse positions
 global_mouse_x = pos_x+mouseX;
 global_mouse_y = pos_y+mouseY;

 //screen x+y movement of mouse
 pos_x += (global_mouse_x - p_global_mouse_x);  
 pos_y += (global_mouse_y - p_global_mouse_y);

 //set new frame possition
 frame.setLocation(pos_x,pos_y);

 // Remember the last global position
 p_global_mouse_x = global_mouse_x;  
 p_global_mouse_y = global_mouse_y;
}
Re: drag frame with mouse
Reply #3 - Feb 14th, 2010, 7:14am
 
@blindfish

wow, that works funtastic thanks a lot!!!
Page Index Toggle Pages: 1