Example usage for java.awt Component getBounds

List of usage examples for java.awt Component getBounds

Introduction

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

Prototype

public Rectangle getBounds() 

Source Link

Document

Gets the bounds of this component in the form of a Rectangle object.

Usage

From source file:Main.java

public static Component getComponentAt(Container parent, Point p) {
    Component comp = null;/*from w w  w  .  j  a va 2  s.co  m*/
    for (Component child : parent.getComponents()) {
        if (child.getBounds().contains(p)) {
            comp = child;
        }
    }
    return comp;
}

From source file:Main.java

public static boolean isInside(Component c, Rectangle r) {
    return r.contains(c.getBounds());
}

From source file:Main.java

/**
 * Returns the center position of the specified component.
 *///from  w  w w . j av  a 2s.c  o m
public static Point2D.Double getCenter(Component c) {
    if (c != null) {
        final Rectangle r = c.getBounds();
        return new Point2D.Double(r.getX() + (r.getWidth() / 2d), r.getY() + (r.getHeight() / 2d));
    }

    return new Point2D.Double(0d, 0d);
}

From source file:Main.java

public static void swingDispatch(MouseEvent e, Point point, final Component component) {
    synchronized (component.getTreeLock()) {
        if (component instanceof Container) {
            Container container = (Container) component;
            for (int i = container.getComponentCount(); i-- != 0;) {
                Component child = container.getComponent(i);
                Rectangle r = child.getBounds();
                if (r.contains(point)) {
                    swingDispatch(e, new Point(point.x - r.x, point.y - r.y), child);
                    return;
                }//w  ww  . jav  a  2  s  . c  om
            }
        }
    }
    final MouseEvent adapted = convertMouseEvent(e, component, point);
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            component.dispatchEvent(adapted);
        }
    });
}

From source file:Main.java

public static MouseEvent adaptEventToDescendent(MouseEvent e, JComponent descendentTarget) {
    Point trans = new Point();
    Component source = e.getComponent();

    Component current = descendentTarget;
    while (current != source) {
        Rectangle b = current.getBounds();
        trans.x += b.x;/*from www .  jav a 2s .c om*/
        trans.y += b.y;
        current = current.getParent();
    }
    Point point = e.getPoint();

    return new MouseEvent(descendentTarget, e.getID(), e.getWhen(), e.getModifiers(), point.x + trans.x,
            point.y + trans.y, e.getClickCount(), e.isPopupTrigger(), e.getButton());
}

From source file:Main.java

/**
 * Centralizar componente/*www . j  a  v a2  s.  c om*/
 * @author marcos
 * @param janela
 */
public static void centralizar(java.awt.Component janela) {
    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    java.awt.Rectangle b = janela.getBounds();
    int x, y;//height, width;

    x = (screenSize.width - b.width) / 2;
    y = (screenSize.height - b.height) / 2;
    janela.setLocation(x, y);
}

From source file:Main.java

/**
 * Returns top component inside the specified container component at the
 * specified point.//from w  ww .j av a  2s .  co m
 *
 * @param component
 *            container component to process
 * @param x
 *            X coordinate
 * @param y
 *            Y coordinate
 * @return top component inside the specified container component at the
 *         specified point
 */
public static Component getTopComponentAt(final Component component, final int x, final int y) {
    final Component child = component.getComponentAt(x, y);
    if (child == component || !(child instanceof Container)) {
        return component;
    } else {
        final Rectangle b = child.getBounds();
        return getTopComponentAt(child, x - b.x, y - b.y);
    }
}

From source file:Main.java

/**
 * Centra una finestra all'interno di un'altra
 *
 * @param f componente target (finestra esterna) - se null centra nello
 * schermo/* w  w w.  j a v  a  2 s .c o  m*/
 * @param c componente da centrare (finestra interna)
 */
public static void centerForm(Component f, Component c) {
    Rectangle rf = new Rectangle();
    ;
    if (f == null) {
        rf = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    } else {
        rf = f.getBounds();
    }
    Rectangle rc = c.getBounds();

    int x = (rf.width - rc.width) / 2;
    if (x < 5) {
        x = 5;
    }
    int y = (rf.height - rc.height) / 2;
    if (y < 5) {
        y = 5;
    }

    if (f == null) {
        c.setLocation(x, y);
    } else {
        c.setLocation(x + f.getX(), y + f.getY());
    }
}

