需要一个 java-processing class 来将鼠标移动到 window 中心

need a java-processing class for moving mouse to window center

我一直在使用 java.awt.Robot 和 java.awt.Dimension 的 Processing 来将鼠标锁定在屏幕中央。机器人将鼠标移动到相对于显示器,我需要它相对于 window。我需要有关如何获取 window 位置或相对于 window.

工作的类似 class 的机器人的建议
void MouseLock() {
  
  if(mouseX != screensizex/2 || mouseY != screensizey/2) {
    xmovement =  (mouseX - screensizex/2);
    Ymovement =  (mouseY - screensizey/2);
          try {
            Robot screenWin = new Robot();
            screenWin.mouseMove((int)screensizex/2, (int)screensizey/2);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
}

在禁止鼠标离开程序的同时获取我的 xmovement 和 ymovement 的任何东西都可以工作

您可以使用此 renderer-independent 方法获取 window 的 top-left 位置,并使用它来定位草图中心的坐标。请注意,您可能需要考虑标题栏的高度。

PVector getWindowLocation() {

  PVector windowLocation = new PVector();

  switch (sketchRenderer()) {
  case P2D:
  case P3D:
    com.jogamp.nativewindow.util.Point p = new com.jogamp.nativewindow.util.Point();
    ((com.jogamp.newt.opengl.GLWindow) surface.getNative()).getLocationOnScreen(p);
    windowLocation.x = p.getX();
    windowLocation.y = p.getY();
    break;
  case FX2D:
    final processing.javafx.PSurfaceFX FXSurface = (processing.javafx.PSurfaceFX) surface;
    final javafx.scene.canvas.Canvas canvas = (javafx.scene.canvas.Canvas) FXSurface.getNative();
    final javafx.stage.Stage stage = (javafx.stage.Stage) canvas.getScene().getWindow();
    windowLocation.x = (float) stage.getX();
    windowLocation.y = (float) stage.getY();
    break;
  case JAVA2D:
    java.awt.Frame f = (java.awt.Frame) ((processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative())
        .getFrame();
    windowLocation.x = f.getX();
    windowLocation.y = f.getY();
    break;
  }

  return windowLocation;
}