Java Code Examples for java.awt.Component#getLocationOnScreen()

The following examples show how to use java.awt.Component#getLocationOnScreen() . 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: Util.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void waitTillShownEx(final Component comp) throws InterruptedException {
    while (true) {
        try {
            Thread.sleep(100);
            comp.getLocationOnScreen();
            break;
        } catch (IllegalComponentStateException e) {}
    }
}
 
Example 2
Source File: Util.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void waitTillShownEx(final Component comp) throws InterruptedException {
    while (true) {
        try {
            Thread.sleep(100);
            comp.getLocationOnScreen();
            break;
        } catch (IllegalComponentStateException e) {}
    }
}
 
Example 3
Source File: PaintNativeOnUpdate.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(final String[] args) throws AWTException {
    ExtendedRobot robot = new ExtendedRobot();
    robot.setAutoDelay(50);
    final Frame frame = new Frame();
    final Component label = new PaintNativeOnUpdate();
    frame.setBackground(Color.RED);
    frame.add(label);
    frame.setSize(300, 300);
    frame.setUndecorated(true);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    robot.waitForIdle(1000);
    label.repaint();// first paint
    robot.waitForIdle(1000);
    label.repaint();// incremental paint
    robot.waitForIdle(1000);

    Point point = label.getLocationOnScreen();
    Color color = robot.getPixelColor(point.x + label.getWidth() / 2,
                                      point.y + label.getHeight() / 2);
    if (!color.equals(Color.GREEN)) {
        System.err.println("Expected color = " + Color.GREEN);
        System.err.println("Actual color = " + color);
        throw new RuntimeException();
    }
    frame.dispose();
}
 
Example 4
Source File: Util.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void waitTillShownEx(final Component comp) throws InterruptedException {
    while (true) {
        try {
            Thread.sleep(100);
            comp.getLocationOnScreen();
            break;
        } catch (IllegalComponentStateException e) {}
    }
}
 
Example 5
Source File: PaintNativeOnUpdate.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(final String[] args) throws AWTException {
    final Frame frame = new Frame();
    final Component label = new PaintNativeOnUpdate();
    frame.setBackground(Color.RED);
    frame.add(label);
    frame.setSize(300, 300);
    frame.setUndecorated(true);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    sleep();
    label.repaint();// first paint
    sleep();
    label.repaint();// incremental paint
    sleep();

    Robot robot = new Robot();
    robot.setAutoDelay(50);
    Point point = label.getLocationOnScreen();
    Color color = robot.getPixelColor(point.x + label.getWidth() / 2,
                                      point.y + label.getHeight() / 2);
    if (!color.equals(Color.GREEN)) {
        System.err.println("Expected color = " + Color.GREEN);
        System.err.println("Actual color = " + color);
        throw new RuntimeException();
    }
    frame.dispose();
}
 
