Class DrawString
- All Implemented Interfaces:
Computable
,Serializable
The second way to specify the position of the string is to set the reference point coordinates to null. In that case, the postioning constant gives the location of the STRING in the CorrdinateRect. A value of TOP_LEFT says that the string is in the top left corner of the rect, etc.
An array of Value objects can be specified to be displayed in the string. Their values are substituted for #'s in the string. (A double # in the string, however, is displayed as a literal single #.)
It is possible to set the color, font and justification of the string.
A DisplayString implements the Computable interface, so it can be added to a Controller. The values of the Value objects used by the string are recomputed only when its compute() method is called.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Color
If backgroundColor is non-null, then a rectangle of this color is filled as a background for the string;protected String
String, possibly with \n and #'s.static final int
Specify string location in rectstatic final int
Specify string location in rectstatic final int
Specify string location in rectstatic final int
For specifying justification of lines in multiline strings.static final int
Specify string location in rectstatic final int
Specify string location in rectstatic final int
Specify string location in rectprotected boolean
If true, the string is clamped to lie within the CoordinateRect.protected Color
Color of string.protected Font
Font for drawing string.protected Color
If frameWidth is greate than zero, then a frame is drawn around the string in this color.protected int
If frameWidth is greater than zero, then a frame of this width is drawn around the string in the color given by frameColor.protected int
Left, right, or center justification of lines in the text.static final int
For specifying justification of lines in multiline strings.protected int
Maximum number of characters desired in numbers; actual number might actually be larger.protected int
If absolute positioning is used, then this gives a gap between the string and edge of rect.protected int
one of the constants defined in this class for specifying positionstatic final int
For specifying justification of lines in multiline strings.protected String[]
The actual lines to draw, derived from baseString.static final int
Specify string location in rectstatic final int
Specify string location in rectstatic final int
Specify string location in rectprotected Value[]
Values to be substituted for #'s in the baseString.protected Value
xy-coords for drawing the string.protected Value
xy-coords for drawing the string. -
Constructor Summary
ConstructorsConstructorDescriptionCreate a DrawString object that initially has no string to draw.DrawString
(String str) Create a DrawString for drawing a black string in the top left corner of the coordinate rect.DrawString
(String str, int pos) Create a DrawString for drawing a black string in the position specified.DrawString
(String str, int pos, Value[] values) Create a DrawString for drawing a black string in the specified position.DrawString
(String str, int pos, Value xPos, Value yPos, Value[] values) Create a string that is displayed at the reference point (xPos,yPos); The positioning constant, pos, gives the positioning relative to this point, if xPos or yPos is non-null. -
Method Summary
Modifier and TypeMethodDescriptionvoid
compute()
The compute method sets up the array of strings that is actually displayed.void
Draws the string.Get the color that is used to fill a rectangle on which the string is drawn.boolean
getClamp()
Returns true if the string is set to be clamped to lie within the CoordinateRect.getColor()
Get the non-null color that is used for drawing the string.getFont()
Return the font that is used for drawing the string.Get the color that is used to draw a frame around the string.int
Get the width, in pixels, of the frame that is drawn around the string.int
Get the justification that is used for a multiple-line string.int
Return the desired maximum number of characters in displayed numbers.int
Get the distance of the bounding box of the string from the reference point where it is drawn.int
Return the positioning, as set by setPositioning().Get a copy of the display string (with \n's #'s, not with substitued values.)Value[]
Return the array of values that are substituted for #'s in the string.getXPos()
Return the Value object that gives the x-coordinate of the reference point of this string.getYPos()
Return the Value object that gives the y-coordinate of the reference point of this string.point of this string.void
setBackgroundColor
(Color color) Set the color that is used to fill a rectangle on which the string is drawn.void
setClamp
(boolean clamp) Set the "clamp" property of the DrawString.void
Set the color for the string.void
Set the font that is used for drawing this string.void
setFrameColor
(Color color) Set the color that is used to draw a frame around the string.void
setFrameWidth
(int width) Set the width, in pixels, of a frame to draw around the string.void
setJustification
(int j) Set the justification to be used if there are multiple lins in the string.void
setNumSize
(int size) Set the desired maximum number of characters in displayed numbers.void
setOffset
(int b) Set the distance of the bounding box of the string from the reference point where it is drawn.void
setPositioning
(int pos) Set the positioning of the string.void
setReferencePoint
(Value x, Value y) Set the values of the (x,y) coordinates of the reference point for the stirng.void
Set the string that is displayed.void
Set the Values that are substituted for (single) #'s in the string.Methods inherited from class edu.hws.jcm.draw.Drawable
getVisible, needsRedraw, setOwnerData, setVisible
-
Field Details
-
TOP_LEFT
public static final int TOP_LEFTSpecify string location in rect- See Also:
-
TOP_CENTER
public static final int TOP_CENTERSpecify string location in rect- See Also:
-
TOP_RIGHT
public static final int TOP_RIGHTSpecify string location in rect- See Also:
-
CENTER_LEFT
public static final int CENTER_LEFTSpecify string location in rect- See Also:
-
CENTER_CENTER
public static final int CENTER_CENTERSpecify string location in rect- See Also:
-
CENTER_RIGHT
public static final int CENTER_RIGHTSpecify string location in rect- See Also:
-
BOTTOM_LEFT
public static final int BOTTOM_LEFTSpecify string location in rect- See Also:
-
BOTTOM_CENTER
public static final int BOTTOM_CENTERSpecify string location in rect- See Also:
-
BOTTOM_RIGHT
public static final int BOTTOM_RIGHTSpecify string location in rect- See Also:
-
CENTER
public static final int CENTERFor specifying justification of lines in multiline strings. (But can also be used as a synonym for CENTER_CENTER to specify the position of the string).- See Also:
-
LEFT
public static final int LEFTFor specifying justification of lines in multiline strings. (But can also be used as a synonym for TOP_LEFT to specify the position of the string).- See Also:
-
RIGHT
public static final int RIGHTFor specifying justification of lines in multiline strings. (But can also be used as a synonym for TOP_RIGHT to specify the position of the string).- See Also:
-
position
protected int positionone of the constants defined in this class for specifying position -
baseString
String, possibly with \n and #'s. This is used as a base to get the actual string that is drawn. -
strings
The actual lines to draw, derived from baseString. -
values
Values to be substituted for #'s in the baseString. -
xPos
xy-coords for drawing the string. If non-null then relative positioning is used. If null, then positioning is absolute. -
yPos
xy-coords for drawing the string. If non-null then relative positioning is used. If null, then positioning is absolute. -
color
Color of string. If null, black is used as the default. -
font
Font for drawing string. If null, get font from graphics context. -
offset
protected int offsetIf absolute positioning is used, then this gives a gap between the string and edge of rect. For relative positioning, this gives an offset from the value of xPos yPos. -
clamp
protected boolean clampIf true, the string is clamped to lie within the CoordinateRect. -
justification
protected int justificationLeft, right, or center justification of lines in the text. -
numSize
protected int numSizeMaximum number of characters desired in numbers; actual number might actually be larger. -
backgroundColor
If backgroundColor is non-null, then a rectangle of this color is filled as a background for the string; -
frameWidth
protected int frameWidthIf frameWidth is greater than zero, then a frame of this width is drawn around the string in the color given by frameColor. -
frameColor
If frameWidth is greate than zero, then a frame is drawn around the string in this color. If the value is null, then the color will be the same as the color of the string.
-
-
Constructor Details
-
DrawString
public DrawString()Create a DrawString object that initially has no string to draw. -
DrawString
Create a DrawString for drawing a black string in the top left corner of the coordinate rect.- Parameters:
str
- The string to draw, which can contain \n's to indicate line breaks.
-
DrawString
Create a DrawString for drawing a black string in the position specified.- Parameters:
str
- The string to draw, which can contain \n's to indicate line breaks.pos
- The positioning of the string in the coordinate rect. One of the positioning constants such as TOP_LEFT or BOTTOM_RIGHT.
-
DrawString
Create a DrawString for drawing a black string in the specified position. The number of #'s in the string should match values.length. The values are computed and substituted for the #'s.- Parameters:
str
- The string to draw, which can contain \n's to indicate line breaks and #'s to be replaced by numeric values.pos
- The positioning of the string in the coordinate rect. One of the positioning constants such as TOP_LEFT or BOTTOM_RIGHT.values
- Value objects associated with #'s in the string.
-
DrawString
Create a string that is displayed at the reference point (xPos,yPos); The positioning constant, pos, gives the positioning relative to this point, if xPos or yPos is non-null.- Parameters:
str
- The string to draw, which can contain \n's to indicate line breaks and #'s to be replaced by numeric values.pos
- The positioning of the string. One of the positioning constants such as TOP_LEFT or BOTTOM_RIGHT. If xPos or yPos is non-nul, this is interpreted relative to their values.xPos
- x-coordinate relative to which the string is drawn (or null for absolute hoizontal positioning).yPos
- y-coordinate relative to which the string is drawn (or null for absolute vertical positioning).values
- Value objects associated with #'s in the string.
-
-
Method Details
-
setColor
Set the color for the string. If c is null, Color.black is used. -
getColor
Get the non-null color that is used for drawing the string. -
setFont
Set the font that is used for drawing this string. If f is null, then the font is obtained from the Graphics context in which the string is drawn. -
getFont
Return the font that is used for drawing the string. If the return value is null, then the font is taken from the Graphics context. -
setValues
Set the Values that are substituted for (single) #'s in the string. If the array of Values is null, then no substitution is done. The length of the array should match the number of #'s, but it is not an error if they do not match. Extra values will be ignored; extra #'s will be shown as "undefined". -
getValues
Return the array of values that are substituted for #'s in the string. -
setPositioning
public void setPositioning(int pos) Set the positioning of the string. The parameter should be one of the positioning contstants defined in this class, such as TOP_LEFT. (If it is not, TOP_LEFT is used by default.) -
getPositioning
public int getPositioning()Return the positioning, as set by setPositioning(). -
setReferencePoint
Set the values of the (x,y) coordinates of the reference point for the stirng. If a value is null, absolute positioning is used. If a value is undefined, the string is not drawn. -
getXPos
Return the Value object that gives the x-coordinate of the reference point of this string. -
getYPos
Return the Value object that gives the y-coordinate of the reference point of this string.point of this string. -
setString
Set the string that is displayed. Note that it can include '\n' to represent a line break, and it can contain #'s which will be replaced by computed values. -
getString
Get a copy of the display string (with \n's #'s, not with substitued values.) -
setOffset
public void setOffset(int b) Set the distance of the bounding box of the string from the reference point where it is drawn. The default value is 3. -
getOffset
public int getOffset()Get the distance of the bounding box of the string from the reference point where it is drawn. -
setClamp
public void setClamp(boolean clamp) Set the "clamp" property of the DrawString. If set to true, the string will be clamped to lie entirely within the CoordinateRect (unless it doens't fit -- then it can stick out on the right and bottom). The default value is true. -
getClamp
public boolean getClamp()Returns true if the string is set to be clamped to lie within the CoordinateRect. -
setJustification
public void setJustification(int j) Set the justification to be used if there are multiple lins in the string. Possible value are DrawString.LEFT, DrawString.RIGHT, and DrawString.CENTER. -
getJustification
public int getJustification()Get the justification that is used for a multiple-line string. The value is one of the constants DrawString.LEFT, DrawString.RIGHT, or DrawString.CENTER -
setNumSize
public void setNumSize(int size) Set the desired maximum number of characters in displayed numbers. Actual size might be larger. Value is clamped to the range 6 to 25. -
getNumSize
public int getNumSize()Return the desired maximum number of characters in displayed numbers. -
getBackgroundColor
Get the color that is used to fill a rectangle on which the string is drawn. Null indicates that no rectangle is filled so the stuff in back of the string shows though. The default value is null. -
setBackgroundColor
Set the color that is used to fill a rectangle on which the string is drawn. If the value is null, no rectangle is filled and the string just overlays whatever is in back of it on the canvas. -
getFrameColor
Get the color that is used to draw a frame around the string. This is only done if the frameWidth property is greater than zero. If the value is null, the frame is the same color as the string. -
setFrameColor
Set the color that is used to draw a frame around the string. This is only done if the frameWidth property is greater than zero. If the value is null, the frame is the same color as the string. -
getFrameWidth
public int getFrameWidth()Get the width, in pixels, of the frame that is drawn around the string. The default width is zero. The largest possible value is 25. -
setFrameWidth
public void setFrameWidth(int width) Set the width, in pixels, of a frame to draw around the string. If the value is zero, no frame is drawn. The default value is zero. The the value is clamped to the range 0 to 25. -
compute
public void compute()The compute method sets up the array of strings that is actually displayed. This is required by the Computable interface and is usually called by a Controller rather than directly.- Specified by:
compute
in interfaceComputable
-
draw
Draws the string.- Specified by:
draw
in classDrawable
- Parameters:
g
- The graphics context in which the Drawble is to be drawn. (The drawing can change the color in g, but should not permanently change font, painting mode, etc. Thus, every drawable is responsible for setting the color it wants to use.)coordsChanged
- Indicates whether the CoordinateRect has changed.
-