MIDP 3: Text

Back | Tutorial Home | Next

Till MIDP 3.0, custom text rendering created lots of problems on Canvas using drawstring(). Text wrapping and scroll bars are not supported on Canvas and developers have grueling time writing these algorithms, After all the hard work, these algorithms ended up with lots of portability problems. MIDP 3.0 has a new Class Text and this class looks like god sent for the game developers who use Canvas extensively for screens like “How To Play”, “About Us” etc. Now They can use Graphics.drawImage() method to rendering these Text element.

Text elements can be created using following constructors

Text()
Text(char[] contents, int offset, int length, float width, float height)
Text(java.lang.String contents, float width, float height)

Character array or String can be used to create a Text element. In case of character array you can define the begging offset from where the characters have to be copied to Text element. Width and height parameters define the parameters of the rectangle in which this text should be rendered. If the text is larger than these boundaries then a scroll bar is added to this element.

To draw the text to the canvas use

Graphics. drawText(Text text, int x, int y)

Text class supports good set of methods to manipulate this element.

Text Manipulations

getText(int index, int length): Get a string from index to the specified length
delete(int index, int length): Delete characters from index to specified length.
insert(int index, char[] contents, int offset, int length): Insert into Text object at an index from a character array.
insert(int index, java.lang.String contents): Insert into Text object at an index from a String object.

Formating and Rendering Methods

getAlignment(): Get the alignment of Text. Alignments are defined by integer parameters - ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT, ALIGN_JUSTIFY and ALIGN_DEFAULT.
setAlignment(int alignment): Set the alignment of Text object.
setInitialDirection(int direction): Sets the direction in which Text is displayed. This is helpful in localization implementations. The text from Middle East are usually displayed from right to left. Initial Directions are defined by integer parameters - DIRECTION_LTR, DIRECTION_RTL, or DIRECTION_NEUTRAL.
getInitialDirection(): Get the direction in which Text is displayed.
getIndent(): Get the indent space used at the beginning of the text. This indent is in pixels.
setIndent(int indent): Sets the indent space used at the beginning of the text.
setSpacing(int spaceAbove, int spaceBelow): Set the line spacing above and below each line.
getSpaceAbove(): Get the spacing above each line.
getSpaceBelow(): Get the spacing below each line.
getARGBColor(int index): Get the color of character at specified index.
getBGARGBColor(): Get the background color of the Text.
setARGBColor(int color, int index, int length): Set the color characters.
setBGARGBColor(int color): Set the background color of the Text.
getFont():
Get the Font of Text.
getFont(int index): Get the Font of character at specified index.
setFont(Font font): Set the Font of the Text.
setFont(Font font, int index, int length): Set the Font of the Text using index.
getHyphenationChar(): Hyphens are the separators used when the word gets broken in a paragraph.
setHyphenationChar(char hyphen): Set your own custom hyphen.
setSize(float width, float height): Set width and height of the Text.
getTextHeight(): Get the permissible height of the text so that the whole text can be rendered in the given boundary.

Search and indexing Methods

getCaret(): Get the index of the caret.
getLineCount(): Get the number of lines.
lastRenderedIndex(): Get the index of the last character that fits into the bounding box.
setCaret(int index): Set the caret at the specified index.

Scrolling and Offsets

getScrollOffset(): Get scroll offset of the text.
setScrollOffset(int offset): Set the scroll offset of the text.
textFits(): Checks if the contents can be fully rendered within the current bounds of this Text.

Back | Tutorial Home | Next

site comments powered by Disqus

More Tutorials

MIDP for Beginners
MIDP Gaming
JSR 179: Location Based Services

This page is a part of a frames based web site. If you have landed on this page from a search engine click here to view the complete page.