Example usage for java.awt Component getLocationOnScreen

List of usage examples for java.awt Component getLocationOnScreen

Introduction

In this page you can find the example usage for java.awt Component getLocationOnScreen.

Prototype

public Point getLocationOnScreen() 

Source Link

Document

Gets the location of this component in the form of a point specifying the component's top-left corner in the screen's coordinate space.

Usage

From source file:net.chaosserver.timelord.swingui.SwingUtil.java

/**
 * Repair location is designed to detect if a box is partially
 * off-screen and move the box back onto the screen.
 *
 * @param component component to repair//from  w w w .jav a  2 s  .c  om
 */
public static void repairLocation(Component component) {
    Point locationPoint = component.getLocation();
    Point locationOnScreenPoint = null;
    if (component.isVisible()) {
        locationOnScreenPoint = component.getLocationOnScreen();
    }
    Dimension componentSize = component.getSize();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    if (log.isDebugEnabled()) {
        log.debug("Repairing location on [" + component.getClass().getName() + "].  Original location point = ["
                + locationPoint + "] and location on screen point = [" + locationOnScreenPoint
                + "].  The screen size is [" + screenSize + "] and the component size is [" + componentSize
                + "]");
    }

    // Is the dialog to far to the left?  Then fix.
    if (locationPoint.getX() < 0) {
        locationPoint.setLocation(0, locationPoint.getY());
    }
    if (locationPoint.getY() < 0) {
        locationPoint.setLocation(locationPoint.getX(), 0);
    }
    // component.setLocation(locationPoint);

    // Is the dialog too wide?
    if (locationPoint.getX() + componentSize.getWidth() > screenSize.getWidth()) {

        componentSize.width = (int) (screenSize.getWidth() - locationPoint.getX());
    }
    if (locationPoint.getY() + componentSize.getHeight() > screenSize.getHeight()) {

        componentSize.height = (int) (screenSize.getHeight() - locationPoint.getY());
    }

    // component.setSize(componentSize);
}

From source file:Main.java

/**
 * Position the given component at the center of the given parent component or physical screen.
 * //from w  w w.j av  a 2  s .  c  o  m
 * @param c the component to be positioned
 * @param parent the component whose center will match the center of the given component.
 * If null, the given component will match the screen center.
 * 
 */
public static void position(Component c, Component parent) {
    Dimension d = c.getPreferredSize();
    if (parent == null) {
        Dimension s = Toolkit.getDefaultToolkit().getScreenSize();
        c.setLocation(s.width / 2 - d.width / 2, s.height / 2 - d.height / 2);
    } else {
        Point p = parent.getLocationOnScreen();
        int pw = parent.getWidth();
        int ph = parent.getHeight();
        c.setLocation(p.x + pw / 2 - d.width / 2, p.y + ph / 2 - d.height / 2);
    }
}

From source file:Win.java

public static void position(Window frame, Component ref, double xfrac, double yfrac) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension size = frame.getSize();
    Dimension refSize = (ref != null) ? ref.getSize() : screenSize;
    Point origin = (ref != null) ? ref.getLocationOnScreen() : new Point(0, 0);

    int x = origin.x + relativePoint(xfrac, refSize.width, size.width);
    int y = origin.y + relativePoint(yfrac, refSize.height, size.height);

    // make sure frame is entirely on screen
    x = Math.max(0, Math.min(screenSize.width - size.width, x));
    y = Math.max(0, Math.min(screenSize.height - size.height, y));

    frame.setLocation(x, y);// w w  w  .  ja  v a  2  s  .co  m
}

From source file:Main.java

public static void centerComponent(Component relativeTo, Component toCenter) {
    if (relativeTo == null) {
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension screenSize = tk.getScreenSize();
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;
        toCenter.setLocation((screenWidth / 2) - (toCenter.getSize().width / 2),
                (screenHeight / 2) - (toCenter.getSize().height / 2));
    } else {// ww  w .  j a va  2 s  .c  om
        Point loc = relativeTo.getLocationOnScreen();
        Rectangle bounds = relativeTo.getBounds();
        toCenter.setLocation((int) (loc.x + bounds.getWidth() / 2) - (toCenter.getWidth() / 2),
                (int) (loc.y + bounds.getHeight() / 2) - (toCenter.getHeight() / 2));

    }
}

From source file:PNGDecoder.java

/** Static method performing one component screen capture into PNG image format file with given fileName.
 * @param comp Component to be captured/*from  w  w w .j  a  v  a2s .  co  m*/
 * @param fileName String image target filename
 * @param mode image color mode */
public static void captureScreen(Component comp, String fileName, byte mode) {
    captureScreen(new Rectangle(comp.getLocationOnScreen(), comp.getSize()), fileName, mode);
}

From source file:AWTUtilities.java

/**
 * Packs and centers the given window relative to the given component. The
 * specified component may be <code>null</code>, in which case the window will
 * be centered on the screen. The method also makes sure that the target
 * window is fully visible by calling <code>forceToScreen</code>.
 *//* w w  w .  ja v  a2  s.  c o  m*/

public static void centerWindow(Window target, Component parent) {
    target.pack();

    Dimension size = target.getSize();
    Rectangle parentBounds = parent == null || !parent.isShowing() ? getUsableScreenBounds()
            : new Rectangle(parent.getLocationOnScreen(), parent.getSize());

    target.setLocation(parentBounds.x + (parentBounds.width - size.width) / 2,
            parentBounds.y + (parentBounds.height - size.height) / 2);

    forceToScreen(target);
}

From source file:io.github.tavernaextras.biocatalogue.model.Util.java

