Java Reference
In-Depth Information
Typically, you use only the constructor of the
class
Font
. As shown in Table 12-4, the
constructor of the
class
Font
takes the following three arguments:
A string specifying the font face name (or font name for short)
An
int
value specifying the font style
An
int
value specifying the font size expressed in points, where 72
points equal one inch
Fonts available on different systems vary widely. However, using the JDK guarantees the
following fonts:
Serif
SanSerif
Monospaced
Dialog
DialogInput
If you want to know which fonts are available on your system, you can run the program
given next. (This program uses a graphics environment, which is covered later in this
chapter.)
import
java.awt.*;
public class
FontNames
{
public static void
main(String[] args)
{
String[] listOfFontNames =
GraphicsEnvironment.getLocalGraphicsEnvironment()
.getAvailableFontFamilyNames();
for
(
int
i = 0; i < listOfFontNames.length; i++)
System.out.println(listOfFontNames[i]);
}
}
The
class
Font
contains the constants
Font.PLAIN
,
Font.ITALIC
, and
Font.BOLD
,
which you can apply to change the style of a font. For example, the Java statement:
new
Font("Serif", Font.ITALIC, 12)
creates a 12-point
Serif
italic font. Likewise, the statement:
new
Font("Dialog", Font.ITALIC + Font.BOLD, 36)
creates a 36-point
Dialog
italic and bold font.
Search WWH ::
Custom Search