public class GraphPaperLayout
extends java.lang.Object
implements java.awt.LayoutManager2
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));
}
}
Modifier and Type | Class and Description |
---|---|
static class |
GraphPaperLayout.EditPanel
Class EditPanel _more_
|
static class |
GraphPaperLayout.Location
Class Location _more_
|
Constructor and Description |
---|
GraphPaperLayout()
Creates a graph paper layout with a default of a 1 x 1 graph, with no
vertical or horizontal padding.
|
GraphPaperLayout(java.awt.Dimension gridSize)
Creates a graph paper layout with the given grid size, with no vertical
or horizontal padding.
|
GraphPaperLayout(java.awt.Dimension gridSize,
int hgap,
int vgap)
Creates a graph paper layout with the given grid size and padding.
|
Modifier and Type | Method and Description |
---|---|
void |
addLayoutComponent(java.awt.Component comp,
java.lang.Object constraints)
Adds the specified component to the layout, using the specified
constraint object.
|
void |
addLayoutComponent(java.lang.String name,
java.awt.Component comp)
Adds the specified component with the specified name to
the layout.
|
static java.awt.Dimension |
getDimensions(java.util.List locations)
_more_
|
java.awt.Dimension |
getGridSize() |
protected java.awt.Dimension |
getLargestCellSize(java.awt.Container parent,
boolean isPreferred)
Algorithm for calculating the largest minimum or preferred cell size.
|
float |
getLayoutAlignmentX(java.awt.Container target)
Returns the alignment along the x axis.
|
float |
getLayoutAlignmentY(java.awt.Container target)
Returns the alignment along the y axis.
|
protected java.awt.Dimension |
getLayoutSize(java.awt.Container parent,
boolean isPreferred)
Algorithm for calculating layout size (minimum or preferred).
|
void |
invalidateLayout(java.awt.Container target)
Invalidates the layout, indicating that if the layout manager
has cached information it should be discarded.
|
static javax.swing.JComponent |
layout(java.util.List locations)
_more_
|
void |
layoutContainer(java.awt.Container parent)
Lays out the container in the specified container.
|
static void |
main(java.lang.String[] args)
_more_
|
java.awt.Dimension |
maximumLayoutSize(java.awt.Container target)
Returns the maximum size of this component.
|
java.awt.Dimension |
minimumLayoutSize(java.awt.Container parent)
Calculates the minimum size dimensions for the specified
panel given the components in the specified parent container.
|
java.awt.Dimension |
preferredLayoutSize(java.awt.Container parent)
Calculates the preferred size dimensions for the specified
panel given the components in the specified parent container.
|
void |
removeLayoutComponent(java.awt.Component comp)
Removes the specified component from the layout.
|
void |
setConstraints(java.awt.Component comp,
java.awt.Rectangle constraints)
_more_
|
void |
setGridSize(java.awt.Dimension d)
Set the size of the graph paper in logical units (n x m)
|
void |
setGridSize(int width,
int height)
Set the size of the graph paper in logical units (n x m)
|
static void |
showDialog(java.util.List locations,
java.lang.String title)
_more_
|
public GraphPaperLayout()
public GraphPaperLayout(java.awt.Dimension gridSize)
gridSize
- public GraphPaperLayout(java.awt.Dimension gridSize, int hgap, int vgap)
gridSize
- size of the graph paper in logical units (n x m)hgap
- horizontal paddingvgap
- vertical paddingpublic java.awt.Dimension getGridSize()
public void setGridSize(java.awt.Dimension d)
d
- public void setGridSize(int width, int height)
width
- height
- public void setConstraints(java.awt.Component comp, java.awt.Rectangle constraints)
comp
- constraints
- public void addLayoutComponent(java.lang.String name, java.awt.Component comp)
addLayoutComponent
in interface java.awt.LayoutManager
name
- comp
- public void removeLayoutComponent(java.awt.Component comp)
removeLayoutComponent
in interface java.awt.LayoutManager
comp
- the component to be removedpublic java.awt.Dimension preferredLayoutSize(java.awt.Container parent)
preferredLayoutSize
in interface java.awt.LayoutManager
parent
- the component to be laid outminimumLayoutSize(java.awt.Container)
public java.awt.Dimension minimumLayoutSize(java.awt.Container parent)
minimumLayoutSize
in interface java.awt.LayoutManager
parent
- the component to be laid outpreferredLayoutSize(java.awt.Container)
protected java.awt.Dimension getLayoutSize(java.awt.Container parent, boolean isPreferred)
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.
parent
- the container in which to do the layout.isPreferred
- true for calculating preferred size, false for
calculating minimum size.getLargestCellSize(java.awt.Container, boolean)
protected java.awt.Dimension getLargestCellSize(java.awt.Container parent, boolean isPreferred)
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.
parent
- the container in which to do the layout.isPreferred
- true for calculating preferred size, false for
calculating minimum size.public void layoutContainer(java.awt.Container parent)
layoutContainer
in interface java.awt.LayoutManager
parent
- the component which needs to be laid outpublic void addLayoutComponent(java.awt.Component comp, java.lang.Object constraints)
addLayoutComponent
in interface java.awt.LayoutManager2
comp
- the component to be addedconstraints
- where/how the component is added to the layout.public java.awt.Dimension maximumLayoutSize(java.awt.Container target)
maximumLayoutSize
in interface java.awt.LayoutManager2
target
- Component.getMinimumSize()
,
Component.getPreferredSize()
,
LayoutManager
public float getLayoutAlignmentX(java.awt.Container target)
getLayoutAlignmentX
in interface java.awt.LayoutManager2
target
- public float getLayoutAlignmentY(java.awt.Container target)
getLayoutAlignmentY
in interface java.awt.LayoutManager2
target
- public void invalidateLayout(java.awt.Container target)
invalidateLayout
in interface java.awt.LayoutManager2
target
- public static java.awt.Dimension getDimensions(java.util.List locations)
locations
- _more_public static javax.swing.JComponent layout(java.util.List locations)
locations
- _more_public static void showDialog(java.util.List locations, java.lang.String title)
locations
- _more_title
- _more_public static void main(java.lang.String[] args)
args
- _more_