Example usage for java.awt Component getPreferredSize

List of usage examples for java.awt Component getPreferredSize

Introduction

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

Prototype

public Dimension getPreferredSize() 

Source Link

Document

Gets the preferred size of this component.

Usage

From source file:CircleLayoutTest.java

public void setSizes(Container parent) {
    if (sizesSet)
        return;/*w  ww  . jav a2 s  .  c  om*/
    int n = parent.getComponentCount();

    preferredWidth = 0;
    preferredHeight = 0;
    minWidth = 0;
    minHeight = 0;
    maxComponentWidth = 0;
    maxComponentHeight = 0;

    // compute the maximum component widths and heights
    // and set the preferred size to the sum of the component sizes.
    for (int i = 0; i < n; i++) {
        Component c = parent.getComponent(i);
        if (c.isVisible()) {
            Dimension d = c.getPreferredSize();
            maxComponentWidth = Math.max(maxComponentWidth, d.width);
            maxComponentHeight = Math.max(maxComponentHeight, d.height);
            preferredWidth += d.width;
            preferredHeight += d.height;
        }
    }
    minWidth = preferredWidth / 2;
    minHeight = preferredHeight / 2;
    sizesSet = true;
}

From source file:CenterLayout.java

/**
 * Returns the preferred size./*w  w w.j a v  a  2  s  . c  o  m*/
 * 
 * @param parent
 *          the parent.
 * 
 * @return the preferred size.
 */
public Dimension preferredLayoutSize(final Container parent) {

    synchronized (parent.getTreeLock()) {
        final Insets insets = parent.getInsets();
        if (parent.getComponentCount() > 0) {
            final Component component = parent.getComponent(0);
            final Dimension d = component.getPreferredSize();
            return new Dimension((int) d.getWidth() + insets.left + insets.right,
                    (int) d.getHeight() + insets.top + insets.bottom);
        } else {
            return new Dimension(insets.left + insets.right, insets.top + insets.bottom);
        }
    }

}

From source file:VerticalLayout.java

/**
 * Lays out the container./*from   w ww.j a  va  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;
        }
    }
}

From source file:CenterLayout.java

/**
 * Lays out the components.//from www .j  a v  a2  s.  co  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:SquareLayout.java

/**
 * Calculates the preferred size dimensions for the specified panel given the
 * components in the specified parent container. 
 *//*from  w  ww.j  a v a 2 s . com*/

public Dimension preferredLayoutSize(Container container) {
    Component child = getChild(container);
    if (child == null)
        return new Dimension(0, 0);

    Dimension childPrefSize = child.getPreferredSize();
    Insets insets = container.getInsets();

    int width = childPrefSize.width + insets.left + insets.right;
    int height = childPrefSize.height + insets.top + insets.bottom;
    int maxSize = Math.max(width, height);
    return new Dimension(maxSize, maxSize);
}

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 .j  a  v  a 2 s.c o m*/
 * @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:com.jakemadethis.graphite.visualization.renderers.HyperedgeLabelRenderer.java

public void labelEdge(RenderContext<V, E> rc, Layout<V, E> layout, E e, String label) {
    if (label == null || label.length() == 0)
        return;//from  ww w .  j a  va 2s .co m

    Graph<V, E> g = layout.getGraph();

    EdgeLayout<E> edgeLayout = EdgeLayout$.MODULE$.apply(layout);

    Hypergraph<V, E> hg = g;

    if (!drawPredicate.evaluate(Context.<Hypergraph<V, E>, E>getInstance(hg, e)))
        return;

    if (!rc.getEdgeIncludePredicate().evaluate(Context.<Graph<V, E>, E>getInstance(g, e)))
        return;

    Point2D edgePos = edgeLayout.getEdgeLocation(e);
    edgePos = rc.getMultiLayerTransformer().transform(Layer.LAYOUT, edgePos);

    GraphicsDecorator gd = rc.getGraphicsContext();

    Component component = prepareRenderer(rc, rc.getEdgeLabelRenderer(), label,
            rc.getPickedEdgeState().isPicked(e), e);

    Dimension d = component.getPreferredSize();

    AffineTransform old = gd.getTransform();
    AffineTransform xform = new AffineTransform(old);
    xform.translate(edgePos.getX(), edgePos.getY());

    gd.setTransform(xform);
    gd.setStroke(new BasicStroke(1));
    Rectangle rect = new Rectangle(-10, -10, 20, 20);
    gd.setPaint(Color.WHITE);
    gd.fill(rect);
    gd.setPaint(Color.GRAY);
    gd.draw(rect);

    xform.translate(-d.width / 2, -d.height / 2);
    gd.setTransform(xform);

    gd.draw(component, rc.getRendererPane(), 0, 0, d.width, d.height, true);

    gd.setTransform(old);
}

