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 › Using an image to link to a URL
Page Index Toggle Pages: 1
Using an image to link to a URL (Read 961 times)
Using an image to link to a URL
Apr 30th, 2007, 12:12am
 
I have an image

image(c, 790, 35);

which I want to link to, say, google.com

Any tips? Thank you!
Re: Using an image to link to a URL
Reply #1 - Apr 30th, 2007, 1:31pm
 
Okay, so far its

 // Icon behaviour
 image(c, 790, 35);
 if (mousePressed == true) {
 link("http://www.processing.org");
 }

But I've no idea how to apply the mousePressed rule to simply the image. Currently clicking anywhere in the project opens the URL.
Re: Using an image to link to a URL
Reply #2 - Apr 30th, 2007, 2:22pm
 
You need to check mouseX and mouseY in your "if" part, to see if the mouse is in the right area.
Re: Using an image to link to a URL
Reply #3 - Apr 30th, 2007, 11:04pm
 
How do I expand the if statement?

Statements like

if (mouseX == 719); (mouseY == 35); (mousePressed == true); {
and
if (mouseX == 719; mouseY == 35; mousePressed == true); {

are producing Syntax errors.

Sorry for all the newbie questions!
Re: Using an image to link to a URL
Reply #4 - Apr 30th, 2007, 11:14pm
 
The right syntax is :

Quote:
if (mouseX == 719 && mouseY == 35 && mousePressed == true){
your code here}


Wink
Re: Using an image to link to a URL
Reply #5 - May 1st, 2007, 12:03am
 
Okay, obviously that code only applies to one pixel which will activate the opening of the link - do I have to create a block to reference instead of the value, or can I map it in the if statement?
Re: Using an image to link to a URL
Reply #6 - May 1st, 2007, 12:11am
 
You can test on ranges within the if statement, and compare a value more than once.. say se if mouseX is greater than one value, but also less than another, e.g.

if(mouseX>790 && mouseX<820 && /*check mouseY and mousePRessed as well ... */)
Re: Using an image to link to a URL
Reply #7 - May 1st, 2007, 12:16am
 
Try this :

Quote:
if(mouseX>=100 && mouseX<=200 && mouseY >= 100 && mouseY <= 200 && your mousePressed/moved/dragged code;)
Re: Using an image to link to a URL
Reply #8 - May 1st, 2007, 12:22am
 
 if (mouseX>=790 && mouseX<=836 && mouseY>=35 && mouseY<=101 && mousePressed == true) {  

fixed it - and I came up with it before the replies! *ischuffed*

The last thing is that mousePressed executes the action so long as the mouse is... well... pressed - is there any way to have it happen only once?
Re: Using an image to link to a URL
Reply #9 - May 1st, 2007, 1:56am
 
i know it's not very clean but it works when you take the mouse from 10 pixels to the picture's edge.

Quote:
PImage img;

void setup(){
 size(640,480);
 background(0);
img = loadImage("myimage.jpg"); // this pic res is 640x480
 }

void draw() {
 if (mouseX>=10 && mouseX<=630 && mouseY>=10 && mouseY<=470 && mousePressed == true) {
 image(img,0,0);}
else if(mouseX<=10 | mouseX>=630 | mouseY<=10 | mouseY>=470) {
background(0);}
}
Re: Using an image to link to a URL
Reply #10 - May 1st, 2007, 8:51am
 
jaylfk wrote on May 1st, 2007, 1:56am:
i know it's not very clean but it works when you take the mouse from 10 pixels to the picture's edge.



No no, by 'action' I meant the opening of the web page - nothing to do with displaying the images. Pressing down on the mouse whilst over the icon opens iterations of the browser for so long as I hold down, but I only want it to open once, regardless of how long my click lasts.
Re: Using an image to link to a URL
Reply #11 - May 1st, 2007, 9:24am
 
You need to use your own boolean for mouse pressed, that you can set to false as soon as you've taken the action. e.g.

Code:
boolean click;

//setup
click=false;

//draw
if(mouseX< /*etc etc*/ && click=true)
{
click=false; //so it only runs once...
//link(.. etc
}

//and also:
void mousePressed()
{
click=true;
//this only runs once when the mouse button is pressed, so will only set click to true once, instead of every frame that the mouse button is held down
}
Page Index Toggle Pages: 1