Arslan
Arslan

Reputation: 27

mouse description in processing

I'd like to add a description next to the mouse cursor while passing the mouse over a certain object in my sketch.

void setup ()
{
  size(400,400);
}

void draw()
{
 background(255);
 fill(150);
 rect(100,100,20,20);

 if (mousePressed)
   {

    String text= "The mouse is pressed and over the button";
    fill(0);
    text(text, mouseX+5, mouseY-5);
   }
}

that's the best i could do. What i want is to make the message appear when the mouse is over the button (same coordinates of the rectangular) without pressing, just brushing.

Does anybody know how to do it?

Thanks

Upvotes: 0

Views: 177

Answers (2)

qmateub
qmateub

Reputation: 512

Try this to check the over event on your object:

if(mouseX >= 100 && mouseX <= 100+20 && mouseY >= 100 && mouseY <= 100+20){
    String text= "The mouse is pressed and over the button";
    fill(0);
    text(text, mouseX+5, mouseY-5);
}

Upvotes: 1

Kevin Workman
Kevin Workman

Reputation: 42174

Well, you've got your mouseX and mouseY coordinates.

You've also got the coordinates of your rectangle.

To check whether your mouse is inside your rectangle, you simply check whether your mouseX is in between the left and right side of the rectangle, and you check whether your mouseY is in between the top and bottom of the rectangle. If those are both true, then the mouse is inside the rectangle.

Put that logic into an if statement instead of checking the mousePressed variable.

Upvotes: 1

Related Questions