Choosing a font face is easy. First you create a new Font
object. Then you call g.setFont(Font f)
.
To instantiate a Font
object use this constructor:
public Font(String name, int style, int size)
name
is the name of the font family, e.g. "Serif",
"SansSerif", or "Mono".
size
is the size of the font in points. In computer
graphics a point is considered to be equal to one pixel. 12 points
is a normal size font. 14 points is probably better on most
computer displays. Smaller point sizes look good on paper printed
with a high resolution printer, but not in the lower resolutions of
a computer monitor.
style
is an mnemonic constant from java.awt.Font
that tells whether the text will be bold,
italic or plain. The three constants are Font.PLAIN
,
Font.BOLD
, and Font.ITALIC
. The program
below prints each font in its own face and 14 point bold.
import java.applet.*;
import java.awt.*;
public class FancyFontList extends Applet {
private String[] availableFonts;
public void init () {
Toolkit t = Toolkit.getDefaultToolkit();
availableFonts = t.getFontList();
}
public void paint(Graphics g) {
for (int i = 0; i < availableFonts.length; i++) {
Font f = new Font(availableFonts[i], Font.BOLD, 14);
g.setFont(f);
g.drawString(availableFonts[i], 5, 15*i + 15);
}
}
}