mouse outside frame
in
Share your Work
•
1 year ago
I had some trouble with it so i post it here for someone else.
The wiki example is broken:
plus it misses the import for the Point library which is a bit unfriendly.
I made it work with insets now so it's probably more friendly when using a different operating system.
And a function to test if it's inside the frame cause that was the main reason i looked into this...
- import java.awt.Point;
- import java.awt.MouseInfo;
- java.awt.Insets insets;
- Point mouse, winloc;
- void setup() {
- size(400, 400);
- frame.pack();
- smooth();
- }
- // . . . . . . . . . . . . . . . . . . .
- void draw() {
- setMouseXY();
- if(mouseInsideFrame()) {
- background(0, 255, 0);
- }
- else {
- background(255, 0, 0);
- }
- println(mouseX+"\t "+mouseY);
- }
- // . . . . . . . . . . . . . . . . . . .
- void setMouseXY()
- {
- insets = frame.getInsets();
- mouse = MouseInfo.getPointerInfo().getLocation();
- winloc = frame.getLocation();
- if(!frame.isUndecorated()){
- winloc.x += insets.left;
- winloc.y += insets.top;
- }
- mouseX = mouse.x-winloc.x;
- mouseY = mouse.y-winloc.y;
- }
- // call this after setMouseXY!
- boolean mouseInsideFrame() {
- if(mouseX >= 0 && mouseX <= width) {
- if(mouseY >= 0 && mouseY <= height) {
- return true;
- }
- }
- return false;
- }