Reputation: 2147
I'd like to write a function that returns true
if the mouse is over the canvass, and false
if not.
My solution would basically look like this:
import java.awt.MouseInfo;
Point globalMouse;
boolean mouseOverCanvass() {
globalMouse = MouseInfo.getPointerInfo().getLocation();
boolean mouseInXRange = (canvass.X < globalMouse.X) && (globalMouse.X < canvass.X + width);
boolean mouseInYRange = (canvass.Y < globalMouse.Y) && (globalMouse.Y < canvass.Y + height);
if (mouseInXRange && mouseInYRange) {
return true;
} else {
return false;
}
}
The problem is that I can't find the canvass position. I found this solution to getting the location of the window, but this provides the coordinate including the top bar, whereas I'd like the location of the canvass within that window.
Upvotes: 1
Views: 188
Reputation: 3810
Override the mouseEntered() and mouseExited() methods provided by PApplet, using a boolean variable to keep track of the current mouse-over-canvas state:
boolean mouse_over = false;
@Override
public void mouseEntered() {
mouse_over = true;
}
@Override
public void mouseExited() {
mouse_over = false;
}
Upvotes: 3