com.ccg.awt
Class Align

java.lang.Object
  extended by com.ccg.awt.Align

public class Align
extends Object

A "calculator" used to align rectangular regions.

This object provides a "calculator" of sorts with the ability of computing the top left corner of where a rectangular region should be drawn. It can be very helpful when drawing text onto graphical areas and can be effectively combined with the MultiLineText object to lay out text on a graphical drawing surface. Currently, it takes the following into acount:

The above sounds rather complicated - but maybe it will provide a little insight.

Since:
1.0
Version:
$Revision: 1.3 $
Author:
$Author: pkb $
See Also:
MultiLineText, parse(java.lang.String)

Field Summary
static int ABSOLUTE
          Constant used to indicate absolute anchor point.
static int BOTTOM
          Constant used to indicate BOTTOM alignment.
static int CENTER
          Constant used to indicate CENTER alignment.
static int LEFT
          Constant used to indicate LEFT alignment.
static int RIGHT
          Constant used to indicate RIGHT alignment.
static int TOP
          Constant used to indicate TOP alignment.
 
Constructor Summary
Align()
          Create a alignment calculator which will compute "centered" coordinates.
 
Method Summary
 void compute(int dx, int dy, int dw, int dh, int w, int h)
          Compute the (X,Y) coordinates of where a object should be placed.
 double getHorizontalAnchor()
          Get the horizontal anchor point
 int getHorizontalMode()
          Get current mode for horizontal alignment
 double getVerticalAnchor()
          Get the vertical anchor point
 int getVerticalMode()
          Get current mode for vertical alignment
 int getX()
          Get currently computed X coordinate
 int getY()
          Get currently computed Y coordinate
 boolean parse(String val)
          Parse the setup information for the object from a string.
 boolean parseHorizontal(String val)
          Parse the horizontal mode and anchor from a string.
 boolean parseVertical(String val)
          Parse the vertical mode and anchor from a string.
 void setHorizontalAnchor(double val)
          Set the horizontal anchor point This method allows one to specify the horizontal anchor point which will be used when computing the final location of the drawn object to be drawn.
 void setHorizontalMode(int val)
          Set current mode for horizontal alignment You can use this method to how you want your coordinates computed relative to the horizontal anchor point.
 void setVerticalAnchor(double val)
          Set the vertical anchor point This method allows one to specify the vertical anchor point which will be used when computing the final location of the object to be drawn.
 void setVerticalMode(int val)
          Set current mode for vertical alignment You can use this method to how you want your coordinates computed relative to the vertical anchor point.
 void setX(int val)
          Set currently computed X coordinate
 void setY(int val)
          Set currently computed Y coordinate
 String toString()
          Get a string representation of the object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CENTER

public static final int CENTER
Constant used to indicate CENTER alignment. This constant is used when you want center alignment. This can be used for both horizontal or vertical alignment. This means that the rectangle which bounds your object will be centered to the horizontal and/or vertical anchor point.

Since:
1.0
See Also:
setHorizontalMode(int), setVerticalMode(int), Constant Field Values

ABSOLUTE

public static final int ABSOLUTE
Constant used to indicate absolute anchor point. This constant is used to indicate that the anchor point is an absolute pixel location as opposed to a percent value. Most users of this class will NOT want to specify absolute coordinates and prefer to have the anchor point computed dynamically based upon the size of the area we are drawing into.

Since:
1.0
See Also:
setHorizontalAnchor(double), setVerticalAnchor(double), Constant Field Values

TOP

public static final int TOP
Constant used to indicate TOP alignment. This constant is used when you want top alignment. This means that the top of the rectangle which bounds your object will be set to the vertical anchor point.

Since:
1.0
See Also:
setVerticalMode(int), Constant Field Values

BOTTOM

public static final int BOTTOM
Constant used to indicate BOTTOM alignment. This constant is used when you want bottom alignment. This means that the bottom of the rectangle which bounds your object will be set to the vertical anchor point.

Since:
1.0
See Also:
setVerticalMode(int), Constant Field Values

LEFT

public static final int LEFT
Constant used to indicate LEFT alignment. This constant is used when you want left alignment. This means that the left side of the rectangle which bounds your object will be set to the horizontal anchor point.

Since:
1.0
See Also:
setHorizontalMode(int), Constant Field Values

RIGHT

public static final int RIGHT
Constant used to indicate RIGHT alignment. This constant is used when you want right alignment. This means that the right side of the rectangle which bounds your object will be set to the horizontal anchor point.

