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 Point getPositionWithinWindow(Component component, Component parent, Point p) {
    Point[] pointCheck = new Point[] { (Point) p.clone(), (Point) p.clone(), (Point) p.clone(),
            (Point) p.clone() };/*w  w  w.ja  va 2  s  .c  o m*/
    int w = component.getWidth();
    int h = component.getHeight();
    pointCheck[0].translate(w, h);
    pointCheck[1].translate(0, h);
    pointCheck[2].translate(w, 0);
    pointCheck[3].translate(0, 0);
    for (Point p2 : pointCheck) {
        if (parent.getBounds().contains(p2)) {
            p2.translate(-w, -h);
            return p2;
        }
    }
    return p;
}

From source file:org.esa.nest.dat.views.polarview.PolarCanvas.java

private static void paintComponents(Container c, Graphics g) {
    if (!c.isShowing())
        return;//  ww  w .ja va 2  s .  c  o  m

    final int ncomponents = c.getComponentCount();
    final Rectangle clip = g.getClipBounds();

    int i = ncomponents - 1;
    while (i >= 0) {
        final Component component[] = c.getComponents();
        final Component comp = component[i];
        if (comp == null || !comp.isVisible())
            continue;
        final Rectangle bounds = comp.getBounds();
        Rectangle cr;
        if (clip == null)
            cr = new Rectangle(bounds);
        else
            cr = bounds.intersection(clip);
        if (cr.isEmpty())
            continue;

        final Graphics cg = g.create();
        cg.setClip(cr);
        cg.translate(bounds.x, bounds.y);
        try {
            comp.paint(cg);
        } catch (Throwable e) {
            //
        }

        cg.dispose();
        i--;
    }
}

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 {/*from  w  ww. j a  v a2  s  .c  o m*/
        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:org.eclipse.wb.internal.swing.utils.SwingImageUtils.java

private static void fetchMenuVisualData_items(MenuVisualData menuData, Container menuObject) {
    menuData.m_itemBounds = Lists.newArrayList();
    for (Component menuComponent : menuObject.getComponents()) {
        menuData.m_itemBounds.add(CoordinateUtils.get(menuComponent.getBounds()));
    }/*from   w ww.j  av a2 s .  c o m*/
}

From source file:org.pentaho.reporting.designer.core.actions.global.ScreenCaptureAction.java

public static void saveScreenShot(final int modifiers) {
    final Component component = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
    final GraphicsConfiguration graphicsConfiguration = component.getGraphicsConfiguration();
    final GraphicsDevice graphicsDevice = graphicsConfiguration.getDevice();
    try {//from w  ww.j  a  v  a2  s  .c o  m
        final Robot robot = new Robot(graphicsDevice);
        final BufferedImage image;
        if ((modifiers & ActionEvent.SHIFT_MASK) == ActionEvent.SHIFT_MASK) {
            image = robot.createScreenCapture(graphicsConfiguration.getBounds());
        } else {
            image = robot.createScreenCapture(component.getBounds());
        }

        final String homeDirectory = ReportDesignerBoot.getInstance().getGlobalConfig()
                .getConfigProperty("user.home", ".");
        final File homeDir = new File(homeDirectory);
        final File f = generateName(homeDir);
        if (f == null) {
            return;
        }
        final FileOutputStream fout = new FileOutputStream(f);
        try {
            final PngEncoder encoder = new PngEncoder();
            encoder.setCompressionLevel(6);
            encoder.setEncodeAlpha(false);
            encoder.setImage(image);
            final byte[] bytes = encoder.pngEncode();
            fout.write(bytes);
        } finally {
            fout.close();
        }
    } catch (IOException ioe) {
        UncaughtExceptionsModel.getInstance().addException(ioe);
    } catch (AWTException e1) {
        // ignore
        UncaughtExceptionsModel.getInstance().addException(e1);
    }
}

From source file:FunLayout.java

public Dimension preferredLayoutSize(Container con) {
    Component comp;
    Rectangle rect;//from  w w w . j a  v a  2  s .c o  m
    int i, count;
    Dimension d;

    d = new Dimension(0, 0);
    count = con.countComponents();
    for (i = 0; i < count; i++) {
        comp = con.getComponent(i);
        if (!comp.isVisible())
            continue;
        rect = comp.getBounds();
        if (d.width < rect.x + rect.width)
            d.width = rect.x + rect.width;
        if (d.height < rect.y + rect.height)
            d.height = rect.y + rect.height;
    }
    return d;
}

From source file:org.eclipse.jubula.rc.swing.tester.adapter.JComboBoxAdapter.java

/**
 * @return a rectangle, where the arrow icon is expected.
 *//*w ww .j a  v a  2  s. c  om*/
private Rectangle findArrowIconArea() {
    JComboBox comboBox = m_comboBox;
    Component editor = getComboBoxEditorComponent(comboBox);
    Rectangle r = null;
    if (editor == null) {
        throw new StepExecutionException("could not find editor", //$NON-NLS-1$
                EventFactory.createActionError(TestErrorEvent.COMP_NOT_FOUND));
    }
    Rectangle ra[] = SwingUtilities.computeDifference(comboBox.getBounds(), editor.getBounds());
    if ((ra == null) || (ra.length < 1)) {
        throw new StepExecutionException("could not arrow icon", //$NON-NLS-1$
                EventFactory.createActionError(TestErrorEvent.COMP_NOT_FOUND));
    }
    r = ra[0];
    // find the largest area of the returned rectangles.
    double bestAreaIndex = Double.MAX_VALUE;
    for (int i = 0; i < ra.length; i++) {
        if ((ra[i].height > 0) && (ra[i].width > 0)) {
            double areaIndex = ((double) ra[i].width) / ra[i].height - 1.0;
            if (areaIndex < 0) {
                areaIndex *= (-1);
            }
            if (areaIndex < bestAreaIndex) {
                bestAreaIndex = areaIndex;
                r = ra[i];
            }
        }
    }
    return r;
}

From source file:edu.ku.brc.af.prefs.PreferencesDlg.java

/**
 * Creates a search panel for the prefs.
 * @return a JPanel//  w w w  .  j  a v  a 2  s. c o  m
 */
protected JPanel createSearchPanel() {
    DocumentListener searchDL = new DocumentAdaptor() {
        @Override
        protected void changed(@SuppressWarnings("unused") DocumentEvent e) {
            delegateRenderer.clearArrows();
            for (int i = 0; i < prefsToolbar.getComponentCount(); i++) {
                Component c = prefsToolbar.getComponent(i);
                if (c instanceof RolloverCommand) {
                    Rectangle r = c.getBounds();
                    delegateRenderer.addArrow(r.x + r.width / 2, 10);
                    delegateRenderer.setIndexVisible(i, true);
                }
            }

            glassPane.setMargin(new Insets(prefsToolbar.getSize().height, 0, 0, 0));
            glassPane.setVisible(true);
            glassPane.setUseBGImage(true);
            glassPane.setHideOnClick(true);
            /*glassPane.addMouseListener(new MouseAdapter() {
            @Override public void mousePressed(MouseEvent e)
            { 
                glassPane.setVisible(false);
            }
            });*/
        }
    };
    searchText = new JAutoCompTextField(UIHelper.isMacOS() ? 15 : 22);
    searchText.getDocument().addDocumentListener(searchDL);
    SearchBox searchBox = new SearchBox(searchText, null);
    return searchBox;
}

From source file:FunLayout.java

public void layoutContainer(Container con) {
    int i, count, deltax, deltay, move;
    Dimension conSize;// ww w  .  j  av  a  2  s. c om
    Rectangle rect;
    Component comp;

    conSize = con.getSize();
    if (_prevContainerSize == null) {
        _prevContainerSize = conSize;
        return;
    }
    deltax = conSize.width - _prevContainerSize.width;
    deltay = conSize.height - _prevContainerSize.height;
    _prevContainerSize = conSize;
    count = con.countComponents();
    for (i = 0; i < count; i++) {
        comp = con.getComponent(i);
        if (!comp.isVisible())
            continue;
        move = _getMove(comp);
        if (move == 0)
            continue;
        rect = comp.getBounds();
        if (_negSized.containsKey(comp)) {
            // the component is really at a negative size
            rect = (Rectangle) _negSized.get(comp);
            _negSized.remove(comp);
        }
        if ((move & MOVES_RIGHT) > 0)
            rect.x += deltax;
        else if ((move & WIDTH_CHANGES) > 0)
            rect.width += deltax;
        if ((move & MOVES_DOWN) > 0)
            rect.y += deltay;
        else if ((move & HEIGHT_CHANGES) > 0)
            rect.height += deltay;
        // if a components size becomes negative, we track it since the AWT
        // does not allow components to have a size < (0, 0)
        if (rect.width < 0 || rect.height < 0)
            _negSized.put(comp, rect);
        comp.setBounds(rect.x, rect.y, rect.width, rect.height);
    }
}

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//from  w w  w  .  ja v  a 2  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;
}