Java Reference
In-Depth Information
Selecting Fonts
An object of type Font represents a font. The Font class is actually quite complicated, so we'll only
scratch the surface enough for our needs here. The Font class differentiates between a character , the
letter uppercase 'Q' say, and a glyph , which is the shape defining its appearance when it is displayed or
printed. In general a given character in one font will have a different glyph in a different font. For fonts
corresponding to many languages, German, French, or Finnish for example, a character may involve
more than one glyph to display it. This is typically the case for characters that involve diacritic marks ,
which are additional graphics attached to a character. The letter รค for instance combines the normal
letter 'a' with an umlaut, the two dots over it, so it may be represented by two glyphs, one for the
appearance of the letter and the other for the appearance of the umlaut. A Font object contains a table
that maps the Unicode value for each character to the glyph code or codes that create the visual
representation of the character.
To create a Font object you must supply the font name, the style of the font, and the point size. For
example, consider the following statement:
Font myFont = new Font("Serif", Font.ITALIC, 12);
This defines a 12-point Times Roman italic font. The other options you could use for the style are
PLAIN and BOLD . The name we have given to the font here, " Serif ", is a logical font name . Other
logical font names we could have used are " Dialog ", "DialogInput" , "Monospaced" ,
"SansSerif" or "Symbol" . Instead of a logical font name, we can supply a font face name - the
name of a particular font such as " Times New Roman " or " Palatino ".
It is important to keep in mind that fonts are for presenting characters visually, on the screen or on a printer
for instance. Although Java has a built-in capability to represent characters by Unicode codes, it doesn't have
any fonts because it doesn't display or print characters itself. The responsibility for this rests entirely with
your operating system. Although your Java programs can store strings of Japanese or Tibetan characters, if
your operating system doesn't have fonts for these characters you can't display or print them. Therefore to
display or print text in the way that you want, you need to know what font face names are available in the
system on which your code is running. We will come back to this in a moment.
You can specify combined styles by adding them together. If we want myFont to be BOLD and ITALIC
we would have written the statement as:
Font myFont = new Font("Serif", Font.ITALIC + Font.BOLD, 12);
You retrieve the style and size of an existing Font object by calling its methods getStyle() and
getSize() , both of which return a value of type int . You can also check the individual font style for
a Font object with the methods isPlain() , isBold() , and isItalic() . Each of these methods
returns a boolean value indicating whether the Font object has that style.
Before you create a font using a particular font face name, you need to know that the font is available on
the system where your code is executing. For this you need to use a method, getAllFonts() , in the
GraphicsEnvironment class defined in the java.awt package. We met this class earlier when we
were centering a window. We could do this as follows:
Search WWH ::




Custom Search