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:fxts.stations.ui.SideLayout.java

/**
 * Lays out the grid./*from  w ww  .  ja v a2s.c o m*/
 *
 * @param aParent the layout container
 */
protected void arrangeGrid(Container aParent) {
    /////////////////////////////////////////////////////////
    //It`s only for debugging
    JComponent jc = (JComponent) aParent;
    String sType = (String) jc.getClientProperty("TYPE");
    if (sType != null) {
        boolean bInternal = "internal".equals(sType);
        mLogger.debug("\n" + sType);
    }
    //////////////////////////////////////////////////////////
    Component comp;
    int compindex;
    SideConstraints constraints;
    Insets insets = aParent.getInsets();
    Component[] components = aParent.getComponents();
    Dimension d;
    Rectangle r = new Rectangle();
    int i, diffw, diffh;
    double weight;
    SideLayoutInfo info;
    mRightToLeft = !aParent.getComponentOrientation().isLeftToRight();

    /*
    * If the parent has no slaves anymore, then don't do anything
    * at all:  just leave the parent's size as-is.
    */
    if (components.length == 0 && (mColumnWidths == null || mColumnWidths.length == 0)
            && (mRowHeights == null || mRowHeights.length == 0)) {
        return;
    }

    /*
    * Pass #1: scan all the slaves to figure out the total amount
    * of space needed.
    */

    info = getLayoutInfo(aParent, PREFERREDSIZE);
    d = getMinSize(aParent, info);

    //
    //    System.out.println("parent=w:" + parent.getWidth() + ",h:" + parent.getHeight() +
    //                       "min=w:" + d.getWidth() + ",h:" + d.getHeight());
    if (aParent.getWidth() < d.width || aParent.getHeight() < d.height) {
        info = getLayoutInfo(aParent, MINSIZE);
        d = getMinSize(aParent, info);
        //
        //      System.out.println("MINSIZE");
    } else {
        //
        //      System.out.println("Non MINSIZE");
    }
    mLayoutInfo = info;
    r.width = d.width;
    r.height = d.height;

    /*
    * If the current dimensions of the window don't match the desired
    * dimensions, then adjust the minWidth and minHeight arrays
    * according to the weights.
    */

    diffw = aParent.getWidth() - r.width;
    //
    //    System.out.println("diffw=" + diffw);
    if (diffw != 0) {
        weight = 0.0;
        for (i = 0; i < info.width; i++) {
            weight += info.weightX[i];
        }
        if (weight > 0.0) {
            for (i = 0; i < info.width; i++) {
                int dx = (int) (((double) diffw * info.weightX[i]) / weight);
                info.minWidth[i] += dx;
                r.width += dx;
                if (info.minWidth[i] < 0) {
                    r.width -= info.minWidth[i];
                    info.minWidth[i] = 0;
                }
            }
        }
        diffw = aParent.getWidth() - r.width;
    } else {
        diffw = 0;
    }
    diffh = aParent.getHeight() - r.height;
    //
    //    System.out.println("diffh=" + diffh);
    if (diffh != 0) {
        weight = 0.0;
        for (i = 0; i < info.height; i++) {
            weight += info.weightY[i];
        }
        if (weight > 0.0) {
            for (i = 0; i < info.height; i++) {
                int dy = (int) (((double) diffh * info.weightY[i]) / weight);
                info.minHeight[i] += dy;
                r.height += dy;
                if (info.minHeight[i] < 0) {
                    r.height -= info.minHeight[i];
                    info.minHeight[i] = 0;
                }
            }
        }
        diffh = aParent.getHeight() - r.height;
    } else {
        diffh = 0;
    }

    /*
    * Now do the actual layout of the slaves using the layout information
    * that has been collected.
    */

    info.startx = /*diffw/2 +*/ insets.left;
    info.starty = /*diffh/2 +*/ insets.top;
    //
    //    System.out.println("info.startx = " + info.startx);
    //    System.out.println("info.starty = " + info.startx);
    for (compindex = 0; compindex < components.length; compindex++) {
        comp = components[compindex];
        if (!comp.isVisible()) {
            continue;
        }
        constraints = lookupConstraints(comp);
        if (!mRightToLeft) {
            r.x = info.startx;
            for (i = 0; i < constraints.tempX; i++) {
                r.x += info.minWidth[i];
            }
        } else {
            r.x = aParent.getWidth() - insets.right;
            for (i = 0; i < constraints.tempX; i++) {
                r.x -= info.minWidth[i];
            }
        }
        r.y = info.starty;
        for (i = 0; i < constraints.tempY; i++) {
            r.y += info.minHeight[i];
        }
        r.width = 0;
        for (i = constraints.tempX; i < constraints.tempX + constraints.tempWidth; i++) {
            r.width += info.minWidth[i];
        }
        r.height = 0;
        for (i = constraints.tempY; i < constraints.tempY + constraints.tempHeight; i++) {
            r.height += info.minHeight[i];
        }
        adjustForGravity(constraints, r);
        if (r.x < 0) {
            r.width -= r.x;
            r.x = 0;
        }
        if (r.y < 0) {
            r.height -= r.y;
            r.y = 0;
        }

        /*
        * If the window is too small to be interesting then
        * unmap it.  Otherwise configure it and then make sure
        * it's mapped.
        */

        if (r.width <= 0 || r.height <= 0) {
            comp.setBounds(0, 0, 0, 0);
        } else {
            if (comp.getX() != r.x || comp.getY() != r.y || comp.getWidth() != r.width
                    || comp.getHeight() != r.height) {
                comp.setBounds(r.x, r.y, r.width, r.height);
            }
        }

        //        System.out.println("Initial component size (x = " + (int)comp.getX() +
        //                           ", y = " + (int)(comp.getY()) +
        //                           ", widht = " + (int)(comp.getWidth()) +
        //                           ", height = " + (int)(comp.getHeight()));
        if (diffw > 0) {
            //            System.out.println("It`s increasing by x!");
            //if (comp instanceof IResizableComponent) {
            //                System.out.println("It`s resizable component: " + comp);

            //IResizableComponent resizeComp = (IResizableComponent)comp;
            ResizeParameter param = constraints.resize;

            //                System.out.println("Params: Left=" + param.getLeft() + ",top=" + param.getTop() +
            //                                   ",Right=" + param.getRight() + ",bottom=" + param.getBottom());
            comp.setBounds((int) (comp.getX() + diffw * param.getLeft()), comp.getY(),
                    (int) (comp.getWidth() + diffw * (param.getRight() - param.getLeft())), comp.getHeight());

            //                System.out.println("Set Bounds (x = " + (int)(comp.getX() + diffw * param.getLeft()) +
            //                    ", y = " + (int)(comp.getY()) +
            //                    ", widht = " + (int)(comp.getWidth() +
            ///                                     diffw * (param.getRight() - param.getLeft())) +
            //                    ", height = " + (int)(comp.getHeight()));
            //            }
        }
        if (diffh > 0) {
            //            System.out.println("It`s increasing by y!");
            //            if (comp instanceof IResizableComponent) {
            //                System.out.println("It`s resizable component: " + comp);

            //                IResizableComponent resizeComp = (IResizableComponent)comp;
            ResizeParameter param = constraints.resize;

            //                System.out.println("Params: Left=" + param.getLeft() + ",top=" + param.getTop() +
            //                                   ",Right=" + param.getRight() + ",bottom=" + param.getBottom());
            comp.setBounds(comp.getX(), (int) (comp.getY() + diffh * param.getTop()), comp.getWidth(),
                    (int) (comp.getHeight() + diffh * (param.getBottom() - param.getTop())));

            //                System.out.println("Set Bounds (x = " + (int)(comp.getX()) +
            //                    ", y = " + (int)(comp.getY() + diffh * param.getTop()) +
            //                    ", widht = " + (int)(comp.getWidth()) +
            //                    ", height = " + (int)(comp.getHeight() +
            //                                     diffh * (param.getBottom() - param.getTop())));
            //            }
        }
    }
}

