Java Reference
In-Depth Information
In addition to the current drawing color, the result of the
drawString()
method is influenced by the
current font. MIDP provides support for three different fonts in three different sizes and with the three
different attributes: bold, italic, and underlined.
A font is not selected directly, but the
setFont()
method takes a separate
Font
object, describing
the desired font, as a parameter. The explicit
Font
class provides additional information about the font,
such as its width and height in pixels, baseline position, ascent and descent, and so on.
Figure 3.14
illustrates the meaning of the corresponding values. This information is important for operations such
as drawing boxes around text strings. In addition, word-wrapping algorithms rely on the actual pixel
width of character strings when rendered to the screen.
Figure 3.14. Font properties and the corresponding query methods.
A
Font
object is created by calling the static method
createFont()
of the class
Font
in the
lcdui
package. The
createFont()
method takes three parameters: the font type, style, and size of
the font. Similar to the text alignment, there are predefined constants for setting the corresponding
Table 3.9.
createFont()
Property Constants
Property
Constants
Size
S
IZE_SMALL
,
SIZE_MEDIUM
,
SIZE_LARGE
Style
S
TYLE_PLAIN
,
STYLE_ITALICS
,
STYLE_BOLD
,
STYLE_UNDERLINED
Face
F
ACE_SYSTEM
,
FACE_MONOSPACE
,
FACE_PROPORTIONAL
The style constants can be combined—for example,
STYLE_ITALICS | STYLE_BOLD
will result
in a bold italics font style.
The following example shows a list of all fonts available, as far as the list fits on the screen of the
device:
import javax.microedition.lcdui.*;
class FontDemoCanvas extends Canvas {
static final int [] styles = {Font.STYLE_PLAIN,
Font.STYLE_BOLD,
Font.STYLE_ITALIC} ;
static final int [] sizes = {Font.SIZE_SMALL,
Font.SIZE_MEDIUM,
Font.SIZE_LARGE} ;
static final int [] faces = {Font.FACE_SYSTEM,
Font.FACE_MONOSPACE,
Font.FACE_PROPORTIONAL} ;
public void paint (Graphics g) {
Font font = null;
Search WWH ::
Custom Search