Java Reference
In-Depth Information
Table 3G.4
Useful Constants of the
Font
Class
Constant
Displays
Bold text
Font.BOLD
Italic text
Font.ITALIC
Bold/Italic text
Font.BOLD + Font.ITALIC
Plain text
Font.PLAIN
Table 3G.5
Common Font Names
Name
Description
a typewriter font, such as
Courier New
"Monospaced"
a font without curves (serifs) at letter edges, such as
Arial
"SansSerif"
a font with curved edges, such as Times New Roman
"Serif"
As in the case of colors, setting the font affects only strings that are drawn after
the font is set. The following program sets several fonts and uses them to draw
strings:
1 // Draws several messages using different fonts.
2
3
import
java.awt.*;
4
5
public class
DrawFonts {
6
public static void
main(String[] args) {
7
DrawingPanel panel =
new
DrawingPanel(200, 100);
8
panel.setBackground(Color.PINK);
9
10
Graphics g = panel.getGraphics();
11
g.setFont(
new
Font("Monospaced",
12
Font.BOLD + Font.ITALIC, 36));
13
g.drawString("Too big", 20, 40);
14
15
g.setFont(
new
Font("SansSerif", Font.PLAIN, 10));
16
g.drawString("Too small", 30, 60);
17
18
g.setFont(
new
Font("Serif", Font.ITALIC, 18));
19
g.drawString("Just right", 40, 80);
20
}
21 }
This program produces the output shown in Figure 3G.11.
Search WWH ::
Custom Search