Example usage for java.awt Container getInsets

List of usage examples for java.awt Container getInsets

Introduction

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

Prototype

public Insets getInsets() 

Source Link

Document

Determines the insets of this container, which indicate the size of the container's border.

Usage

From source file:OverlayLayout.java

/**
 * Calculates the minimum size dimensions for the specified
 * container, given the components it contains.
 *
 * @param parent the component to be laid out
 * @return the minimum size computed for the parent.
 * @see #preferredLayoutSize/*from   w w w  .  java 2  s.  co  m*/
 */
public Dimension minimumLayoutSize(final Container parent) {
    synchronized (parent.getTreeLock()) {
        final Insets ins = parent.getInsets();
        final Component[] comps = parent.getComponents();
        int height = 0;
        int width = 0;
        for (int i = 0; i < comps.length; i++) {
            if ((comps[i].isVisible() == false) && this.ignoreInvisible) {
                continue;
            }

            final Dimension pref = comps[i].getMinimumSize();
            if (pref.height > height) {
                height = pref.height;
            }
            if (pref.width > width) {
                width = pref.width;
            }
        }
        return new Dimension(width + ins.left + ins.right, height + ins.top + ins.bottom);
    }
}

From source file:OverlayLayout.java

/**
 * Calculates the preferred size dimensions for the specified
 * container, given the components it contains.
 *
 * @param parent the container to be laid out
 * @return the preferred size computed for the parent.
 * @see #minimumLayoutSize//ww w  .  ja  va  2  s.  co m
 */
public Dimension preferredLayoutSize(final Container parent) {
    synchronized (parent.getTreeLock()) {
        final Insets ins = parent.getInsets();
        final Component[] comps = parent.getComponents();
        int height = 0;
        int width = 0;
        for (int i = 0; i < comps.length; i++) {
            if ((comps[i].isVisible() == false) && this.ignoreInvisible) {
                continue;
            }

            final Dimension pref = comps[i].getPreferredSize();
            if (pref.height > height) {
                height = pref.height;
            }
            if (pref.width > width) {
                width = pref.width;
            }
        }
        return new Dimension(width + ins.left + ins.right, height + ins.top + ins.bottom);
    }
}

From source file:DividerLayout.java

/**
 * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
 *///from  w w w . j  av a2s.  c o  m
public void layoutContainer(Container container) {
    Insets insets = container.getInsets();
    Dimension westSize = new Dimension(0, 0);
    Dimension centerSize = new Dimension(0, 0);
    Dimension eastSize = new Dimension(0, 0);
    Rectangle centerBounds = new Rectangle(0, 0, 0, 0);
    Dimension containerSize = container.getSize();
    int centerX = containerSize.width / 2;
    int centerY = containerSize.height / 2;
    if ((centerComponent != null) && (centerComponent.isVisible())) {
        centerSize = centerComponent.getPreferredSize();
        centerSize.width = Math.min(centerSize.width, containerSize.width - insets.left - insets.right);
        centerSize.height = Math.min(centerSize.height, containerSize.height - insets.top - insets.bottom);
        centerComponent.setBounds(centerX - (centerSize.width / 2), centerY - (centerSize.height / 2),
                centerSize.width, centerSize.height);
        centerBounds = centerComponent.getBounds();
    }
    if ((westComponent != null) && (westComponent.isVisible())) {
        westSize = westComponent.getPreferredSize();
    }
    if ((eastComponent != null) && (eastComponent.isVisible())) {
        eastSize = eastComponent.getPreferredSize();
    }
    /* int maxWidth = Math.min(westSize.width, eastSize.width);
     maxWidth = Math.min(maxWidth, (containerSize.width -
         centerBounds.width - insets.left -
         insets.right) / 2);*/
    int maxWidth = (containerSize.width - centerBounds.width - insets.left - insets.right) / 2;

    /* int maxHeight = Math.min(westSize.height, eastSize.height);
      maxHeight = Math.max(maxHeight, containerSize.height -
         insets.top - insets.bottom);*/
    int maxHeight = containerSize.height - insets.top - insets.bottom;
    if (westComponent != null) {
        westComponent.setBounds(centerBounds.x - maxWidth, centerY - (maxHeight / 2), maxWidth, maxHeight);
    }
    if (eastComponent != null) {
        eastComponent.setBounds(centerBounds.x + centerBounds.width, centerY - (maxHeight / 2), maxWidth,
                maxHeight);
    }
}

From source file:CenterLayout.java

public void layoutContainer(Container container) {
    int count = container.getComponentCount();
    if (count > 0) {
        Component child = container.getComponent(0);
        java.awt.Insets insets = container.getInsets();
        int availWidth = container.getWidth() - insets.left - insets.right;
        int availHeight = container.getHeight() - insets.top - insets.bottom;
        Dimension preferredSize = child.getPreferredSize();
        double preferredWidth = preferredSize.getWidth();
        double preferredHeight = preferredSize.getHeight();
        int width;
        int height;
        int x;//  w  ww.  j ava  2  s.com
        int y;
        if (preferredWidth < availWidth) {
            x = (int) Math.round(insets.left + (availWidth - preferredWidth) / 2);
            width = (int) Math.round(preferredWidth);
        } else {
            x = insets.left;
            width = availWidth;
        }
        if (preferredHeight < availHeight) {
            y = (int) Math.round(insets.top + (availHeight - preferredHeight) / 2);
            height = (int) Math.round(preferredHeight);
        } else {
            y = insets.top;
            height = availHeight;
        }
        child.setBounds(x, y, width, height);
    }
}

From source file:ColumnLayout.java

protected Dimension layoutSize(Container parent, int sizetype) {
    int nkids = parent.getComponentCount();
    Dimension size = new Dimension(0, 0);
    Insets insets = parent.getInsets();
    int num_visible_kids = 0;

    // Compute maximum width and total height of all visible kids
    for (int i = 0; i < nkids; i++) {
        Component kid = parent.getComponent(i);
        Dimension d;//from w w  w  . j  a  va2  s.  c o  m
        if (!kid.isVisible())
            continue;
        num_visible_kids++;
        if (sizetype == 1)
            d = kid.getPreferredSize();
        else if (sizetype == 2)
            d = kid.getMinimumSize();
        else
            d = kid.getMaximumSize();
        if (d.width > size.width)
            size.width = d.width;
        size.height += d.height;
    }

    // Now add in margins and stuff
    size.width += insets.left + insets.right + 2 * margin_width;
    size.height += insets.top + insets.bottom + 2 * margin_height;
    if (num_visible_kids > 1)
        size.height += (num_visible_kids - 1) * spacing;
    return size;
}

From source file:RadialLayout.java

/**
 * This is called when the panel is first displayed, and every time its size
 * changes./*from   w  w  w. jav a  2s  . 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: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:RelativeLayout.java

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

/**
 * Lays out the container in the specified panel. 
 *//*from   w  ww  .jav  a2s. com*/

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

/**
 * Lays out the components.//from  ww  w. j  av  a  2s . 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);
        }
    }

}