/**
 * Makes sure that one component (for example, a window) is centered horizontally and vertically
 * relatively to the other component. This is achieved by aligning centers of the two components.
 * //from   ww w  .ja v  a  2s  . co  m
 * This method can be used even if the 'dependentComponent' is larger than the 'mainComponent'. In
 * this case it is probably only useful for centering windows against each other rather than
 * components inside a container.
 * 
 * Method also makes sure that the dependent component will not be placed above the screen's upper
 * edge and to the left of the left edge.
 */
public static void centerComponentWithinAnother(Component mainComponent, Component dependentComponent) {
    int iMainComponentCenterX = (int) Math
            .round(mainComponent.getLocationOnScreen().getX() + (mainComponent.getWidth() / 2));
    int iPosX = iMainComponentCenterX - (dependentComponent.getWidth() / 2);
    if (iPosX < 0)
        iPosX = 0;

    int iMainComponentCenterY = (int) Math
            .round(mainComponent.getLocationOnScreen().getY() + (mainComponent.getHeight() / 2));
    int iPosY = iMainComponentCenterY - (dependentComponent.getHeight() / 2);
    if (iPosY < 0)
        iPosY = 0;

    dependentComponent.setLocation(iPosX, iPosY);
}

From source file:Main.java

public static Window showBelow(Component parent, Window window, int horizontalAlignment) {
    final int DISTANCE = 2;

    if (null == parent)
        throw new IllegalArgumentException("parent null");
    if (null == window)
        throw new IllegalArgumentException("parent null");
    if (!((SwingConstants.LEADING == horizontalAlignment) || (SwingConstants.TRAILING == horizontalAlignment)
            || (SwingConstants.CENTER == horizontalAlignment)
            || (SwingConstants.SOUTH == horizontalAlignment))) {

        throw new IllegalArgumentException("Illegal horizontal alignment " + horizontalAlignment
                + " should be either SwingConstants.LEADING or "
                + "SwingConstants.TRAILING or SwingConstants.CENTER");
    }/* w  ww  . java  2s .  c  o m*/

    window.pack();

    Dimension windowSize = window.getPreferredSize();
    Dimension parentSize = parent.getSize();

    Point loc = parent.getLocationOnScreen();

    int newX;

    if ((SwingConstants.CENTER == horizontalAlignment) || (SwingConstants.SOUTH == horizontalAlignment)) {

        newX = (parentSize.width - windowSize.width) / 2 + loc.x;
    } else if (SwingConstants.TRAILING == horizontalAlignment) {
        newX = loc.x + parentSize.width - windowSize.width;
    } else {
        newX = loc.x;
    }

    window.setLocation(newX, (loc.y + parentSize.height + DISTANCE));

    window.setVisible(true);

    return window;
}

From source file:org.eclipse.jubula.rc.swing.driver.RobotAwtImpl.java

/**
 * Gets a location inside the component. If <code>offset</code> is
 * <code>null</code>, it returns the middle of the component otherwise it
 * adds the offset to the upper left corner. 
 * @param component the component to get the location for
 * @param offset the offset//  ww w .  j  a  v a2  s  .  c o m
 * @throws IllegalArgumentException if <code>component</code> is null
 * @return the <b>global </b> coordinates of <code>component</code>
 */
private Point getLocation(Component component, final Point offset) throws IllegalArgumentException {

    Validate.notNull(component, "component must not be null"); //$NON-NLS-1$ 
    final Component comp = component;
    IRunnable runnable = new IRunnable() {
        public Object run() {
            Point pos = comp.getLocationOnScreen();
            if (offset == null) {
                pos.x += comp.getBounds().width / 2;
                pos.y += comp.getBounds().height / 2;
            } else {
                pos.x += offset.x;
                pos.y += offset.y;
            }
            return pos;
        }
    };
    Point point = null;
    StepExecutionException exc = null;
    int retries = 0;
    do {
        try {
            point = (Point) m_queuer.invokeAndWait("getLocation", runnable); //$NON-NLS-1$
        } catch (StepExecutionException e) {

            List allElements = new ArrayList(ComponentHandler.getAutHierarchy().getHierarchyMap().values());
            List allCheckBoxes = new ArrayList();
            for (int i = 0; i < allElements.size(); i++) {
                if (((SwingHierarchyContainer) allElements.get(i)).getComponentID()
                        .getRealComponent() instanceof JCheckBox) {

                    allCheckBoxes.add(allElements.get(i));
                }
            }
            exc = e;
            retries++;
            log.error("getLocation failed - " + retries); //$NON-NLS-1$
            try {
                Thread.sleep(TimingConstantsServer.GET_LOCATION_RETRY_DELAY);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
        }
    } while (point == null && retries < MAX_RETRIES);
    if (point == null) {
        throw exc;
    }
    return point;
}

From source file:org.eclipse.jubula.rc.swing.driver.RobotAwtImpl.java

/**
 * /*from  w  ww.j av a  2 s .  c o m*/
 * {@inheritDoc}
 */
public boolean isMouseInComponent(Object graphicsComponent) {

    Component comp = (Component) graphicsComponent;
    final Point currMousePos = getCurrentMousePosition();
    if (currMousePos == null) {
        return false;
    }
    final Point treeLocUpperLeft = comp.getLocationOnScreen();
    final Rectangle bounds = comp.getBounds();
    final Point treeLocLowerRight = new Point(bounds.width + treeLocUpperLeft.x,
            bounds.height + treeLocUpperLeft.y);

    final boolean x1 = currMousePos.x >= treeLocUpperLeft.x;
    final boolean x2 = currMousePos.x < treeLocLowerRight.x;
    final boolean y1 = currMousePos.y >= treeLocUpperLeft.y;
    final boolean y2 = currMousePos.y < treeLocLowerRight.y;

    return x1 && x2 && y1 && y2;
}