Since:
1.0
See Also:
setHorizontalMode(int), Constant Field Values
Constructor Detail

Align

public Align()
Create a alignment calculator which will compute "centered" coordinates. This is the default constructor for the class. It creates an object which when used with the compute method will result in fully centered alignment. It is as if you passed "cp.5,cp.5" to the parse method. You can use the various methods contained to adjust this.

Since:
1.0
See Also:
parse(java.lang.String), compute(int, int, int, int, int, int)
Method Detail

compute

public void compute(int dx,
                    int dy,
                    int dw,
                    int dh,
                    int w,
                    int h)
Compute the (X,Y) coordinates of where a object should be placed.

This method allows one to determine the top left corner where an object of a specific size (width/height) should be drawn onto a "drawing area". You do this by specifying a "drawing area" (rectangle defined by a top left starting point and width/height) and the size of an object which you wish to draw. Computations are then made (based upon the settings of this object) to determine the top left (x,y) where your object should be drawn.

After invoking this method, use getX() and getY() to retrieve the determined top left coordinates for where your object should be drawn.

Parameters:
dx - The x coordinate of the top left corner of the drawing area (0 is common).
dy - The y coordinate of the top left corner of the drawing area (0 is common).
dw - The width (in pixels) of the drawing area (the size of your drawing area is common).
dh - The height (in pixels) of the drawing area (the size of your drawing area is common).
w - The width of the object (in pixels) you wish to place onto the drawing area.
h - The height of the object (in pixels) you wish to place onto the drawing area.
Since:
1.0
See Also:
getX(), getY()

toString

public String toString()
Get a string representation of the object. This method returns the information about the object in a string form which is suitable for parsing (you can refer to parse(java.lang.String) for details on how the string will look).

Overrides:
toString in class Object
Returns:
String representation that can be parsed by parse(java.lang.String).
Since:
1.0
See Also:
parse(java.lang.String)

parse

