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:ColumnLayout.java

/**
 * The method that actually performs the layout. Called by the Container
 *//*from  w  w  w . ja  v a  2 s.c  om*/
public void layoutContainer(Container parent) {
    Insets insets = parent.getInsets();
    Dimension parent_size = parent.getSize();
    Component kid;
    int nkids = parent.getComponentCount();
    int x0 = insets.left + margin_width; // The base X position
    int x;
    int y = insets.top + margin_height; // Start at the top of the column

    for (int i = 0; i < nkids; i++) { // Loop through the kids
        kid = parent.getComponent(i); // Get the kid
        if (!kid.isVisible())
            continue; // Skip hidden ones
        Dimension pref = kid.getPreferredSize(); // How big is it?
        switch (alignment) { // Compute X coordinate
        default:
        case LEFT:
            x = x0;
            break;
        case CENTER:
            x = (parent_size.width - pref.width) / 2;
            break;
        case RIGHT:
            x = parent_size.width - insets.right - margin_width - pref.width;
            break;
        }
        // Set the size and position of this kid
        kid.setBounds(x, y, pref.width, pref.height);
        y += pref.height + spacing; // Get Y position of the next one
    }
}

From source file:CircleLayoutDemo.java

/**
 * Arranges the parent's Component objects in either an Ellipse or a Circle.
 * Ellipse is not yet implemented.//from w  w  w  . j  a v  a2 s  .  c  o m
 */
public void layoutContainer(Container parent) {
    int x, y, w, h, s, c;
    int n = parent.getComponentCount();
    double parentWidth = parent.getSize().width;
    double parentHeight = parent.getSize().height;
    Insets insets = parent.getInsets();
    int centerX = (int) (parentWidth - (insets.left + insets.right)) / 2;
    int centerY = (int) (parentHeight - (insets.top + insets.bottom)) / 2;

    Component comp = null;
    Dimension compPS = null;
    if (n == 1) {
        comp = parent.getComponent(0);
        x = centerX;
        y = centerY;
        compPS = comp.getPreferredSize();
        w = compPS.width;
        h = compPS.height;
        comp.setBounds(x, y, w, h);
    } else {
        double r = (Math.min(parentWidth - (insets.left + insets.right),
                parentHeight - (insets.top + insets.bottom))) / 2;
        r *= 0.75; // Multiply by .75 to account for extreme right and bottom
                   // Components
        for (int i = 0; i < n; i++) {
            comp = parent.getComponent(i);
            compPS = comp.getPreferredSize();
            if (isCircle) {
                c = (int) (r * Math.cos(2 * i * Math.PI / n));
                s = (int) (r * Math.sin(2 * i * Math.PI / n));
            } else {
                c = (int) ((centerX * 0.75) * Math.cos(2 * i * Math.PI / n));
                s = (int) ((centerY * 0.75) * Math.sin(2 * i * Math.PI / n));
            }
            x = c + centerX;
            y = s + centerY;

            w = compPS.width;
            h = compPS.height;

            comp.setBounds(x, y, w, h);
        }
    }

}

From source file:CircleLayoutTest.java

public void layoutContainer(Container parent) {
    setSizes(parent);/* ww  w .  ja v a  2s . co  m*/

    // compute center of the circle

    Insets insets = parent.getInsets();
    int containerWidth = parent.getSize().width - insets.left - insets.right;
    int containerHeight = parent.getSize().height - insets.top - insets.bottom;

    int xcenter = insets.left + containerWidth / 2;
    int ycenter = insets.top + containerHeight / 2;

    // compute radius of the circle

    int xradius = (containerWidth - maxComponentWidth) / 2;
    int yradius = (containerHeight - maxComponentHeight) / 2;
    int radius = Math.min(xradius, yradius);

    // lay out components along the circle

    int n = parent.getComponentCount();
    for (int i = 0; i < n; i++) {
        Component c = parent.getComponent(i);
        if (c.isVisible()) {
            double angle = 2 * Math.PI * i / n;

            // center point of component
            int x = xcenter + (int) (Math.cos(angle) * radius);
            int y = ycenter + (int) (Math.sin(angle) * radius);

            // move component so that its center is (x, y)
            // and its size is its preferred size
            Dimension d = c.getPreferredSize();
            c.setBounds(x - d.width / 2, y - d.height / 2, d.width, d.height);
        }
    }
}

