try
{
int button =
rightClick ? InputEvent.BUTTON3_MASK : InputEvent.BUTTON1_MASK;
Robot robot = new Robot();
robot.mouseMove(p.x, p.y);
while (display.readAndDispatch());
try { Thread.sleep(20); } catch (InterruptedException _) { }
robot.mousePress(button);
while (display.readAndDispatch());
try { Thread.sleep(20); } catch (InterruptedException _) { }
robot.mouseRelease(button);
while (display.readAndDispatch());
try { Thread.sleep(20); } catch (InterruptedException _) { }
}
catch (AWTException e) { throw new RuntimeException(e); }
}