下面列出了怎么用java.awt.PointerInfo的API类实例代码及写法,或者点击链接到github查看源代码。
/** Handle that this tab was dragged elsewhere, or drag aborted */
private void handleDragDone(final DragEvent event)
{
final DockItem item = dragged_item.getAndSet(null);
if (item != null && !event.isDropCompleted())
{
// Would like to position new stage where the mouse was released,
// but event.getX(), getSceneX(), getScreenX() are all 0.0.
// --> Using MouseInfo, which is actually AWT code
final Stage other = item.detach();
final PointerInfo pi = MouseInfo.getPointerInfo();
if (pi != null)
{
final Point loc = pi.getLocation();
other.setX(loc.getX());
other.setY(loc.getY());
}
}
event.consume();
}
/**
* Returns the current pointer location.
*
* @return The current pointer location.
*/
public static Point getPointerLocation() {
// XXX: Ensure AWT is not considered to be in headless mode, as
// otherwise MouseInfo#getPointerInfo() will not work; therefore
// adjust AWT headless property, if required
String awtHeadlessPropertyValue = System
.getProperty(JAVA_AWT_HEADLESS_PROPERTY);
if (awtHeadlessPropertyValue != null
&& awtHeadlessPropertyValue != Boolean.FALSE.toString()) {
System.setProperty(JAVA_AWT_HEADLESS_PROPERTY,
Boolean.FALSE.toString());
}
// retrieve mouse location
PointerInfo pi = MouseInfo.getPointerInfo();
java.awt.Point mp = pi.getLocation();
// restore AWT headless property
if (awtHeadlessPropertyValue != null) {
System.setProperty(JAVA_AWT_HEADLESS_PROPERTY,
awtHeadlessPropertyValue);
}
return new Point(mp.x, mp.y);
}
/**
* Create a mouse movement event as if the mouse just
* moved to its current position.
* @param c the base swing component to relativize the mouse location
* @return the mouse event
*/
public static UIMouse createCurrent(Component c) {
UIMouse m = new UIMouse();
m.type = UIMouse.Type.MOVE;
PointerInfo pointerInfo = MouseInfo.getPointerInfo();
if (pointerInfo != null) {
Point pm = pointerInfo.getLocation();
Point pc = new Point(0, 0);
try {
pc = c.getLocationOnScreen();
} catch (IllegalStateException ex) {
// ignored
}
m.x = pm.x - pc.x;
m.y = pm.y - pc.y;
}
return m;
}
/**
* 处理鼠标移动
*/
public void mouseMove(String info) {
String args[] = info.split(",");
String x = args[0];
String y = args[1];
float px = Float.valueOf(x);
float py = Float.valueOf(y);
PointerInfo pinfo = MouseInfo.getPointerInfo(); // 得到鼠标的坐标
java.awt.Point p = pinfo.getLocation();
double mx = p.getX(); // 得到当前电脑鼠标的坐标
double my = p.getY();
try {
java.awt.Robot robot = new Robot();
robot.mouseMove((int) (mx + px), (int) (my + py));
} catch (AWTException e) {
}
}
public Location getLocation() {
PointerInfo mp = MouseInfo.getPointerInfo();
if (mp != null) {
return new Location(MouseInfo.getPointerInfo().getLocation());
} else {
Debug.error("Mouse: not possible to get mouse position (PointerInfo == null)");
return null;
}
}
private Point getCurrentCursorLocation() {
Point res = null;
PointerInfo pi = MouseInfo.getPointerInfo();
if( null != pi ) {
res = pi.getLocation();
}
return res;
}
private Rectangle getScreenBounds() {
Rectangle res = null;
PointerInfo pi = MouseInfo.getPointerInfo();
if( null != pi ) {
GraphicsDevice gd = pi.getDevice();
if( gd != null ) {
GraphicsConfiguration gc = gd.getDefaultConfiguration();
if( gc != null ) {
res = gc.getBounds();
}
}
}
return res;
}
/** Workaround for JDK bug 6663119, it ensures that first item in submenu
* is correctly selected during keyboard navigation.
*/
private void ensureSelected () {
if (menu.getMenuComponentCount() <=0) {
return;
}
Component first = menu.getMenuComponent(0);
if (!(first instanceof JMenuItem)) {
return;
}
PointerInfo pointerInfo = MouseInfo.getPointerInfo();
if (pointerInfo == null) {
return; // probably a mouseless computer
}
Point loc = pointerInfo.getLocation();
SwingUtilities.convertPointFromScreen(loc, menu);
MenuElement[] selPath =
MenuSelectionManager.defaultManager().getSelectedPath();
// apply workaround only when mouse is not hovering over menu
// (which signalizes mouse driven menu traversing) and only
// when selected menu path contains expected value - submenu itself
if (!menu.contains(loc) && selPath.length > 0 &&
menu.getPopupMenu() == selPath[selPath.length - 1]) {
// select first item in submenu through MenuSelectionManager
MenuElement[] newPath = new MenuElement[selPath.length + 1];
System.arraycopy(selPath, 0, newPath, 0, selPath.length);
JMenuItem firstItem = (JMenuItem)first;
newPath[selPath.length] = firstItem;
MenuSelectionManager.defaultManager().setSelectedPath(newPath);
}
}
protected Point getMouseLoc() {
// I observed a null PointerInfo after unplugging a second monitor
PointerInfo pointerInfo = MouseInfo.getPointerInfo();
if (pointerInfo == null)
return null;
Point p = pointerInfo.getLocation();
SwingUtilities.convertPointFromScreen(p, zoomedComponent);
return p;
}
public int[] getPosition(int x, int y) {
int[] xy = new int[2];
if (x < 0 && y < 0) {
PointerInfo a = MouseInfo.getPointerInfo();
Point c = graphComponent.getLocationOnScreen();
Point b = a.getLocation();
x = (int)(b.getX() - c.getX());
y = (int)(b.getY() - c.getY());
}
if (x<0) x = 0;
if (y<0) y = 0;
xy[0] = x;
xy[1] = y;
return xy;
}
/**
* Detect the preferred screen by where the mouse is the moment the method
* is called. This is for multi-monitor support.
*
* @return GraphicsEnvironment of the current screen
*/
private static GraphicsConfiguration detectScreen() {
PointerInfo pointer = MouseInfo.getPointerInfo();
if (pointer != null) {
return pointer.getDevice().getDefaultConfiguration();
}
return null;
}
/**
* Returns the current location of the mouse cursor.<br>
* On Windows if the workstation is locked (the login screen is displayed), the mouse location is not available
* and <code>null</code> is returned. Also if a mouse is not present, <code>null</code> is returned.
* @return the current location of the mouse cursor.
*/
private Point getMouseLocation() {
final PointerInfo pointerInfo = MouseInfo.getPointerInfo();
if ( pointerInfo == null ) // Mouse not present?
return null;
final Point location = pointerInfo.getLocation();
// Some big random number is returned if the workstation is locked on Windows
if ( location.x < -20000 || location.x > 20000 || location.y < -20000 || location.y > 20000 )
return null;
return location;
}
@Override public RTCode runDetails(final RTObject myEnclosedScope) {
final RTDynamicScope activationRecord = RunTimeEnvironment.runTimeEnvironment_.currentDynamicScope();
final PointerInfo pointerInfo = MouseInfo.getPointerInfo();
final RTPointerInfoObject retval = new RTPointerInfoObject(pointerInfo);
addRetvalTo(activationRecord);
activationRecord.setObject("ret$val", retval);
return super.nextCode();
}
public void init() {
frame = new JFrame("MHISoft FastCopy " + UI.version);
frame.setContentPane(layoutPanel1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//progressBar1.setVisible(false);
progressBar1.setMaximum(100);
progressBar1.setMinimum(0);
progressPanel.setVisible(false);
//frame.setPreferredSize(new Dimension(1200, 800));
frame.setPreferredSize(new Dimension(UserPreference.getInstance().getDimensionX(), UserPreference.getInstance().getDimensionY()));
frame.pack();
/*position it*/
//frame.setLocationRelativeTo(null); // *** this will center your app ***
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int x = (int) b.getX();
int y = (int) b.getY();
frame.setLocation(x + 100, y);
btnHelp.setBorder(null);
frame.setVisible(true);
componentsList = ViewHelper.getAllComponents(frame);
setupFontSpinner();
ViewHelper.setFontSize(componentsList, UserPreference.getInstance().getFontSize());
}
public RTPointerInfoObject(final PointerInfo pointerInfo) {
super(InterpretiveCodeGenerator.scopeToRTTypeDeclaration(
StaticScope.globalScope().lookupTypeDeclaration("PointerInfo").enclosedScope()));
pointerInfo_ = pointerInfo;
}
public PointerInfoClass(final PointerInfo pointerInfo) {
super((TypeDeclaration)StaticScope.globalScope().lookupTypeDeclaration("PointerInfo"));
}
public PointerInfo pointerInfo() { return pointerInfo_; }