Using Fonts
Text fonts in Java are represented by instances of the
java.awt.Font class. A
Font object is constructed from a name,
style identifier, and a point size. We can create a Font object at any time, but it’s meaningful
only when applied to a particular component on a given display device.
Here are a couple of fonts:
FontsmallFont=newFont("Monospaced",Font.PLAIN,10);FontbigFont=newFont("Serif",Font.BOLD,18);
Font names come in three varieties: family names, face names (also called font names), and logical names. Family and font names are closely related. For example, Garamond Italic is a font name for a font whose family name is Garamond.
A logical name is a generic name for the font family. The following logical font names should be available on all platforms:
Serif(generic name forTimesRoman)SansSerif(generic name forHelvetica)Monospaced(generic name forCourier)DialogDialogInput
The logical font name is mapped to an actual font on the local platform. Java’s fonts.properties file maps the font names to the available fonts, covering as much of the Unicode character set as possible. If you request a font that doesn’t exist, you get the default font.
One of the big wins in the 2D API is that it can use most of the fonts you have installed on your computer. The following program prints out a full list of the fonts that are available to the 2D API:
//file: ShowFonts.javaimportjava.awt.*;publicclassShowFonts{publicstaticvoidmain(String[]args){