Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

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

I have been using Processing with java.awt.Robot and java.awt.Dimension to lock the mouse into the center of the screen. robot moves the mouse to relative to to the monitor, I need it to be relative to the window. I need a suggestion for how to get window location or a class like robot that works relative to the window.

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();
          }
        }
}

anything that gets my xmovement and ymovement while disabling the mouse from leaving the program will work

question from:https://stackoverflow.com/questions/65600999/need-a-java-processing-class-for-moving-mouse-to-window-center

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can get the window's top-left location with this renderer-independent method and use that to location the coordinate of the center of the sketch. Note you might have to account for the height of the titlebar.

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...