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 & HelpPrograms › MousePressed on draw. I want just one event.
Page Index Toggle Pages: 1
MousePressed on draw. I want just one event. (Read 1398 times)
MousePressed on draw. I want just one event.
May 10th, 2010, 7:54am
 
I have this code , where if you press the image on the top -left corner , it takes you to processing.com.



Buut the thing is , like it is on draw it  opens many links ,


is there a way to make the mousepressed only act once ???


heres the code :

you need an image called  "javi_img_01.jp" in your folder , just rename any image and put it in the sketch folder.

thanx !!!

Code:
int borde = 20;
int hoover = 5;
int borde_hoover;
PImage b;


void setup(){
size(600,600);
smooth();
background(255);
b = loadImage("javi_img_01.jpg");
}

void draw(){
background(255);
drawR_1(width/2-borde,width/4,height/4);
drawR_2(width/2-borde,width-width/4,height/4);
drawR_3(width/2-borde,width/4,height-height/4);
drawR_4(width/2-borde, width-width/4,height-height/4);
}

void drawR_1( int tamanio, int rx_pos, int yx_pos ){
fill (0);
imageMode(CENTER);
noStroke ();
if(mouseX > borde_hoover && mouseX < width/2-borde_hoover && mouseY > borde && mouseY < height/2-borde_hoover ){
tamanio += hoover ;
} else {
tamanio -=hoover ;
}
image (b , rx_pos, yx_pos , tamanio, tamanio);


// here is link

if (mouseX > borde_hoover && mouseX < width/2-borde_hoover && mouseY > borde && mouseY < height/2-borde_hoover && mousePressed == true ){
link("http://processing.org");
}





}


void drawR_2 ( int tamanio, int rx_pos, int yx_pos ){
fill (0);
rectMode ( CENTER);
noStroke ();
if (mouseX > width/2+borde_hoover && mouseX < width-borde_hoover && mouseY > borde && mouseY < height/2-borde_hoover ){

tamanio += hoover ;
}
else{
tamanio -=hoover ;

}

rect ( rx_pos, yx_pos , tamanio, tamanio);
}

void drawR_3 ( int tamanio, int rx_pos, int yx_pos ){
fill (0);
rectMode ( CENTER);
noStroke ();
if (mouseX > borde && mouseX < width/2-borde_hoover && mouseY > height/2 && mouseY < height-borde_hoover ){

tamanio += hoover ;
}
else{
tamanio -=hoover ;



}

rect ( rx_pos, yx_pos , tamanio, tamanio);
}

void drawR_4 ( int tamanio, int rx_pos, int yx_pos ){
fill (0);
rectMode ( CENTER);
noStroke ();
if (mouseX > width/2+borde_hoover && mouseX < width- borde && mouseY > height/2+borde_hoover && mouseY < height-borde_hoover ){

tamanio += hoover ;
}
else{
tamanio -=hoover ;

}

rect ( rx_pos, yx_pos , tamanio, tamanio);
}
Re: MousePressed on draw. I want just one event.
Reply #1 - May 10th, 2010, 8:05am
 
fixed it.

moved the mousedpressed function out of the drawrec function and this new function :


Code:
void mousePressed() { 
   if (mouseX > borde_hoover && mouseX < width/2-borde_hoover && mouseY > borde && mouseY < height/2-borde_hoover && mousePressed == true ){
 link("http://processing.org");
}



so this would be the new code :


Code:
int borde = 20;
int hoover = 5;
int borde_hoover;
PImage b;


void setup(){
size(600,600);
frameRate (12);
smooth();
background(255);
b = loadImage("javi_img_01.jpg");
}

void draw(){
background(255);
drawR_1(width/2-borde,width/4,height/4);
drawR_2(width/2-borde,width-width/4,height/4);
drawR_3(width/2-borde,width/4,height-height/4);
drawR_4(width/2-borde, width-width/4,height-height/4);
}

void drawR_1( int tamanio, int rx_pos, int yx_pos ){
fill (0);
imageMode(CENTER);
noStroke ();
if(mouseX > borde_hoover && mouseX < width/2-borde_hoover && mouseY > borde && mouseY < height/2-borde_hoover ){
tamanio += hoover ;
} else {
tamanio -=hoover ;
}
image (b , rx_pos, yx_pos , tamanio, tamanio);



}


void drawR_2 ( int tamanio, int rx_pos, int yx_pos ){
fill (0);
rectMode ( CENTER);
noStroke ();
if (mouseX > width/2+borde_hoover && mouseX < width-borde_hoover && mouseY > borde && mouseY < height/2-borde_hoover ){

tamanio += hoover ;
}
else{
tamanio -=hoover ;

}

rect ( rx_pos, yx_pos , tamanio, tamanio);
}

void drawR_3 ( int tamanio, int rx_pos, int yx_pos ){
fill (0);
rectMode ( CENTER);
noStroke ();
if (mouseX > borde && mouseX < width/2-borde_hoover && mouseY > height/2 && mouseY < height-borde_hoover ){

tamanio += hoover ;
}
else{
tamanio -=hoover ;



}

rect ( rx_pos, yx_pos , tamanio, tamanio);
}

void drawR_4 ( int tamanio, int rx_pos, int yx_pos ){
fill (0);
rectMode ( CENTER);
noStroke ();
if (mouseX > width/2+borde_hoover && mouseX < width- borde && mouseY > height/2+borde_hoover && mouseY < height-borde_hoover ){

tamanio += hoover ;
}
else{
tamanio -=hoover ;

}

rect ( rx_pos, yx_pos , tamanio, tamanio);
}





void mousePressed() {
if (mouseX > borde_hoover && mouseX < width/2-borde_hoover && mouseY > borde && mouseY < height/2-borde_hoover && mousePressed == true ){
link("http://processing.org");
}

}
Re: MousePressed on draw. I want just one event.
Reply #2 - May 10th, 2010, 9:56am
 
what about using http://processing.org/reference/mouseReleased_.html ?
Re: MousePressed on draw. I want just one event.
Reply #3 - May 10th, 2010, 11:02am
 
hi cedric ,


do you think there is any beterment in using mousereleased ?



thanx !!!


Re: MousePressed on draw. I want just one event.
Reply #4 - May 10th, 2010, 11:24am
 
By convention with something like a link or button that's pressed with a mouse you perform the action on mouse release.  That way if the user presses down but changes their mind about following the link or pressing the button they can move the mouse away and release without invoking the action...
Re: MousePressed on draw. I want just one event.
Reply #5 - May 10th, 2010, 11:26am
 
allright , yres its true.

so should i just change the function void mousePressed(){  for

void mouseReleased (){

?



Or should i do something else ?

EDIT :


Mousereleased , is just not working if i just change it instead of mousePressed , any ideas ?
Re: MousePressed on draw. I want just one event.
Reply #6 - May 10th, 2010, 11:50am
 
should work. if you look at the examples on the reference page. they are the same.
Re: MousePressed on draw. I want just one event.
Reply #7 - May 10th, 2010, 3:56pm
 
Quote:
void mouseReleased() {
   if (mouseX > borde_hoover && mouseX < width/2-borde_hoover && mouseY > borde && mouseY < height/2-borde_hoover && mousePressed == true ){
 link("http://processing.org");
}


Shocked
Page Index Toggle Pages: 1