From source file:org.kineticsystem.commons.layout.TetrisLayout.java

/**
 * Lay out components in the specified container.
 * @param parent The container which needs to be laid out.
 *///from  ww  w.  j  av  a 2s .  com
public void layoutContainer(Container parent) {

    /* 
     * This method synchronizes on the tree lock of the component. This tree
     * lock is an object that can be used to provide thread-safe access to
     * the layout manager in case different threads are simultaneously
     * adding or removing components. The tree lock object is used as a
     * synchronization point for all of the methods associated with laying
     * out containers and invalidating components, and it is good
     * programming practice to use it to ensure a thread-safe
     * implementation.
     */

    synchronized (parent.getTreeLock()) {

        // Layout components.

        if (containerSize == null) {
            setup(parent);
        }

        Insets insets = parent.getInsets();

        int componentNumber = parent.getComponentCount();
        if (componentNumber == 0) {
            return;
        }

        Dimension size = parent.getSize();

        cGaps[0].setIncSize(cGaps[0].getSize());
        for (int i = 1; i < cBars.length + 1; i++) {
            cGaps[i].setIncSize(cGaps[i].getSize() + cGaps[i - 1].getIncSize());
        }
        rGaps[0].setIncSize(rGaps[0].getSize());
        for (int i = 1; i < rBars.length + 1; i++) {
            rGaps[i].setIncSize(rGaps[i].getSize() + rGaps[i - 1].getIncSize());
        }

        int actualGapFreeWidth = Math
                .max(size.width - insets.left - insets.right - cGaps[cBars.length].getIncSize(), 0);
        int actualGapFreeHeight = Math
                .max(size.height - insets.top - insets.bottom - rGaps[rBars.length].getIncSize(), 0);

        for (Map.Entry<Component, Cell> entry : constraintsMap.entrySet()) {

            Component comp = (Component) entry.getKey();
            Cell cell = (Cell) entry.getValue();
            if (cell != null) {

                // Actual cell position.

                int x0 = (int) Math.round(cSizes[cell.getCol()].evaluate(actualGapFreeWidth));
                int y0 = (int) Math.round(rSizes[cell.getRow()].evaluate(actualGapFreeHeight));
                int x1 = (int) Math.round(cSizes[cell.getCol() + cell.getCols()].evaluate(actualGapFreeWidth));
                int y1 = (int) Math.round(rSizes[cell.getRow() + cell.getRows()].evaluate(actualGapFreeHeight));

                // Actual cell dimension.

                int w = x1 - x0;
                int h = y1 - y0;

                // Component position correction.

                int xCorrection = insets.left + cGaps[cell.getCol()].getIncSize();
                int yCorrection = insets.top + rGaps[cell.getRow()].getIncSize();

                // Component dimension correction.

                int wCorrection = cGaps[cell.getCol() + cell.getCols() - 1].getIncSize()
                        - cGaps[cell.getCol()].getIncSize();
                int hCorrection = rGaps[cell.getRow() + cell.getRows() - 1].getIncSize()
                        - rGaps[cell.getRow()].getIncSize();

                // Preferred component dimension.

                int wComp = comp.getPreferredSize().width;
                int hComp = comp.getPreferredSize().height;

                int fill = cell.getFill();
                int anchor = cell.getAnchor();

                switch (fill) {
                case Cell.NONE: {
                    if (wComp > w) {
                        wComp = w;
                    }
                    if (hComp > h) {
                        hComp = h;
                    }
                    switch (anchor) {
                    case Cell.FIRST_LINE_START:
                        x0 += xCorrection;
                        y0 += yCorrection;
                        break;
                    case Cell.PAGE_START:
                        x0 += xCorrection + (w - wComp) / 2;
                        y0 += yCorrection;
                        break;
                    case Cell.FIRST_LINE_END:
                        x0 += xCorrection + w + wCorrection - wComp;
                        y0 += yCorrection;
                        break;
                    case Cell.LINE_START:
                        x0 += xCorrection;
                        y0 += yCorrection + (h - hComp) / 2;
                        break;
                    case Cell.CENTER:
                        x0 += xCorrection + (w - wComp) / 2;
                        y0 += yCorrection + (h - hComp) / 2;
                        break;
                    case Cell.LINE_END:
                        x0 += xCorrection + w + wCorrection - wComp;
                        y0 += yCorrection + (h - hComp) / 2;
                        break;
                    case Cell.LAST_LINE_START:
                        x0 += xCorrection;
                        y0 += yCorrection + h + hCorrection - hComp;
                        break;
                    case Cell.PAGE_END:
                        x0 += xCorrection + (w - wComp) / 2;
                        y0 += yCorrection + h + hCorrection - hComp;
                        break;
                    case Cell.LAST_LINE_END:
                        x0 += xCorrection + w + wCorrection - wComp;
                        y0 += yCorrection + h - hCorrection - hComp;
                        break;
                    }
                    w = Math.min(w, wComp) + wCorrection;
                    h = Math.min(h, hComp) + hCorrection;
                    break;
                }
                case Cell.PROPORTIONAL: {
                    double ratio = Math.min((double) ((double) w / (double) wComp),
                            (double) ((double) h / (double) hComp));
                    wComp = (int) Math.round(wComp * ratio);
                    hComp = (int) Math.round(hComp * ratio);
                    switch (anchor) {
                    case Cell.FIRST_LINE_START:
                        x0 += xCorrection;
                        y0 += yCorrection;
                        break;
                    case Cell.PAGE_START:
                        x0 += xCorrection + (w - wComp) / 2;
                        y0 += yCorrection;
                        break;
                    case Cell.FIRST_LINE_END:
                        x0 += xCorrection + wCorrection + w - wComp;
                        y0 += yCorrection;
                        break;
                    case Cell.LINE_START:
                        x0 += xCorrection;
                        y0 += yCorrection + (h - hComp) / 2;
                        break;
                    case Cell.CENTER:
                        x0 += xCorrection + (w - wComp) / 2;
                        y0 += yCorrection + (h - hComp) / 2;
                        break;
                    case Cell.LINE_END:
                        x0 += xCorrection + wCorrection + w - wComp;
                        y0 += yCorrection + (h - hComp) / 2;
                        break;
                    case Cell.LAST_LINE_START:
                        x0 += xCorrection;
                        y0 += yCorrection + hCorrection + h - hComp;
                        break;
                    case Cell.PAGE_END:
                        x0 += xCorrection + (w - wComp) / 2;
                        y0 += yCorrection + hCorrection + h - hComp;
                        break;
                    case Cell.LAST_LINE_END:
                        x0 += xCorrection + wCorrection + w - wComp;
                        y0 += yCorrection + hCorrection + h - hComp;
                        break;
                    }
                    w = Math.min(w, wComp) + wCorrection;
                    h = Math.min(h, hComp) + hCorrection;
                    break;
                }
                case Cell.BOTH: {
                    x0 += xCorrection;
                    y0 += yCorrection;
                    w += wCorrection;
                    h += hCorrection;
                    break;
                }
                case Cell.HORIZONTAL: {
                    if (hComp > h) {
                        hComp = h;
                    }
                    switch (anchor) {
                    case Cell.FIRST_LINE_START:
                    case Cell.PAGE_START:
                    case Cell.FIRST_LINE_END:
                        y0 += yCorrection;
                        break;
                    case Cell.LINE_START:
                    case Cell.CENTER:
                    case Cell.LINE_END:
                        y0 += yCorrection + (h - hComp) / 2;
                        break;
                    case Cell.LAST_LINE_START:
                    case Cell.PAGE_END:
                    case Cell.LAST_LINE_END:
                        y0 += yCorrection + h + hCorrection - hComp;
                        break;
                    }
                    x0 += xCorrection;
                    w += wCorrection;
                    h = Math.min(h, hComp) + hCorrection;
                    break;
                }
                case Cell.VERTICAL: {
                    if (wComp > w) {
                        wComp = w;
                    }
                    switch (anchor) {
                    case Cell.FIRST_LINE_START:
                    case Cell.LINE_START:
                    case Cell.LAST_LINE_START:
                        x0 += xCorrection;
                        break;
                    case Cell.PAGE_START:
                    case Cell.CENTER:
                    case Cell.PAGE_END:
                        x0 += xCorrection + (w - wComp) / 2;
                        break;
                    case Cell.FIRST_LINE_END:
                    case Cell.LINE_END:
                    case Cell.LAST_LINE_END:
                        x0 += xCorrection + w + wCorrection - wComp;
                        break;
                    }
                    y0 += yCorrection;
                    w = Math.min(w, wComp) + wCorrection;
                    h += hCorrection;
                    break;
                }
                }
                comp.setBounds(x0, y0, w, h);
            }
        }
    }
}