FileDocCategorySizeDatePackage
GraphPaperLayout.javaAPI DocExample12043Tue Dec 12 18:59:00 GMT 2000None

GraphPaperLayout

public class GraphPaperLayout extends Object implements LayoutManager2
The GraphPaperLayout class is a layout manager that lays out a container's components in a rectangular grid, similar to GridLayout. Unlike GridLayout, however, components can take up multiple rows and/or columns. The layout manager acts as a sheet of graph paper. When a component is added to the layout manager, the location and relative size of the component are simply supplied by the constraints as a Rectangle.

import java.awt.*;
import java.applet.Applet;
public class ButtonGrid extends Applet {
public void init() {
setLayout(new GraphPaperLayout(new Dimension(5,5)));
// Add a 1x1 Rect at (0,0)
add(new Button("1"), new Rectangle(0,0,1,1));
// Add a 2x1 Rect at (2,0)
add(new Button("2"), new Rectangle(2,0,2,1));
// Add a 1x2 Rect at (1,1)
add(new Button("3"), new Rectangle(1,1,1,2));
// Add a 2x2 Rect at (3,2)
add(new Button("4"), new Rectangle(3,2,2,2));
// Add a 1x1 Rect at (0,4)
add(new Button("5"), new Rectangle(0,4,1,1));
// Add a 1x2 Rect at (2,3)
add(new Button("6"), new Rectangle(2,3,1,2));
}
}
author
Michael Martak

Fields Summary
int
hgap
int
vgap
Dimension
gridSize
Hashtable
compTable
Constructors Summary
public GraphPaperLayout()
Creates a graph paper layout with a default of a 1 x 1 graph, with no vertical or horizontal padding.

        this(new Dimension(1,1));
    
public GraphPaperLayout(Dimension gridSize)
Creates a graph paper layout with the given grid size, with no vertical or horizontal padding.

        this(gridSize, 0, 0);
    
public GraphPaperLayout(Dimension gridSize, int hgap, int vgap)
Creates a graph paper layout with the given grid size and padding.

param
gridSize size of the graph paper in logical units (n x m)
param
hgap horizontal padding
param
vgap vertical padding

        if ((gridSize.width <= 0) || (gridSize.height <= 0)) {
            throw new IllegalArgumentException(
                "dimensions must be greater than zero");
        }
        this.gridSize = new Dimension(gridSize);
        this.hgap = hgap;
        this.vgap = vgap;
        compTable = new Hashtable();
    
Methods Summary
public voidaddLayoutComponent(java.awt.Component comp, java.lang.Object constraints)
Adds the specified component to the layout, using the specified constraint object.

param
comp the component to be added
param
constraints where/how the component is added to the layout.

        if (constraints instanceof Rectangle) {
            Rectangle rect = (Rectangle)constraints;
            if ( rect.width <= 0 || rect.height <= 0 ) {
                throw new IllegalArgumentException(
                    "cannot add to layout: rectangle must have positive width and height");
            }
            if ( rect.x < 0 || rect.y < 0 ) {
                throw new IllegalArgumentException(
                    "cannot add to layout: rectangle x and y must be >= 0");
            }
            setConstraints(comp, rect);
        } else if (constraints != null) {
            throw new IllegalArgumentException(
                "cannot add to layout: constraint must be a Rectangle");
        }
    
public voidaddLayoutComponent(java.lang.String name, java.awt.Component comp)
Adds the specified component with the specified name to the layout. This does nothing in GraphPaperLayout, since constraints are required.

    
public java.awt.DimensiongetGridSize()

return
the size of the graph paper in logical units (n x m)

        return new Dimension( gridSize );
    
protected java.awt.DimensiongetLargestCellSize(java.awt.Container parent, boolean isPreferred)
Algorithm for calculating the largest minimum or preferred cell size.

Largest cell size is calculated by getting the applicable size of each component and keeping the maximum value, dividing the component's width by the number of columns it is specified to occupy and dividing the component's height by the number of rows it is specified to occupy.