public boolean parse(String val)
Parse the setup information for the object from a string. This method parses the horizontal mode/anchor and vertical mode/anchor from a string value. The string passed should contain two components (one for the horizontal and one for the vertical compontents. The two values will be ordered like "HOR,VER" in the string passed. Refer to parseHorizontal(java.lang.String) for specifics about the form of the "HOR" component. Refer to parseVertical(java.lang.String) for specifics about the form of the "VER" component. Here are some examples:
"cp.5,cp.5"
This indicates that the alignment should be centered around an anchor point which will be centered within the drawing area (this produces a fully centered alignment which handles dynamic sizing).
"la50,tp.1"
This indicates that the alignment will be horizontally left aligned from the absolute offset of 50. Vertically, the alignment will be top aligned 10% down from the top of the drawing area.
"rp.95,bp1.0"
This will cause the alignment to appear at the bottom right of the component (there will be a 5% gap to the right of and a 0% gap below).
"ra-10,ba-5"
This will cause the alignment with a 10 pixel gap on the right size of the drawing area and a 5 pixel gap at the bottom.

Parameters:
val - The string to parse (two components separated by commas). It is safe to pass null - you'll get false back.
Returns:
true if the string passed was successfully parsed.
Since:
1.0
See Also:
toString(), parseHorizontal(java.lang.String), parseVertical(java.lang.String)

parseHorizontal

public boolean parseHorizontal(String val)
Parse the horizontal mode and anchor from a string. This method parses the horizontal mode and anchor from a string. The string passed MUST contain three components "MPV" and must not contain any white space. Valid values look like "cp.5", "ra-10" and "la200". Here are the specifics:
M
Must be one of the characters: 'c' (for centering), 'l' (for left alignment), or 'r' (for right alignment).
P
Must be the character 'p' (if the value specified should be interpretted as a percent), or 'a' (if the value specified is an absolute pixel offset).
V
This is the numeric value to be used in the computation of the horizontal anchor point. It must be an integer value if absolute mode was specified with 'a'. It must be a value in the range of [-1.0,1.0] if the percent mode was specified with 'p'.

If the value specified ("V" from "MPV" as described above) for the anchor point is negative, then the anchor point will be computed from the right edge of the drawing area.

Parameters:
val - String value to parse.
Returns:
true if string value was valid and successfully parsed, false if not.
Since:
1.0
See Also:
parse(java.lang.String)

parseVertical

public boolean parseVertical(String val)
Parse the vertical mode and anchor from a string. This method parses the vertical mode and anchor from a string. The string passed MUST contain three components "MPV" and must not contain any white space. Valid values look like "cp.5" and "ta200". Here are the specifics:
M
Must be one of the characters: 'c' (for centering), 't' (for top alignment), or 'b' (for bottom alignment).
P
Must be the character 'p' (if the value specified should be interpretted as a percent), or 'a' (if the value specified is an absolute pixel offset).
V
This is the numeric value to be used in the computation of the vertical anchor point. It must be an integer value if absolute mode was specified with 'a'. It must be a value in the range of [-1.0,1.0] if the percent mode was specified with 'p'.

If the value specified ("V" from "MPV" as described above) for the anchor point is negative, then the anchor point will be computed from the bottom edge of the drawing area.

Parameters:
val - String value to parse.
Returns:
true if string value was valid and successfully parsed, false if not.
Since:
1.0
See Also:
parse(java.lang.String)

setHorizontalMode

public void setHorizontalMode(int val)
Set current mode for horizontal alignment You can use this method to how you want your coordinates computed relative to the horizontal anchor point. You can specify any combination of the ABSOLUTE, CENTER, LEFT and/or RIGHT flags.

Parameters:
val - New byte value (combination of flags) to assign.
See Also:
getHorizontalMode()

getHorizontalMode

public int getHorizontalMode()
Get current mode for horizontal alignment

Returns:
Current byte value assigned.
See Also:
setHorizontalMode(int)

setVerticalMode

public void setVerticalMode(int val)
Set current mode for vertical alignment You can use this method to how you want your coordinates computed relative to the vertical anchor point. You can specify any combination of the ABSOLUTE, CENTER, TOP and/or BOTTOM flags.

Parameters:
val - New byte value (combination of flags) to assign.
See Also:
getVerticalMode()

getVerticalMode

public int getVerticalMode()
Get current mode for vertical alignment

Returns:
Current byte value assigned.
See Also:
setVerticalMode(int)

setHorizontalAnchor

public void setHorizontalAnchor(double val)
Set the horizontal anchor point This method allows one to specify the horizontal anchor point which will be used when computing the final location of the drawn object to be drawn. How this value is interpretted depends upon whether the ABSOLUTE flag is set or not.
Percent
If the ABSOLUTE flag is NOT set in the horizontal mode, then this value will be treated as a "percent" (typically in the range of [-1.0,1.0]). This "percent" value will then be used to compute the anchor point based upon the size of the area we will be drawing on (this is the preferred way to specify an anchor point).
Absolute
If the ABSOLUTE flag is set, this value will be interpretted as an absolute pixel offset (it won't scale if the size of the area we are drawing into changes).

Positive values result in computations from the left edge of the "drawing area". Negative values result in computations from the right edge of the "drawing area".

Parameters:
val - New float value to assign.
See Also:
getHorizontalAnchor()

getHorizontalAnchor

public double getHorizontalAnchor()
Get the horizontal anchor point

Returns:
Current float value assigned.
See Also:
setHorizontalAnchor(double)

setVerticalAnchor

public void setVerticalAnchor(double val)
Set the vertical anchor point This method allows one to specify the vertical anchor point which will be used when computing the final location of the object to be drawn. How this value is interpretted depends upon whether the ABSOLUTE flag is set or not.
Percent
If the ABSOLUTE flag is NOT set in the vertical mode, then this value will be treated as a "percent" (typically in the range of [-1.0,1.0]). This "percent" value will then be used to compute the anchor point based upon the size of the area we will be drawing the object onto (this is the preferred way to specify an anchor point).
Absolute
If the ABSOLUTE flag is set, this value will be interpretted as an absolute pixel offset (it won't scale if the size of the area we are drawing into changes).

Positive values result in computations from the top edge of the "drawing area". Negative values result in computations from the bottom edge of the "drawing area".

Parameters:
val - New float value to assign.
See Also:
getVerticalAnchor()

getVerticalAnchor

public double getVerticalAnchor()
Get the vertical anchor point

Returns:
Current float value assigned.
See Also:
setVerticalAnchor(double)

setX

public void setX(int val)
Set currently computed X coordinate

Parameters:
val - New int value to assign.
See Also:
getX()

getX

public int getX()
Get currently computed X coordinate

Returns:
Current int value assigned.
See Also:
setX(int)

setY

public void setY(int val)
Set currently computed Y coordinate

Parameters:
val - New int value to assign.
See Also:
getY()

getY

public int getY()
Get currently computed Y coordinate

Returns:
Current int value assigned.
See Also:
setY(int)


Copyright 1998-1998-2006 null. All Rights Reserved.