From source file:ca.ubc.cs.ferret.cg.VertexLabelAsShapeRenderer.java

/**
 * Labels the specified vertex with the specified label.  
 * Uses the font specified by this instance's 
 * <code>VertexFontFunction</code>.  (If the font is unspecified, the existing
 * font for the graphics context is used.)  If vertex label centering
 * is active, the label is centered on the position of the vertex; otherwise
  * the label is offset slightly.//from  www  .j  a  v  a2s.  co  m
  */
public void labelVertex(RenderContext<V, E> rc, Layout<V, E> layout, V v, String label) {
    Graph<V, E> graph = layout.getGraph();
    if (rc.getVertexIncludePredicate().evaluate(Context.<Graph<V, E>, V>getInstance(graph, v)) == false) {
        return;
    }
    GraphicsDecorator g = rc.getGraphicsContext();
    Component component = prepareRenderer(rc, rc.getVertexLabelRenderer(), label,
            rc.getPickedVertexState().isPicked(v), v);
    Dimension d = component.getPreferredSize();

    int h_offset = -d.width / 2;
    int v_offset = -d.height / 2;

    Point2D p = layout.transform(v);
    p = rc.getMultiLayerTransformer().transform(Layer.LAYOUT, p);

    int x = (int) p.getX();
    int y = (int) p.getY();

    g.draw(component, rc.getRendererPane(), x + h_offset, y + v_offset, d.width, d.height, true);

    Dimension size = component.getPreferredSize();
    Rectangle bounds = new Rectangle(-size.width / 2 - 2, -size.height / 2 - 2, size.width + 4, size.height);
    shapes.put(v, bounds);
}

From source file:EntryLayout.java

/**
 * Compute the size of the whole mess. Serves as the guts of
 * preferredLayoutSize() and minimumLayoutSize().
 * /*from   w w  w . j a  v a 2  s .c o  m*/
 * @param parent
 *            The container in which to do the layout.
 * @param hp
 *            The horizontal padding (may be zero)
 * @param vp
 *            The Vertical Padding (may be zero).
 */
protected Dimension computeLayoutSize(Container parent, int hp, int vp) {
    if (!validWidths)
        return null;
    Component[] components = parent.getComponents();
    Dimension contSize = parent.getSize();
    int preferredWidth = 0, preferredHeight = 0;
    widths = new int[COLUMNS];
    heights = new int[components.length / COLUMNS];
    // System.out.println("Grid: " + widths.length + ", " + heights.length);

    int i;
    // Pass One: Compute largest widths and heights.
    for (i = 0; i < components.length; i++) {
        int row = i / widthPercentages.length;
        int col = i % widthPercentages.length;
        Component c = components[i];
        Dimension d = c.getPreferredSize();
        widths[col] = Math.max(widths[col], d.width);
        heights[row] = Math.max(heights[row], d.height);
    }

    // Pass two: agregate them.
    for (i = 0; i < widths.length; i++)
        preferredWidth += widths[i] + hp;
    for (i = 0; i < heights.length; i++)
        preferredHeight += heights[i] + vp;

    // Finally, pass the sums back as the actual size.
    return new Dimension(preferredWidth, preferredHeight);
}

From source file:Main.java

private void setSizes(Container parent) {
    int nComps = parent.getComponentCount();
    Dimension d = null;//from  w  w  w .j a v a2  s.  c o m

    //Reset preferred/minimum width and height.
    preferredWidth = 0;
    preferredHeight = 0;
    minWidth = 0;
    minHeight = 0;

    for (int i = 0; i < nComps; i++) {
        Component c = parent.getComponent(i);
        if (c.isVisible()) {
            d = c.getPreferredSize();

            if (i > 0) {
                preferredWidth += d.width / 2;
                preferredHeight += vgap;
            } else {
                preferredWidth = d.width;
            }
            preferredHeight += d.height;

            minWidth = Math.max(c.getMinimumSize().width, minWidth);
            minHeight = preferredHeight;
        }
    }
}