param
parent the container in which to do the layout.
param
isPreferred true for calculating preferred size, false for calculating minimum size.
return
the largest cell size required.

        int ncomponents = parent.getComponentCount();
        Dimension maxCellSize = new Dimension(0,0);
        for ( int i = 0; i < ncomponents; i++ ) {
            Component c = parent.getComponent(i);
            Rectangle rect = (Rectangle)compTable.get(c);
            if ( c != null && rect != null ) {
                Dimension componentSize;
                if ( isPreferred ) {
                    componentSize = c.getPreferredSize();
                } else {
                    componentSize = c.getMinimumSize();
                }
                // Note: rect dimensions are already asserted to be > 0 when the
                // component is added with constraints
                maxCellSize.width = Math.max(maxCellSize.width,
                    componentSize.width / rect.width);
                maxCellSize.height = Math.max(maxCellSize.height,
                    componentSize.height / rect.height);
            }
        }
        return maxCellSize;
    
public floatgetLayoutAlignmentX(java.awt.Container target)
Returns the alignment along the x axis. This specifies how the component would like to be aligned relative to other components. The value should be a number between 0 and 1 where 0 represents alignment along the origin, 1 is aligned the furthest away from the origin, 0.5 is centered, etc.

        return 0.5f;
    
public floatgetLayoutAlignmentY(java.awt.Container target)
Returns the alignment along the y axis. This specifies how the component would like to be aligned relative to other components. The value should be a number between 0 and 1 where 0 represents alignment along the origin, 1 is aligned the furthest away from the origin, 0.5 is centered, etc.

        return 0.5f;
    
protected java.awt.DimensiongetLayoutSize(java.awt.Container parent, boolean isPreferred)
Algorithm for calculating layout size (minimum or preferred).

The width of a graph paper layout is the largest cell width (calculated in getLargestCellSize() times the number of columns, plus the horizontal padding times the number of columns plus one, plus the left and right insets of the target container.

The height of a graph paper layout is the largest cell height (calculated in getLargestCellSize() times the number of rows, plus the vertical padding times the number of rows plus one, plus the top and bottom insets of the target container.

param
parent the container in which to do the layout.
param
isPreferred true for calculating preferred size, false for calculating minimum size.
return
the dimensions to lay out the subcomponents of the specified container.
see
java.awt.GraphPaperLayout#getLargestCellSize

        Dimension largestSize = getLargestCellSize(parent, isPreferred);
        Insets insets = parent.getInsets();
        largestSize.width = ( largestSize.width * gridSize.width ) +
            ( hgap * ( gridSize.width + 1 ) ) + insets.left + insets.right;
        largestSize.height = ( largestSize.height * gridSize.height ) +
            ( vgap * ( gridSize.height + 1 ) ) + insets.top + insets.bottom;
        return largestSize;
    
public voidinvalidateLayout(java.awt.Container target)
Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.

        // Do nothing
    
public voidlayoutContainer(java.awt.Container parent)
Lays out the container in the specified container.

param
parent the component which needs to be laid out

        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);
                }
            }
        }
    
public java.awt.DimensionmaximumLayoutSize(java.awt.Container target)
Returns the maximum size of this component.

see
java.awt.Component#getMinimumSize()
see
java.awt.Component#getPreferredSize()
see
LayoutManager

        return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
    
public java.awt.DimensionminimumLayoutSize(java.awt.Container parent)
Calculates the minimum size dimensions for the specified panel given the components in the specified parent container.

param
parent the component to be laid out
see
#preferredLayoutSize

        return getLayoutSize(parent, false);
    
public java.awt.DimensionpreferredLayoutSize(java.awt.Container parent)
Calculates the preferred size dimensions for the specified panel given the components in the specified parent container.

param
parent the component to be laid out
see
#minimumLayoutSize

        return getLayoutSize(parent, true);
    
public voidremoveLayoutComponent(java.awt.Component comp)
Removes the specified component from the layout.

param
comp the component to be removed

        compTable.remove(comp);
    
public voidsetConstraints(java.awt.Component comp, java.awt.Rectangle constraints)

        compTable.put(comp, new Rectangle(constraints));
    
public voidsetGridSize(java.awt.Dimension d)
Set the size of the graph paper in logical units (n x m)

        setGridSize( d.width, d.height );
    
public voidsetGridSize(int width, int height)
Set the size of the graph paper in logical units (n x m)

        gridSize = new Dimension( width, height );