|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.ccg.awt.MultiLineText
public class MultiLineText
Class to draw text onto a graphic surface which may have multiple lines.
The java.awt package does not come with methods for
drawing multiple lines of text onto a graphical surface. The
purpose of this class is to simplify the process of drawing text
onto a graphical surface and provides the following features:
parse multiple lines from a
single string.
width and height required to draw all of the lines of text.
align the text within its drawing area
(left, right or center).
Align object and nicely
drawn.
Align| Field Summary | |
|---|---|
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. |
| Constructor Summary | |
|---|---|
MultiLineText()
Default constructor for the object. |
|
MultiLineText(String[] lines,
int align)
Construct the object with some lines of text. |
|
| Method Summary | |
|---|---|
static String[] |
breakString(String text)
Break a string up into an array of strings. |
void |
draw(Graphics g,
int x,
int y)
Draw the multi line text contained at the position specified. |
int |
getAlign()
Get how the text should be aligned when drawn. |
FontMetrics |
getFontMetrics()
Get the FontMetrics used to compute text size. |
int |
getHeight()
Get the height necessary to draw all lines. |
String |
getLine(int index)
Get one of the text lines which are displayed. |
int |
getLineLength(int index)
Get the length in pixels of one of the lines of text. |
int |
getNumLines()
Get the number of lines to display. |
int |
getWidth()
Get the width necessary to draw all lines. |
boolean |
isSizeKnown()
Indicates whether the size is known yet. |
protected boolean |
recompute()
Recomputes area to draw all text. |
void |
setAlign(int val)
Set how the text should be aligned when drawn. |
boolean |
setAlign(String val)
Set the alignment based on the contents of a string. |
void |
setFontMetrics(FontMetrics val)
Set the FontMetrics used to compute text size. |
void |
setLines(String[] val)
Set the text lines which are displayed. |
void |
setNewLines(String lines)
Parse a single line into multiple lines. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final int CENTER
setAlign(int),
Constant Field Valuespublic static final int LEFT
setAlign(int),
Constant Field Valuespublic static final int RIGHT
setAlign(int),
Constant Field Values| Constructor Detail |
|---|
public MultiLineText()
Constructed in this fashion, nothing will be drawn until text is assigned (and the width and height will both
be 0).
setNewLines(java.lang.String),
setLines(java.lang.String[])
public MultiLineText(String[] lines,
int align)
draw(java.awt.Graphics, int, int) the object. However, you won't be able
to retrieve height or width
information until you specify a FontMetrics object.
lines - An array of strings (one string per line). Null entries are
treated the same a "" and result in blank lines of output. You
may pass null (for no output/size).align - The alignment mode you wish to set (CENTER,
LEFT or RIGHT).draw(java.awt.Graphics, int, int)| Method Detail |
|---|
public static String[] breakString(String text)
string up into an array of strings.
This method looks at a string to see if it
contains any new line ("\n") characters. If the string does
contain new line characters, an array of strings will be created
where each element in the array contains one line from the
original array (with the "\n" character removed). For example, if
you pass "Hello\n\nWorld!", you'll get back a 3 element array {
"Hello", "", "World!" }. Here are some of the rules:
s - String to check and break apart (its OK to pass null)
string which doesn't
contain any new line ("\n") characters. Otherwise a array of
strings.draw(java.awt.Graphics, int, int)
public void draw(Graphics g,
int x,
int y)
Font which is currently set for the graphics
object, its possible that the size necessary to draw the text
will change. That is, width and height may return new values after invoking this
method.
You need to specify the top left corner where you want the text drawn. Think of the lines of text occupying a rectangular region - the (x,y) you specify will be treated as the upper left corner of where this rectangular region should be located.
Since this method invokes setFontMetrics(java.awt.FontMetrics) prior to
drawing the text, you do not need to set the FontMetrics
object prior to using this method. In other words, it is always
safe to call this method.
g - The Graphics object to draw with.x - The offset from the left side to the "drawing area". Text will
not appear to the left of this point.y - The offset from the top to the "drawing area". Text will not
appear above this point.breakString(java.lang.String)protected boolean recompute()
setFontMetrics(java.awt.FontMetrics),
setLines(java.lang.String[])public boolean isSizeKnown()
We can't compute the size necessary for the text we know what
FontMetrics to use. This method lets you
know that the getWidth(), getHeight(), and getLineLength(int) methods will return valid values.
setFontMetrics(java.awt.FontMetrics)public void setFontMetrics(FontMetrics val)
FontMetrics used to compute text size.
Note, you don't have to do this prior to using the draw(java.awt.Graphics, int, int)
method (it will invoke this method if necessary). However, you do
need to use this method before getWidth(), getHeight() or getLineLength(int) will return valid
information.
val - New FontMetrics value to assign.getFontMetrics()public FontMetrics getFontMetrics()
FontMetrics used to compute text size.
setFontMetrics(java.awt.FontMetrics)public void setNewLines(String lines)
setLines(breakString(lines). This allows you to pass a single string like
"Hello\nWorld" instead of an array of strings { "Hello", "World"
}.
text - A single string which may new line characters ("\n") to
separate each line.breakString(java.lang.String)public void setLines(String[] val)
val - New String[] value to assign (null is OK).getLine(int)public String getLine(int index)
index - Which line you want to retrieve (in the range of 0-getNumLines()). You can pass a index which is too large and
get null back - don't pass negative values though.
setLines(java.lang.String[]),
getNumLines()public int getLineLength(int index)
draw the text on the screen (or
explicitly set the FontMetrics), you can retrieve the length (in pixels) of each
line using this method. In other words, this method only returns
valid information if isSizeKnown() returns true.
index - Which line you want to retrieve the length of (in the range of
0-getNumLines()). You can pass a index which is too
large and get -1 back - don't pass negative values though.
setFontMetrics(java.awt.FontMetrics) was invoked or the object was drawn).getLine(int)public int getNumLines()
getLine(int)public int getWidth()
draw the text on the screen (or
explicitly set the FontMetrics), you can retrieve the width (in pixels) required to
draw all of the lines of text using this method. In other words,
this method only returns valid information if isSizeKnown() returns true.
getHeight()public int getHeight()
draw the text on the screen (or
explicitly set the FontMetrics), you can retrieve the height (in pixels) required to
draw all of the lines of text using this method. In other words,
this method only returns valid information if isSizeKnown() returns true.
getWidth()public void setAlign(int val)
This method allows you to specify how the lines of text are aligned as they are drawn within the computed drawing area.
val - Desired alignment (LEFT, CENTER, or RIGHT).getAlign()public boolean setAlign(String val)
CENTER if the string passed starts with "c" or "C". It will be
LEFT if the string passed starts with "l" or "L". It
will be RIGHT if the string starts with "r" or "R". If
you pass null, a zero length string, or a string which doesn't
start with one of the previously mentioned characters, then
nothing will be done and this method will return false.
This method returns true if the string passed was considered valid.
val - The alignment string to pass (you can pass null or "" - but
you'll get false back).
setAlign(int)public int getAlign()
LEFT, CENTER, or RIGHT).setAlign(int)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||