From source file:GUIUtils.java

public static boolean isWithinParent(Component wind) {
    if (wind == null) {
        throw new IllegalArgumentException("Null Component passed");
    }//from ww  w .java2  s.  c om

    Rectangle windowBounds = wind.getBounds();
    Component parent = wind.getParent();
    Rectangle parentRect = null;
    if (parent != null) {
        parentRect = new Rectangle(parent.getSize());
    } else {
        // parentRect = new
        // Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        parentRect = getScreenBoundsFor(windowBounds);
    }

    // if (windowBounds.x > (parentRect.width - 20)
    // || windowBounds.y > (parentRect.height - 20)
    // || (windowBounds.x + windowBounds.width) < 20
    // || (windowBounds.y + windowBounds.height) < 20)
    // {
    // return false;
    // }
    if (windowBounds.x < (parentRect.x - 20) || windowBounds.y < (parentRect.y - 20)) {
        return false;
    }
    return true;
}

From source file:Main.java

/**
 * Returns an appropriate location for a component's tool tip that <i>always</i>
 * lies within the specified frame./*from ww w .  j av a2s .  c om*/
 * <p>
 * Intended be used in custom implementations of {@link JComponent#getToolTipLocation(MouseEvent)}.
 *
 * @param e
 *          the event that caused the display of the tool tip
 * @param c
 *          the parent component of the tool tip
 * @param frame
 *          a component in which the tool tip has to fit (usually the surrounding window of "c")
 * @return
 */
public static Point getAdjustedToolTipLocation(MouseEvent e, JComponent c, Component frame) {
    JToolTip tip = new JToolTip();
    tip.setTipText(c.getToolTipText(e));
    Dimension tipSize = tip.getPreferredSize();
    // Tool tip will be positioned within the bounds of the specified component (+ 5px inset)
    Rectangle frameR = frame.getBounds();
    if (frame instanceof Container) {
        Container container = (Container) frame;
        Insets insets = container.getInsets();
        frameR.x += insets.left;
        frameR.y += insets.top;
        frameR.width -= (insets.left + insets.right);
        frameR.height -= (insets.top + insets.bottom);
    }
    frameR.x += 5;
    frameR.y += 5;
    frameR.width -= 10;
    frameR.height -= 10;
    // Initial try for the tool tip's position
    Rectangle r = new Rectangle(e.getXOnScreen(), c.getLocationOnScreen().y + c.getSize().height + 1,
            tipSize.width, tipSize.height);
    // Check if it fits within the frame
    Rectangle intersection = frameR.intersection(r);
    if (r.equals(intersection)) {
        // Tool tip is fully visible within the frame --> use default behaviour
        //
        // Note: The implementation of ToolTipManager.showTipWindow() is not always
        // correct in dual screen mode. The tool tip is _always_ put on that screen,
        // where the most part of the frame lies upon, even if we return coordinates
        // that clearly belong to the other screen. Unfortunately we cannot change
        // that behavior... (bsh 2010-11-24)
        return null;
    }
    // Otherwise, move the tool tip
    int correction = 0;
    if (r.height == intersection.height) {
        // Height is okay, just move left. To make it look better, position the
        // tip 5px below the component.
        r = new Rectangle(r.x, c.getLocationOnScreen().y + c.getSize().height + 5, tipSize.width,
                tipSize.height);
        correction = -5; // needed to make the ToolTipManager use a lightweight pop-up
    } else {
        // The height does not fit. Position the tool tip above the component.
        r = new Rectangle(c.getLocationOnScreen().x + 10, c.getLocationOnScreen().y - tipSize.height - 1,
                tipSize.width, tipSize.height);
    }
    // Adjust to frame bounds
    intersection = frameR.intersection(r);
    intersection.x -= (r.width - intersection.width);
    intersection.y -= (r.height - intersection.height);
    // Return value is expected to be relative to the component's position
    return new Point((-c.getLocationOnScreen().x) + intersection.x + correction,
            (-c.getLocationOnScreen().y) + intersection.y);
}