Java Reference
In-Depth Information
For drawing text,
lcdui
provides the method
drawString()
. In addition to the basic
drawString()
method, several variants let you draw partial strings or single characters. (Details
about the additional methods can be found in the
lcdui
API documentation.) The simple
drawString()
method takes four parameters: The character string to be displayed, the x and y
coordinates, and an integer determining the horizontal and vertical alignment of the text. The alignment
parameter lets you position the text relative to any of the four corners of its invisible surrounding box.
Additionally, the text can be aligned to the text baseline and the horizontal center. The sum or logical
or (
|
) of a constant for horizontal alignment (
LEFT
,
RIGHT
, and
HCENTER
) and constants for vertical
anchor points for the valid constant combinations.
Figure 3.12. Valid combinations of the alignment constants and the corresponding
anchor points.
The following example illustrates the usage of the
drawString()
method. By choosing the anchor
point correspondingly, the text is displayed relative to the upper-left and lower-right corner of the
screen without overlapping the screen border:
import javax.microedition.lcdui.*;
class TextDemoCanvas extends Canvas {
public void paint (Graphics g) {
g.setGrayScale (255);
g.fillRect (0, 0, getWidth(), getHeight());
g.setGrayScale (0);
g.drawString ("Top/Left", 0, 0, Graphics.TOP |
Graphics.LEFT);
g.drawString ("Baseline/Center", getWidth() / 2, getHeight()
/ 2,
Graphics.HCENTER | Graphics.BASELINE);
g.drawString ("Bottom/Right", getWidth(), getHeight(),
Graphics.BOTTOM | Graphics.RIGHT);
}
}
Figure 3.13
shows the output of the
TextDemo
example.
Figure 3.13. Output of the
TextDemo
example.
Search WWH ::
Custom Search