Example 6
Source File: DrawImageBilinear.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void doCapture(Component test) {
    try {
        Thread.sleep(2000);
    } catch (InterruptedException ex) {}
    // Grab the screen region
    try {
        Robot robot = new Robot();
        Point pt1 = test.getLocationOnScreen();
        Rectangle rect =
            new Rectangle(pt1.x, pt1.y, test.getWidth(), test.getHeight());
        capture = robot.createScreenCapture(rect);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 7
Source File: GraphicUtils.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
    * Centers the window over a component (usually another window). The window
    * must already have been sized.
    * 
    * @param window
    *            Window to center.
    * @param over
    *            Component to center over.
    */
   public static void centerWindowOnComponent(Window window, Component over) {
if ((over == null) || !over.isShowing()) {
    centerWindowOnScreen(window);
    return;
}

Point parentLocation = over.getLocationOnScreen();
Dimension parentSize = over.getSize();
Dimension size = window.getSize();

// Center it.
int x = parentLocation.x + (parentSize.width - size.width) / 2;
int y = parentLocation.y + (parentSize.height - size.height) / 2;

// Now, make sure it's onscreen
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

// This doesn't actually work on the Mac, where the screen
// doesn't necessarily start at 0,0
if (x + size.width > screenSize.width)
    x = screenSize.width - size.width;

if (x < 0)
    x = 0;

if (y + size.height > screenSize.height)
    y = screenSize.height - size.height;

if (y < 0)
    y = 0;

window.setLocation(x, y);
   }
 
Example 8
Source File: Util.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void waitTillShownEx(final Component comp) throws InterruptedException {
    while (true) {
        try {
            Thread.sleep(100);
            comp.getLocationOnScreen();
            break;
        } catch (IllegalComponentStateException e) {}
    }
}
 
Example 9
Source File: DrawImageBilinear.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static void doCapture(Component test) {
    try {
        Thread.sleep(2000);
    } catch (InterruptedException ex) {}
    // Grab the screen region
    try {
        Robot robot = new Robot();
        Point pt1 = test.getLocationOnScreen();
        Rectangle rect =
            new Rectangle(pt1.x, pt1.y, test.getWidth(), test.getHeight());
        capture = robot.createScreenCapture(rect);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 10
Source File: Util.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public static void waitTillShownEx(final Component comp) throws InterruptedException {
    while (true) {
        try {
            Thread.sleep(100);
            comp.getLocationOnScreen();
            break;
        } catch (IllegalComponentStateException e) {}
    }
}
 
Example 11
Source File: JRobot.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Move mouse cursor to the center of the Component.
 * @param c Component the mouse is placed over
 */
public void moveMouseTo(Component c) {
    Point p = c.getLocationOnScreen();
    Dimension size = c.getSize();
    p.x += size.width / 2;
    p.y += size.height / 2;
    mouseMove(p.x, p.y);
    delay();
}
 
Example 12
Source File: Util.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Moves mouse pointer in the center of given {@code comp} component
 * using {@code robot} parameter.
 */
public static void pointOnComp(final Component comp, final Robot robot) {
    Rectangle bounds = new Rectangle(comp.getLocationOnScreen(), comp.getSize());
    robot.mouseMove(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
}
 
Example 13
Source File: ExtendedModifiersTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private void doTest() throws Exception {

        ArrayList<Component> components = new ArrayList();
        components.add(button);
        components.add(buttonLW);
        components.add(textField);
        components.add(textArea);
        components.add(list);
        components.add(listLW);

        String OS = System.getProperty("os.name").toLowerCase();
        System.out.println(OS);

        for (Component c : components) {

            String className = c.getClass().getName();
            System.out.println("component class : " + className);

            Point origin = c.getLocationOnScreen();
            int xc = origin.x + c.getWidth() / 2;
            int yc = origin.y + c.getHeight() / 2;
            Point center = new Point(xc, yc);

            robot.waitForIdle();
            robot.glide(origin, center);
            robot.click();
            robot.waitForIdle();

            // 1. shift + control
            runScenario(new int[]{KeyEvent.VK_SHIFT, KeyEvent.VK_CONTROL},
                    InputEvent.SHIFT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK);

            // 2. alt + shift + control
            runScenario(new int[]{KeyEvent.VK_ALT, KeyEvent.VK_SHIFT,
                KeyEvent.VK_CONTROL}, InputEvent.ALT_DOWN_MASK
                    | InputEvent.SHIFT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK);

            // 3. shift
            runScenario(new int[]{KeyEvent.VK_SHIFT},
                    InputEvent.SHIFT_DOWN_MASK);

            // 4. alt + control
            runScenario(new int[]{KeyEvent.VK_ALT, KeyEvent.VK_CONTROL},
                    InputEvent.ALT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK);

            // 5. shift + alt
            runScenario(new int[]{KeyEvent.VK_SHIFT, KeyEvent.VK_ALT},
                    InputEvent.SHIFT_DOWN_MASK | InputEvent.ALT_DOWN_MASK);

            if (OS.contains("os x") || OS.contains("sunos")) {
                // 6. meta
                runScenario(new int[]{KeyEvent.VK_META},
                        InputEvent.META_DOWN_MASK);

                // 7. shift + ctrl + alt + meta
                runScenario(new int[]{KeyEvent.VK_SHIFT, KeyEvent.VK_CONTROL,
                    KeyEvent.VK_ALT, KeyEvent.VK_META},
                        InputEvent.SHIFT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK
                        | InputEvent.ALT_DOWN_MASK | InputEvent.META_DOWN_MASK);

                // 8. meta + shift + ctrl
                runScenario(new int[]{KeyEvent.VK_META, KeyEvent.VK_SHIFT,
                    KeyEvent.VK_CONTROL}, InputEvent.META_DOWN_MASK
                      | InputEvent.SHIFT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK);

                // 9. meta + shift + alt
                runScenario(new int[]{KeyEvent.VK_META, KeyEvent.VK_SHIFT,
                    KeyEvent.VK_ALT}, InputEvent.META_DOWN_MASK
                      | InputEvent.SHIFT_DOWN_MASK | InputEvent.ALT_DOWN_MASK);

                // 10. meta + ctrl + alt
                runScenario(new int[]{KeyEvent.VK_META, KeyEvent.VK_CONTROL,
                    KeyEvent.VK_ALT}, InputEvent.META_DOWN_MASK
                      | InputEvent.CTRL_DOWN_MASK | InputEvent.ALT_DOWN_MASK);
            }
        }

        robot.waitForIdle();
        frame.dispose();
    }
 
Example 14
Source File: Util.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Moves mouse pointer in the center of given {@code comp} component
 * using {@code robot} parameter.
 */
public static void pointOnComp(final Component comp, final Robot robot) {
    Rectangle bounds = new Rectangle(comp.getLocationOnScreen(), comp.getSize());
    robot.mouseMove(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
}
 
Example 15
Source File: Util.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Moves mouse pointer in the center of given {@code comp} component
 * using {@code robot} parameter.
 */
public static void pointOnComp(final Component comp, final Robot robot) {
    Rectangle bounds = new Rectangle(comp.getLocationOnScreen(), comp.getSize());
    robot.mouseMove(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
}
 
Example 16
Source File: Util.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Moves mouse pointer in the center of given {@code comp} component
 * using {@code robot} parameter.
 */
public static void pointOnComp(final Component comp, final Robot robot) {
    Rectangle bounds = new Rectangle(comp.getLocationOnScreen(), comp.getSize());
    robot.mouseMove(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
}
 
Example 17
Source File: Util.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Moves mouse pointer in the center of given {@code comp} component
 * using {@code robot} parameter.
 */
public static void pointOnComp(final Component comp, final Robot robot) {
    Rectangle bounds = new Rectangle(comp.getLocationOnScreen(), comp.getSize());
    robot.mouseMove(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
}
 
Example 18
Source File: Util.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Moves mouse pointer in the center of given {@code comp} component
 * using {@code robot} parameter.
 */
public static void pointOnComp(final Component comp, final Robot robot) {
    Rectangle bounds = new Rectangle(comp.getLocationOnScreen(), comp.getSize());
    robot.mouseMove(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
}
 
Example 19
Source File: GraphicUtils.java    From Spark with Apache License 2.0 3 votes vote down vote up
/**
    * Returns a point where the given popup menu should be shown. The point is
    * calculated by adjusting the X and Y coordinates from the given mouse
    * event so that the popup menu will not be clipped by the screen
    * boundaries.
    * 
    * @param popup
    *            the popup menu
    * @param event
    *            the mouse event
    * @return the point where the popup menu should be shown
    */
   public static Point getPopupMenuShowPoint(JPopupMenu popup, MouseEvent event) {
Component source = (Component) event.getSource();
Point topLeftSource = source.getLocationOnScreen();
Point ptRet = getPopupMenuShowPoint(popup,
	topLeftSource.x + event.getX(), topLeftSource.y + event.getY());
ptRet.translate(-topLeftSource.x, -topLeftSource.y);
return ptRet;
   }
 
Example 20
Source File: GraphicUtils.java    From xyTalk-pc with GNU Affero General Public License v3.0 3 votes vote down vote up
/**
    * Returns a point where the given popup menu should be shown. The point is
    * calculated by adjusting the X and Y coordinates from the given mouse
    * event so that the popup menu will not be clipped by the screen
    * boundaries.
    * 
    * @param popup
    *            the popup menu
    * @param event
    *            the mouse event
    * @return the point where the popup menu should be shown
    */
   public static Point getPopupMenuShowPoint(JPopupMenu popup, MouseEvent event) {
Component source = (Component) event.getSource();
Point topLeftSource = source.getLocationOnScreen();
Point ptRet = getPopupMenuShowPoint(popup,
	topLeftSource.x + event.getX(), topLeftSource.y + event.getY());
ptRet.translate(-topLeftSource.x, -topLeftSource.y);
return ptRet;
   }