Java Code Examples for java.awt.MouseInfo#getPointerInfo()

The following examples show how to use java.awt.MouseInfo#getPointerInfo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DockItem.java    From phoebus with Eclipse Public License 1.0 6 votes vote down vote up
/** 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();
}
 
Example 2
Source File: CursorUtils.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 3
Source File: UIMouse.java    From open-ig with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 4
Source File: Main.java    From KJController with Apache License 2.0 6 votes vote down vote up
/**
 * 处理鼠标移动
 */
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) {
    }
}
 
Example 5
Source File: Device.java    From SikuliX1 with MIT License 5 votes vote down vote up
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;
  }
}
 
Example 6
Source File: WindowSnapper.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private Point getCurrentCursorLocation() {
    Point res = null;
    PointerInfo pi = MouseInfo.getPointerInfo();
    if( null != pi ) {
        res = pi.getLocation();
    }
    return res;
}
 
Example 7
Source File: WindowSnapper.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 8
Source File: RecentFileAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** 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);
    }
}
 
Example 9
Source File: MagnificationPanel.java    From pumpernickel with MIT License 5 votes vote down vote up
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;
}
 
Example 10
Source File: Schematic.java    From iBioSim with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 11
Source File: StendhalFirstScreen.java    From stendhal with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 12
Source File: MousePrintRecorder.java    From sc2gears with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 13
Source File: MouseInfoClass.java    From trygve with GNU General Public License v2.0 5 votes vote down vote up
@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();
}
 
Example 14
Source File: FastCopyMainForm.java    From FastCopy with Apache License 2.0 5 votes vote down vote up
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());



}
 
Example 15
Source File: PointerInfoCrashTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static void testMouseInfo() {
    // call the getPointerInfo() before graphics devices initialization
    MouseInfo.getPointerInfo();
}