Java Reference
In-Depth Information
which may help you keep the names of the three guaranteed fonts clear in your mind.
Serifs
are those small lines that sometimes finish off the ends of the lines in letters. For
example,
S
has serifs (at the two ends of the curved line), but
S
does not have serifs.
The
"Serif"
font will always have these decorative little lines.
Sans
means without, so
the
"SansSerif"
font will not have these decorative little lines. As you might guess,
"Monospaced"
means that all the characters have equal width.
Fonts can be given style modifiers, such as bold or italic, and they can come in
different sizes. The second and third arguments to the constructor for
Font
specify
the style modifications and size for the font, as in the following, which occurs in the
actionPerformed
method in Display 18.21 :
new
Font("Serif", Font.BOLD|Font.ITALIC, POINT_SIZE);
The second argument specifies style modifications. Note that you can specify
multiple style modifications by connecting them with the symbol | as in
Font.
BOLD|Font.ITALIC
.
2
The last argument specifies the size of the letters in the version
of the font created.
Character sizes are specified in units known as
points
, so the size of a particular
version of a font is called a
point size
. One
point
is 1/72 of an inch, but measurements
of font sizes are not as precise as might be ideal; two different fonts of the same point
size may be slightly different in size.
The method
setFont
sets the font for the
Graphics
object, which is named
g
in
Display 18.21. The font remains in effect until it is changed. If you do not specify any
font, then a default font is used.
There is no simple way to change the properties of the current font, such as making
it italic. Every change in a font normally requires that you define a new
Font
object
and use it as an argument to
setFont
.
Display 18.23 gives some useful details about constructors, methods, and constants
that are members of, or are related to, the class
Font
.
point size
The
drawString
Method
The
drawString
method writes the text given by the
String
at the point (
X, Y
) of the
Graphics_Object.
The text is written in the current font, color, and font size.
SYNTAX
Graphics_Object
.drawString
(
String, X, Y
);
EXAMPLE
g.drawString("I love you madly.", X_START, Y_START);
2
The symbol | produces a “bitwise or of the numbers,” but that detail need not concern you. You need
not even know what is meant by a “bitwise or of the numbers.” Just think of | as a special way to con-
nect style specifications.