Example usage for java.awt Component setBounds

List of usage examples for java.awt Component setBounds

Introduction

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

Prototype

public void setBounds(int x, int y, int width, int height) 

Source Link

Document

Moves and resizes this component.

Usage

From source file:RadialLayout.java

/**
 * This is called when the panel is first displayed, and every time its size
 * changes.//  ww  w .  j a  v 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:CircleLayoutDemo.java

/**
 * Arranges the parent's Component objects in either an Ellipse or a Circle.
 * Ellipse is not yet implemented./*w  ww  .j  av  a  2  s .  c  om*/
 */
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);//w w w  .  j a va 2s .  c  o  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:CenterLayout.java

/**
 * Lays out the components.//from  w  ww  . j  a v a 2 s.c o m
 * 
 * @param parent
 *          the parent.
 */
public void layoutContainer(final Container parent) {

    synchronized (parent.getTreeLock()) {
        if (parent.getComponentCount() > 0) {
            final Insets insets = parent.getInsets();
            final Dimension parentSize = parent.getSize();
            final Component component = parent.getComponent(0);
            final Dimension componentSize = component.getPreferredSize();
            final int xx = insets.left
                    + (Math.max((parentSize.width - insets.left - insets.right - componentSize.width) / 2, 0));
            final int yy = insets.top + (Math
                    .max((parentSize.height - insets.top - insets.bottom - componentSize.height) / 2, 0));
            component.setBounds(xx, yy, componentSize.width, componentSize.height);
        }
    }

}

From source file:RelativeLayout.java

/**
 * Called by AWT to lay out the components in the target Container at its
 * current size./*from  w  ww  .  j ava 2  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:GraphPaperLayout.java

/**
 * Lays out the container in the specified container.
 * /* w ww .j  a  v a 2s  .c o m*/
 * @param parent
 *          the component which needs to be laid out
 */
public void layoutContainer(Container parent) {
    synchronized (parent.getTreeLock()) {
        Insets insets = parent.getInsets();
        int ncomponents = parent.getComponentCount();

        if (ncomponents == 0) {
            return;
        }

        // Total parent dimensions
        Dimension size = parent.getSize();
        int totalW = size.width - (insets.left + insets.right);
        int totalH = size.height - (insets.top + insets.bottom);

        // Cell dimensions, including padding
        int totalCellW = totalW / gridSize.width;
        int totalCellH = totalH / gridSize.height;

        // Cell dimensions, without padding
        int cellW = (totalW - ((gridSize.width + 1) * hgap)) / gridSize.width;
        int cellH = (totalH - ((gridSize.height + 1) * vgap)) / gridSize.height;

        for (int i = 0; i < ncomponents; i++) {
            Component c = parent.getComponent(i);
            Rectangle rect = compTable.get(c);
            if (rect != null) {
                int x = insets.left + (totalCellW * rect.x) + hgap;
                int y = insets.top + (totalCellH * rect.y) + vgap;
                int w = (cellW * rect.width) - hgap;
                int h = (cellH * rect.height) - vgap;
                c.setBounds(x, y, w, h);
            }
        }
    }
}

From source file:SquareLayout.java

/**
 * Lays out the container in the specified panel. 
 *//*from www  .  jav  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:GraphPaperTest.java

/**
 * Lays out the container in the specified container.
 * /*from w  w w .ja  v  a2 s. c  o m*/
 * @param parent
 *            the component which needs to be laid out
 */
public void layoutContainer(Container parent) {
    synchronized (parent.getTreeLock()) {
        Insets insets = parent.getInsets();
        int ncomponents = parent.getComponentCount();

        if (ncomponents == 0) {
            return;
        }

        // Total parent dimensions
        Dimension size = parent.getSize();
        int totalW = size.width - (insets.left + insets.right);
        int totalH = size.height - (insets.top + insets.bottom);

        // Cell dimensions, including padding
        int totalCellW = totalW / gridSize.width;
        int totalCellH = totalH / gridSize.height;

        // Cell dimensions, without padding
        int cellW = (totalW - ((gridSize.width + 1) * hgap)) / gridSize.width;
        int cellH = (totalH - ((gridSize.height + 1) * vgap)) / gridSize.height;

        for (int i = 0; i < ncomponents; i++) {
            Component c = parent.getComponent(i);
            Rectangle rect = (Rectangle) compTable.get(c);
            if (rect != null) {
                int x = insets.left + (totalCellW * rect.x) + hgap;
                int y = insets.top + (totalCellH * rect.y) + vgap;
                int w = (cellW * rect.width) - hgap;
                int h = (cellH * rect.height) - vgap;
                c.setBounds(x, y, w, h);
            }
        }
    }
}

From source file:BeanContainer.java

public void layoutContainer(Container parent) {
        int divider = getDivider(parent);

        Insets insets = parent.getInsets();
        int w = parent.getWidth() - insets.left - insets.right - divider;
        int x = insets.left;
        int y = insets.top;

        for (int k = 1; k < parent.getComponentCount(); k += 2) {
            Component comp1 = parent.getComponent(k - 1);
            Component comp2 = parent.getComponent(k);
            Dimension d = comp2.getPreferredSize();

            comp1.setBounds(x, y, divider - m_hGap, d.height);
            comp2.setBounds(x + divider, y, w, d.height);
            y += d.height + m_vGap;//from  w  ww . j  a  v  a  2s.  c o m
        }
    }

From source file:ColumnLayout.java

/**
 * The method that actually performs the layout. Called by the Container
 *//*w  ww.  j  av  a  2 s. co  m*/
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
    }
}