Example usage for java.awt Container getSize

List of usage examples for java.awt Container getSize

Introduction

In this page you can find the example usage for java.awt Container getSize.

Prototype

public Dimension getSize() 

Source Link

Document

Returns the size of this component in the form of a Dimension object.

Usage

From source file:Main.java

public static void main(String[] args) {
    Main t = new Main();
    t.setSize(500, 500);/*from w w w.ja v  a2 s .  co m*/
    Container bg = t.getContentPane();
    t.createJEditorPane(bg, bg.getSize());
    t.setVisible(true);
}

From source file:Main.java

public static Dimension getFrameSize(Component c) {
    Container parent = getRootContainer(c);
    if (parent != null) {
        return parent.getSize();
    }//from www .j av a2s.  c  o m
    return Toolkit.getDefaultToolkit().getScreenSize();
}

From source file:Main.java

/**
 * Center a window on screen.//w ww .ja  va  2s.co  m
 *
 * @param w
 *          the window to center on screen.
 */
public static void centerInParent(Window w) {
    Container parent = w.getParent();
    if (parent != null) {
        Dimension parentSize = parent.getSize();
        w.setLocation(parent.getX() + (parentSize.width - w.getWidth()) / 2,
                parent.getY() + (parentSize.height - w.getHeight()) / 2);
    }
}

From source file:Main.java

/**
 * Positions the specified dialog at a position relative to its parent.
 *
 * @param dialog  the dialog to be positioned.
 * @param horizontalPercent  the relative location.
 * @param verticalPercent  the relative location.
 *///from  w w  w.  j av a2  s . com
public static void positionDialogRelativeToParent(final Dialog dialog, final double horizontalPercent,
        final double verticalPercent) {
    final Dimension d = dialog.getSize();
    final Container parent = dialog.getParent();
    final Dimension p = parent.getSize();

    final int baseX = parent.getX() - d.width;
    final int baseY = parent.getY() - d.height;
    final int w = d.width + p.width;
    final int h = d.height + p.height;
    int x = baseX + (int) (horizontalPercent * w);
    int y = baseY + (int) (verticalPercent * h);

    // make sure the dialog fits completely on the screen...
    final Rectangle s = getMaximumWindowBounds();
    x = Math.min(x, (s.width - d.width));
    x = Math.max(x, 0);
    y = Math.min(y, (s.height - d.height));
    y = Math.max(y, 0);

    dialog.setBounds(x + s.x, y + s.y, d.width, d.height);

}

From source file:GUIUtils.java

/**
 * Centers passed internal frame within its desktop area. If centering would
 * cause the title bar to go off the top of the screen then move the window
 * down.//from  w  ww.  j a  v a  2  s . co m
 * 
 * @param frame
 *          The internal frame to be centered.
 * 
 * @throws IllegalArgumentException
 *           If <TT>frame</TT> is <TT>null</TT>.
 */
public static void centerWithinDesktop(JInternalFrame frame) {
    if (frame == null) {
        throw new IllegalArgumentException("null JInternalFrame passed");
    }
    final Container parent = frame.getDesktopPane();
    if (parent != null && parent.isVisible()) {
        center(frame, new Rectangle(new Point(0, 0), parent.getSize()));
    }
}

From source file:GUIUtils.java

/**
 * Centers <CODE>wind</CODE> within its parent. If it has no parent then
 * center within the screen. If centering would cause the title bar to go
 * above the parent (I.E. cannot see the titlebar and so cannot move the
 * window) then move the window down./*w ww .  j a v a 2  s.  c  om*/
 * 
 * @param wind
 *          The Window to be centered.
 * 
 * @throws IllegalArgumentException
 *           If <TT>wind</TT> is <TT>null</TT>.
 */
public static void centerWithinParent(Window wind) {
    if (wind == null) {
        throw new IllegalArgumentException("null Window passed");
    }
    final Container parent = wind.getParent();
    if (parent != null && parent.isVisible()) {
        center(wind, new Rectangle(parent.getLocationOnScreen(), parent.getSize()));
    } else {
        centerWithinScreen(wind);
    }
}

From source file:Main.java

/**
 * Positions the specified dialog at a position relative to its parent.
 *
 * @param dialog            the dialog to be positioned.
 * @param horizontalPercent the relative location.
 * @param verticalPercent   the relative location.
 *///from  w  w  w  . j  a v  a 2 s .c o m
public static void positionDialogRelativeToParent(final Dialog dialog, final double horizontalPercent,
        final double verticalPercent) {
    final Container parent = dialog.getParent();
    if (parent == null || (parent.isVisible() == false)) {
        positionFrameOnScreen(dialog, horizontalPercent, verticalPercent);
        return;
    }

    final Dimension d = dialog.getSize();
    final Dimension p = parent.getSize();

    final int baseX = parent.getX();
    final int baseY = parent.getY();

    final int parentPointX = baseX + (int) (horizontalPercent * p.width);
    final int parentPointY = baseY + (int) (verticalPercent * p.height);

    final int dialogPointX = Math.max(0, parentPointX - (int) (horizontalPercent * d.width));
    final int dialogPointY = Math.max(0, parentPointY - (int) (verticalPercent * d.height));

    // make sure the dialog fits completely on the screen...
    final Rectangle s = parent.getGraphicsConfiguration().getBounds();
    final Rectangle r = new Rectangle(dialogPointX, dialogPointY, d.width, d.height);
    final Rectangle intersectedDialogBounds = r.intersection(s);
    if (intersectedDialogBounds.width < d.width) {
        r.x = s.width - d.width;
        r.width = d.width;
    }
    if (intersectedDialogBounds.height < d.height) {
        r.y = s.height - d.height;
        r.height = d.height;
    }
    final Rectangle finalIntersection = r.intersection(s);
    dialog.setBounds(finalIntersection);
}

From source file:CircleLayoutDemo.java

/**
 * Returns this CircleLayout's minimum size based on its Container
 * /*from  w  ww .  j  a v  a  2s. c o m*/
 * @param target
 *          This CircleLayout's target container
 * @return The minimum size
 */
public Dimension minimumLayoutSize(Container target) {
    return target.getSize();
}

From source file:CircleLayoutDemo.java

/**
 * Returns this CircleLayout's preferred size based on its Container
 * /*from   ww w .  j  a  v a 2 s. c om*/
 * @param target
 *          This CircleLayout's target container
 * @return The preferred size
 */

public Dimension preferredLayoutSize(Container target) {
    return target.getSize();
}

From source file:CircleLayoutTest.java

public void layoutContainer(Container parent) {
    Insets insets = parent.getInsets();
    int containerWidth = parent.getSize().width - insets.left - insets.right;
    int containerHeight = parent.getSize().height - insets.top - insets.bottom;
    int xradius = (containerWidth - maxComponentWidth) / 2;
    int yradius = (containerHeight - maxComponentHeight) / 2;

    setSizes(parent);//  ww  w . j  a va  2s . c om
    int centerX = insets.left + containerWidth / 2;
    int centerY = insets.top + containerHeight / 2;

    int comCount = parent.getComponentCount();
    for (int i = 0; i < comCount; i++) {
        Component c = parent.getComponent(i);
        if (c.isVisible()) {
            Dimension size = c.getPreferredSize();
            double angle = 2 * Math.PI * i / comCount;
            int x = centerX + (int) (Math.cos(angle) * xradius);
            int y = centerY + (int) (Math.sin(angle) * yradius);

            c.setBounds(x - size.width / 2, y - size.height / 2, size.width, size.height);
        }
    }
}