Hi,
After some research, I have a working code :
import java.awt.*;
/**
* From : https://forum.processing.org/two/discussion/17675/how-to-get-surface-window-x-and-y-coordinates
*/
java.awt.Frame getWindowFrame() {
return (java.awt.Frame) ((processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative()).getFrame();
}
/**
* From : https://stackoverflow.com/questions/19185162/how-to-simulate-a-real-mouse-click-using-java/19296065#19296065
*/
void click(int x, int y) throws AWTException {
Robot bot = new Robot();
bot.mouseMove(x, y);
bot.mousePress(java.awt.event.InputEvent.BUTTON1_MASK);
bot.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
}
void setup() {
size(500, 500);
background(255);
}
void draw() {
// Every 50 frames
if (frameCount % 50 == 0) {
try {
java.awt.Frame f = getWindowFrame();
// Get random coordinates on the window
int randomX = int(random(width));
int randomY = int(random(height));
// Click at that location
click(f.getX() + randomX, f.getY() + randomY);
} catch(Exception e) {
println(e);
}
}
}
void mousePressed() {
println("Robot clicked at : " + mouseX + ", " + mouseY);
// Draw a black circle
fill(0);
circle(mouseX, mouseY, 50);
}
Make sure you don’t put a value that is too low for the click interval because the robot is powerful!
You can still press escape if so.
The only problem is that the frame location is the top left corner and don’t take into account the window header.
Useful links :
-
Get window coordinates : How to get surface / window x and y coordinates? - Processing 2.x and 3.x Forum
-
Java doc for the Robot class : Robot (Java Platform SE 7 )
For your second question, this is how Java handles exceptions and in a function, you must catch the exception using a try {...} catch {...}
or use throws
so the function throw the exception higher in the call stack.
Have fun!