From source file:SquareLayout.java

/**
 * Lays out the container in the specified panel. 
 *//*from  ww w  . j  av  a 2  s .c om*/

public void layoutContainer(Container container) {
    Component child = getChild(container);
    if (child == null)
        return;

    Dimension parentSize = container.getSize();
    Insets insets = container.getInsets();

    // A rectangle specifying the actual area available for layout. 
    Rectangle rect = new Rectangle(insets.left, insets.top, parentSize.width - insets.left - insets.right,
            parentSize.height - insets.top - insets.bottom);

    int minSize = rect.width < rect.height ? rect.width : rect.height;
    int widthSpace = rect.width - minSize;
    int heightSpace = rect.height - minSize;
    child.setBounds(rect.x + (int) (widthSpace * child.getAlignmentX()),
            rect.y + (int) (heightSpace * child.getAlignmentY()), minSize, minSize);
}

From source file:RelativeLayout.java

/**
 * Called by AWT to lay out the components in the target Container at its
 * current size.//  w ww .  j av  a2 s  .  c  o  m
 * 
 * @param target
 *            Container whose components are to be laid out.
 */
public void layoutContainer(Container target) {
    Dimension targSize = target.getSize();
    Insets ins = target.getInsets();
    // System.out.println("layoutContainer: size " + targSize);
    curWid = targSize.width;
    curHgt = targSize.height;
    float widRatio = (float) curWid / (float) reqWid;
    float hgtRatio = (float) curHgt / (float) reqHgt;
    for (int i = 0; i < curComps.size(); i++) {
        int px, py, pw, ph;
        Tracker t = (Tracker) curComps.elementAt(i);
        Component tc = t.getComponent();
        Dimension d = tc.getPreferredSize();
        px = ins.right + (int) (t.getRequestedLoc().x * widRatio);
        py = ins.top + (int) (t.getRequestedLoc().y * hgtRatio);
        pw = d.width;
        ph = d.height;
        // System.out.println("layoutContainer["+i+"]: move " +
        // tc + " to " + px + ", " + py);
        tc.setBounds(px, py, pw, ph);
    }
}

From source file:com.equitysoft.cellspark.ProportionalLayout.java

/**
 * Lays out the container.//from  w  w w . j a v  a 2 s.  c  om
 */
public void layoutContainer(Container parent) {
    Insets insets = parent.getInsets();
    synchronized (parent.getTreeLock()) {
        int n = parent.getComponentCount();
        Dimension pd = parent.getSize();
        //do layout
        int cnt = 0;
        int totalwid = pd.width - insets.left - insets.right;
        int x = insets.left;
        int cumwid = 0;
        for (int i = 0; i < n; i++) {
            Component c = parent.getComponent(i);
            int wid = proportions[i] * totalwid / total;
            c.setBounds(x, insets.top, wid, pd.height - insets.bottom - insets.top);
            x += wid;
            cnt++;
            if (cnt == num)
                break;
        }
    }
}

From source file:TreeLinkTest.java

public void layoutContainer(Container target) {
    Insets insets = target.getInsets();
    Dimension d = target.getSize();
    Dimension root_pref = root.getPreferredSize();
    double xscale = ((double) (d.width - insets.left - insets.right) / (double) (root_pref.width));
    double yscale = ((double) (d.height - insets.top - insets.bottom) / (double) (root_pref.height));
    root.doLayout(xscale, yscale, insets.left, insets.top);
}

From source file:RadialLayout.java

/**
 * This is called when the panel is first displayed, and every time its size
 * changes./*w ww  .jav a 2  s.  c  o  m*/
 * Note: You CAN'T assume preferredLayoutSize or minimumLayoutSize will be
 * called -- in the case of applets, at least, they probably won't be.
 *
 * @param  parent  the parent.
 * @see LayoutManager
 */
