| java.awt.Rectangle gnu.jpdf.BoundingBox
BoundingBox | public class BoundingBox extends Rectangle (Code) | | This class simplifies the placement of Strings within
a canvas area where the placement of objects is absolute
A BoundingBox is just a Rectangle that knows how to
find the coordinates for a String based on the desired alignment and
FontMetrics . For each new String, a new child
BoundingBox is made that can be subtracted from the
original box so new Strings can be added
One of the more helpful features of this class is the string wrap
feature of getStringBounds . The box returned by that method
will contain an array of strings that have been broken down to fit the
box. The box's coordinates and size will reflect the size of the
entire group of strings if it is laid out as expected. Using the
returned box and iterating through the array of strings from top to
bottom, getting new bounding boxes for each one (with upper left
alignment and no padding) will result in the correct string wrap.
Note that you will need to have Xvfb running on a Unix server to
use this class
author: Eric Z. Beard, ericzbeard@hotmail.com author: $Author: ezb $ version: $Revision: 1.8 $, $Date: 2002/02/05 19:25:35 $ |
Method Summary | |
public void | add(BoundingBox child) Make the specified box this box's child. | public boolean | boxExists() | public void | drawChoppedString(Graphics g, FontMetrics fm, int padding, int hAlign) Take the first line of the string (if it is wrapped, otherwise just
take the whole string) and chop the end of it off to make it fit in the
box. | public void | drawWrappedString(Graphics g, FontMetrics fm, int padding, int hAlign) This method is called after getting the box by calling
getStringBounds on the parent. | public void | drawWrappedStringTruncate(Graphics g, FontMetrics fm, int padding, int hAlign) Draws lines from the wrapped string until there is no more room and
then stops. | public Point | getAbsoluteLocation() | public Point | getDrawingPoint() Gets the drawing point to use in Graphics drawing
methods. | public String | getFullString() | public BoundingBox | getParent() | public String[] | getStringArray() | public BoundingBox | getStringBounds(String string, int hAlign, int vAlign, FontMetrics fm, int padding, boolean enforce) Gets the location of a String after it is adjusted for
alignment within this box. | public BoundingBox | getStringBounds(String string, int hAlign, int vAlign, FontMetrics fm, int padding) Gets the location of a String after it is adjusted for
alignment within this box. | public boolean | hasParent() Returns true if this box has a parent. | public static void | main(String[] args) | public void | setAbsoluteLocation(Point point) | public void | setFullString(String string) | public void | setParent(BoundingBox parent) | public void | setStringArray(String[] strArray) | public BoundingBox | subtract(BoundingBox child, int subtractFrom) Removes the child box from this parent box. |
HORIZ_ALIGN_CENTER | final public static int HORIZ_ALIGN_CENTER(Code) | | Used to align a String horizontally in the center of the box
|
HORIZ_ALIGN_LEFT | final public static int HORIZ_ALIGN_LEFT(Code) | | Used to align a String to the left in the box
|
HORIZ_ALIGN_RIGHT | final public static int HORIZ_ALIGN_RIGHT(Code) | | Used to aling a String to the right in a box
|
LINE_SPACING_PERCENTAGE | final public static int LINE_SPACING_PERCENTAGE(Code) | | Percent f line height to space lines
|
SUBTRACT_FROM_BOTTOM | final public static int SUBTRACT_FROM_BOTTOM(Code) | | Used to subtract a child from a box, *leaving* the bottom portion
|
SUBTRACT_FROM_LEFT | final public static int SUBTRACT_FROM_LEFT(Code) | | Used to subtract a child from a box, *leaving* the left portion
|
SUBTRACT_FROM_RIGHT | final public static int SUBTRACT_FROM_RIGHT(Code) | | Used to subtract a child from a box, *leaving" the right portion
|
SUBTRACT_FROM_TOP | final public static int SUBTRACT_FROM_TOP(Code) | | Used to subtract a child from a box, *leaving* the top portion
|
VERT_ALIGN_BOTTOM | final public static int VERT_ALIGN_BOTTOM(Code) | | Used to align a String at the bottom of the box
|
VERT_ALIGN_CENTER | final public static int VERT_ALIGN_CENTER(Code) | | Used to a align a String centered vertically
|
VERT_ALIGN_TOP | final public static int VERT_ALIGN_TOP(Code) | | Used to align a String at the top of the box
|
BoundingBox | public BoundingBox(Point p, Dimension d)(Code) | | Creates a new BoundingBox instance.
Parameters: p - a Point , upper left coords Parameters: d - a Dimension , used to determine height and width |
add | public void add(BoundingBox child)(Code) | | Make the specified box this box's child. Equivalent to
child.setParent(parent) where the specified 'parent' is
this instance
Parameters: child - a BoundingBox , any box that can fit inside this one. The results of calling getAbsoluteLocation() on the child will be altered after this to take into account the child's new location in the 'world' |
boxExists | public boolean boxExists()(Code) | | Returns false if for any reason this box has negative dimensions
|
drawChoppedString | public void drawChoppedString(Graphics g, FontMetrics fm, int padding, int hAlign)(Code) | | Take the first line of the string (if it is wrapped, otherwise just
take the whole string) and chop the end of it off to make it fit in the
box. If the box is smaller than one letter, draw nothing
Parameters: g - the Graphics object to draw to Parameters: fm - the FontMetrics object to use for string sizing Parameters: padding - the int amount of padding around the string Parameters: hAlign - the int horizontal alignment |
drawWrappedString | public void drawWrappedString(Graphics g, FontMetrics fm, int padding, int hAlign) throws IllegalArgumentException, StringTooLongException(Code) | | This method is called after getting the box by calling
getStringBounds on the parent. Wraps the string at
word boundaries and draws it to the specified Graphics
context. Make sure padding is the same as specified for the
getStringBounds call, or you may get an unexpected
gnu.jpdf.StringTooLongException
Parameters: g - the Graphics object Parameters: fm - the FontMetrics to use for sizing Parameters: padding - an int, the padding around the strings Parameters: hAlign - the int horizontal alignment IllegalArgumentException if the args are invalid StringTooLongException if the string won't fit this will only happen if the fm or padding has been changed since getStringBounds was called succesfully |
drawWrappedStringTruncate | public void drawWrappedStringTruncate(Graphics g, FontMetrics fm, int padding, int hAlign)(Code) | | Draws lines from the wrapped string until there is no more room and
then stops. If there is no string or the box is too small for
anything to be drawn, does nothing
Parameters: g - the Graphics object to draw to Parameters: fm - the FontMetrics object to use for string sizing Parameters: padding - the int amount of padding around the string Parameters: hAlign - the int horizontal alignment |
getAbsoluteLocation | public Point getAbsoluteLocation()(Code) | | Get the absolute upper left location point for this box
a Point value |
getDrawingPoint | public Point getDrawingPoint()(Code) | | Gets the drawing point to use in Graphics drawing
methods. After getting a new BoundingBox with getStringBounds(),
calling this method will give you an absolute point, accounting
for alignment and padding, etc, from which to start drawing the
String
If getStringBounds was not called (this is a parent box), the
upper left coordinates will be returned (this.getLocation())
a Point |
getFullString | public String getFullString()(Code) | | Returns the full string associated with a call to
getStringBounds
|
getParent | public BoundingBox getParent()(Code) | | Get this box's parent box
a BoundingBox value |
getStringArray | public String[] getStringArray()(Code) | | Get the wrapped strings if this box was from a call to getStringBounds,
otherwise this method returns null
a String[] array of strings, top to bottom in layout |
getStringBounds | public BoundingBox getStringBounds(String string, int hAlign, int vAlign, FontMetrics fm, int padding, boolean enforce) throws IllegalArgumentException, StringTooLongException(Code) | | Gets the location of a String after it is adjusted for
alignment within this box. The point's coordinates are
either within this box or within the enclosing area.
Parameters: string - a String , the String to be placed Parameters: hAlign - an int , HORIZ_ALIGN_CENTER, HORIZ_ALIGN_LEFT, HORIX_ALIGN_RIGHT Parameters: vAlign - an int , VERT_ALIGN_CENTER, VERT_ALIGN_TOP, VERT_ALIGN_BOTTOM Parameters: fm - a FontMetrics object for this String Parameters: padding - an int , the padding around the String Parameters: enforce - a boolean , if true the method will throw an exception when the string is too big, if not true it will break the string down and overrun the bottom of the box. If the box is too small for even one word, the exception will be thrown a Point , the coords to use in drawString() See Also: BoundingBox.HORIZ_ALIGN_LEFT See Also: BoundingBox.HORIZ_ALIGN_CENTER See Also: BoundingBox.HORIZ_ALIGN_RIGHT See Also: BoundingBox.VERT_ALIGN_TOP See Also: BoundingBox.VERT_ALIGN_CENTER See Also: BoundingBox.VERT_ALIGN_BOTTOM IllegalArgumentException if the args are invalid StringTooLongException if the string won't fit and enforce is set to true. The exception can still be thrown if enforce is false, but only in cases such as the box having no height or width |
getStringBounds | public BoundingBox getStringBounds(String string, int hAlign, int vAlign, FontMetrics fm, int padding) throws StringTooLongException, IllegalArgumentException(Code) | | Gets the location of a String after it is adjusted for
alignment within this box. The point's coordinates are
either within this box or within the enclosing area.
By default, this method enforces string length and throws the
exception if it is too long
Parameters: string - a String , the String to be placed Parameters: hAlign - an int , HORIZ_ALIGN_CENTER, HORIZ_ALIGN_LEFT, HORIX_ALIGN_RIGHT Parameters: vAlign - an int , VERT_ALIGN_CENTER, VERT_ALIGN_TOP, VERT_ALIGN_BOTTOM Parameters: fm - a FontMetrics object for this String Parameters: padding - an int , the padding around the String a Point , the coords to use in drawString() IllegalArgumentException if the args are invalid StringTooLongException if the string won't fit |
hasParent | public boolean hasParent()(Code) | | Returns true if this box has a parent. The 'world', or
enclosing canvas is not considered a parent
a boolean value |
main | public static void main(String[] args)(Code) | | For testing
Parameters: args - a String[] value |
setAbsoluteLocation | public void setAbsoluteLocation(Point point)(Code) | | Set the absolute upper left world location point for this box
Parameters: point - a Point value |
setFullString | public void setFullString(String string)(Code) | | Sets the full string associated with getStringBounds
Parameters: string - a String |
setParent | public void setParent(BoundingBox parent)(Code) | | Make the specified box this box's parent
Parameters: parent - a BoundingBox value |
setStringArray | public void setStringArray(String[] strArray)(Code) | | Set the value of the string array
Parameters: strArray - a String array |
Methods inherited from java.awt.Rectangle | public void add(int newx, int newy)(Code)(Java Doc) public void add(Point pt)(Code)(Java Doc) public void add(Rectangle r)(Code)(Java Doc) public boolean contains(Point p)(Code)(Java Doc) public boolean contains(int x, int y)(Code)(Java Doc) public boolean contains(Rectangle r)(Code)(Java Doc) public boolean contains(int X, int Y, int W, int H)(Code)(Java Doc) public Rectangle2D createIntersection(Rectangle2D r)(Code)(Java Doc) public Rectangle2D createUnion(Rectangle2D r)(Code)(Java Doc) public boolean equals(Object obj)(Code)(Java Doc) public Rectangle getBounds()(Code)(Java Doc) public Rectangle2D getBounds2D()(Code)(Java Doc) public double getHeight()(Code)(Java Doc) public Point getLocation()(Code)(Java Doc) public Dimension getSize()(Code)(Java Doc) public double getWidth()(Code)(Java Doc) public double getX()(Code)(Java Doc) public double getY()(Code)(Java Doc) public void grow(int h, int v)(Code)(Java Doc) public boolean inside(int X, int Y)(Code)(Java Doc) public Rectangle intersection(Rectangle r)(Code)(Java Doc) public boolean intersects(Rectangle r)(Code)(Java Doc) public boolean isEmpty()(Code)(Java Doc) public void move(int x, int y)(Code)(Java Doc) public int outcode(double x, double y)(Code)(Java Doc) public void reshape(int x, int y, int width, int height)(Code)(Java Doc) public void resize(int width, int height)(Code)(Java Doc) public void setBounds(Rectangle r)(Code)(Java Doc) public void setBounds(int x, int y, int width, int height)(Code)(Java Doc) public void setLocation(Point p)(Code)(Java Doc) public void setLocation(int x, int y)(Code)(Java Doc) public void setRect(double x, double y, double width, double height)(Code)(Java Doc) public void setSize(Dimension d)(Code)(Java Doc) public void setSize(int width, int height)(Code)(Java Doc) public String toString()(Code)(Java Doc) public void translate(int dx, int dy)(Code)(Java Doc) public Rectangle union(Rectangle r)(Code)(Java Doc)
|
|
|