Example usage for java.awt Component getAlignmentX

List of usage examples for java.awt Component getAlignmentX

Introduction

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

Prototype

public float getAlignmentX() 

Source Link

Document

Returns the alignment along the x axis.

Usage

From source file:SquareLayout.java

/**
 * Lays out the container in the specified panel. 
 *//* w  w  w. ja  v a  2s  .  c o m*/

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

/**
 * Lays out the specified container. Throws an
 * <code>IllegalStateException</code> if any of the components added via the
 * <code>addLayoutComponent</code> method have a different parent than the
 * specified Container./*  www. j ava  2 s .  c  o m*/
 */

public void layoutContainer(Container parent) {
    synchronized (parent.getTreeLock()) {
        int rowCount = rows.size();

        Insets parentInsets = parent.getInsets();

        // Collect the preferred sizes.
        Dimension[][] prefSizes = getPreferredSizes(parent);
        Pair layout = calculateLayout(prefSizes);
        int[] columnWidths = (int[]) layout.getFirst();
        int[] rowHeights = (int[]) layout.getSecond();

        Dimension prefParentSize = calculatePreferredLayoutSize(parent, columnWidths, rowHeights);
        Dimension parentSize = parent.getSize();
        Dimension layoutSize = new Dimension(
                parentSize.width - xGap * (rowCount - 1) - parentInsets.left - parentInsets.right,
                parentSize.height - yGap * (columnCount - 1) - parentInsets.top - parentInsets.bottom);
        Dimension prefLayoutSize = new Dimension(
                prefParentSize.width - xGap * (rowCount - 1) - parentInsets.left - parentInsets.right,
                prefParentSize.height - yGap * (columnCount - 1) - parentInsets.top - parentInsets.bottom);

        // Layout the components.
        int y = parentInsets.top;
        for (int i = 0; i < rowCount; i++) {
            int x = parentInsets.left;
            int cellHeight = (rowHeights[i] * layoutSize.height) / prefLayoutSize.height;
            Component[] row = (Component[]) rows.elementAt(i);
            for (int j = 0; j < row.length; j++) {
                int cellWidth = (columnWidths[j] * layoutSize.width) / prefLayoutSize.width;
                Component component = row[j];

                // Can only happen on the last line when all the remaining components are null as well
                if (component == null)
                    break;

                Dimension maxSize = component.getMaximumSize();

                int compWidth = Math.min(maxSize.width, cellWidth);
                int compHeight = Math.min(maxSize.height, cellHeight);

                int compX = x + (int) ((cellWidth - compWidth) * component.getAlignmentX());
                int compY = y + (int) ((cellHeight - compHeight) * component.getAlignmentY());

                component.setBounds(compX, compY, compWidth, compHeight);

                x += cellWidth + xGap;
            }

            y += cellHeight + yGap;
        }
    }
}