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

The following examples show how to use java.awt.Component#getLocationOnScreen() . These examples are extracted from open source projects. 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
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 2
Source Project: jdk8u-jdk   File: Util.java    License: 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 Project: jdk8u-dev-jdk   File: Util.java    License: 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 4
public static void waitTillShownEx(final Component comp) throws InterruptedException {
    while (true) {
        try {
            Thread.sleep(100);
            comp.getLocationOnScreen();
            break;
        } catch (IllegalComponentStateException e) {}
    }
}
 
Example 5
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
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 Project: hottub   File: Util.java    License: 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 8
/**
 * 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 9
Source Project: openjdk-jdk9   File: Util.java    License: 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 10
Source Project: Spark   File: GraphicUtils.java    License: 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 11
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 12
Source Project: jdk8u-jdk   File: Util.java    License: 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 Project: jdk8u-jdk   File: Util.java    License: 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 14
Source Project: TencentKona-8   File: Util.java    License: 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 Project: jdk8u_jdk   File: Util.java    License: 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 Project: openjdk-8   File: Util.java    License: 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 Project: jdk8u60   File: Util.java    License: 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
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 19
Source Project: Spark   File: GraphicUtils.java    License: 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
/**
    * 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;
   }