2. Рецентеринг Вот кусок кода, который это делает:
Код | import java.awt.*; import java.awt.event.*; import javax.swing.SwingUtilities;
public class QuakeLook implements MouseMotionListener { private Robot robot; private Point mouseLocation; private Point centerLocation; private boolean isRecentering;
public void init() { mouseLocation = new Point(); centerLocation = new Point(); imageLocation = new Point(); isRecentering = false;
try { robot = new Robot(); recenterMouse(); mouseLocation.x = centerLocation.x; mouseLocation.y = centerLocation.y; } catch (AWTException ex) { System.out.println("Couldn't create Robot!"); } Window window = //... window.addMouseMotionListener(this); Cursor invisibleCursor = Toolkit.getDefaultToolkit().createCustomCursor( Toolkit.getDefaultToolkit().getImage(""), new Point(0,0), "invisible"); window.setCursor(invisibleCursor); }
private synchronized void recenterMouse() { Window window = screen.getFullScreenWindow(); if (robot != null && window.isShowing()) { centerLocation.x = window.getWidth() / 2; centerLocation.y = window.getHeight() / 2; SwingUtilities.convertPointToScreen(centerLocation, window); isRecentering = true; robot.mouseMove(centerLocation.x, centerLocation.y); } }
public void mouseDragged(MouseEvent e) { mouseMoved(e); }
public synchronized void mouseMoved(MouseEvent e) { if (isRecentering && centerLocation.x == e.getX() && centerLocation.y == e.getY()) { isRecentering = false; } else { int dx = e.getX() - mouseLocation.x; int dy = e.getY() - mouseLocation.y; imageLocation.x += dx; imageLocation.y += dy; recenterMouse(); } mouseLocation.x = e.getX(); mouseLocation.y = e.getY();
}
}
| |