public void layoutContainer(final Container parent) {
    final Insets insets = parent.getInsets();
    final int maxWidth = parent.getSize().width - (insets.left + insets.right);
    final int maxHeight = parent.getSize().height - (insets.top + insets.bottom);
    final int nComps = parent.getComponentCount();
    int x = 0;
    int y = 0;

    // Go through the components' sizes, if neither preferredLayoutSize nor
    // minimumLayoutSize has been called.
    if (this.sizeUnknown) {
        setSizes(parent);
    }

    if (nComps < 2) {
        final Component c = parent.getComponent(0);
        if (c.isVisible()) {
            final Dimension d = c.getPreferredSize();
            c.setBounds(x, y, d.width, d.height);
        }
    } else {
        double radialCurrent = Math.toRadians(90);
        final double radialIncrement = 2 * Math.PI / nComps;
        final int midX = maxWidth / 2;
        final int midY = maxHeight / 2;
        final int a = midX - this.maxCompWidth;
        final int b = midY - this.maxCompHeight;
        for (int i = 0; i < nComps; i++) {
            final Component c = parent.getComponent(i);
            if (c.isVisible()) {
                final Dimension d = c.getPreferredSize();
                x = (int) (midX - (a * Math.cos(radialCurrent)) - (d.getWidth() / 2) + insets.left);
                y = (int) (midY - (b * Math.sin(radialCurrent)) - (d.getHeight() / 2) + insets.top);

                // Set the component's size and position.
                c.setBounds(x, y, d.width, d.height);
            }
            radialCurrent += radialIncrement;
        }
    }
}

From source file:EntryLayout.java

/**
 * Lays out the container in the specified panel. This is a row-column type
 * layout; find x, y, width and height of each Component.
 * //from   w  w w.  ja v  a  2s.c  om
 * @param parent
 *            The Container whose children we are laying out.
 */
public void layoutContainer(Container parent) {

    if (!validWidths)
        return;
    Component[] components = parent.getComponents();
    Dimension contSize = parent.getSize();
    int x = 0;
    for (int i = 0; i < components.length; i++) {
        int row = i / COLUMNS;
        int col = i % COLUMNS;
        Component c = components[i];
        Dimension d = c.getPreferredSize();
        int colWidth = (int) (contSize.width * widthPercentages[col]);

        if (col == 0) {
            x = hpad;
        } else {
            x += hpad * (col - 1) + (int) (contSize.width * widthPercentages[col - 1]);
        }
        int y = vpad * (row) + (row * heights[row]) + (heights[row] - d.height);
        Rectangle r = new Rectangle(x, y, colWidth, d.height);
        c.setBounds(r);
    }
}

From source file:VerticalLayout.java

/**
 * Lays out the container.//from  w  w  w .  j a  v  a 2s.c o m
 */
public void layoutContainer(Container parent) {
    Insets insets = parent.getInsets();
    synchronized (parent.getTreeLock()) {
        int n = parent.getComponentCount();
        Dimension pd = parent.getSize();
        int y = 0;
        //work out the total size
        for (int i = 0; i < n; i++) {
            Component c = parent.getComponent(i);
            Dimension d = c.getPreferredSize();
            y += d.height + vgap;
        }
        y -= vgap; //otherwise there's a vgap too many
        //Work out the anchor paint
        if (anchor == TOP)
            y = insets.top;
        else if (anchor == CENTER)
            y = (pd.height - y) / 2;
        else
            y = pd.height - y - insets.bottom;
        //do layout
        for (int i = 0; i < n; i++) {
            Component c = parent.getComponent(i);
            Dimension d = c.getPreferredSize();
            int x = insets.left;
            int wid = d.width;
            if (alignment == CENTER)
                x = (pd.width - d.width) / 2;
            else if (alignment == RIGHT)
                x = pd.width - d.width - insets.right;
            else if (alignment == BOTH)
                wid = pd.width - insets.left - insets.right;
            c.setBounds(x, y, wid, d.height);
            y += d.height + vgap;
        }
    }
}