The "Not a Hello, World" program at the beginning of this chapter displayed a string in the default font. Often, you want to show text in a different font. You specify a font by its font face name (or font name for short). A font face name is composed of a font family name, such as "Helvetica," and an optional suffix such as "Bold." For example, the font faces "Helvetica" and "Helvetica Bold" are both considered to be part of the family named "Helvetica." To find out which fonts are available on a particular computer, call the getAvailableFontFamilyNames method of the GraphicsEnvironment class. The method returns an array of strings that contains the names of all available fonts. To obtain an instance of the GraphicsEnvironment class that describes the graphics environment of the user's system, use the static getLocalGraphicsEnvironment method. Thus, the following program gives you a printout of the names of all fonts on your system:

import java.awt.*;
public class ListFonts
{
 public static void main(String[] args)
 {
 String[] fontNames = GraphicsEnvironment
 .getLocalGraphicsEnvironment()
 .getAvailableFontFamilyNames();
 for (int i = 0; i < fontNames.length; i++)
 System.out.println(fontNames[i]);
 }
}

On one system, the list starts out like this:

Abadi MT Condensed Light Arial Arial Black Arial Narrow Arioso Baskerville Binner Gothic
. . .

and goes on for another 70 fonts or so.Java graphics notes_icon

The SDK documentation claims that suffixes such as "heavy," "medium," "oblique," or "gothic" are variations inside within a single family. In our experience, that is not the case. The "Bold," "Italic," and "Bold Italic" suffixes are recognized as family variations, but other suffixes aren't.Unfortunately, there is no absolute way of knowing whether a user has a font with a particular "look" installed. Font names can be trademarked, and font designs can be copyrighted in some jurisdictions. Thus, the distribution of fonts often involves royalty payments to a font foundry. Of course, just as there are inexpensive imitations of famous perfumes, there are lookalikes for name-brand fonts. For example, the Helvetica and Times Roman imitations that are shipped with Windows are called Arial and Times New Roman. To establish a common baseline, the AWT defines five logical font names:

SansSerif Serif Monospaced Dialog DialogInput

These font names are always mapped to fonts that actually exist on the client machine. For example, on a Windows system, SansSerif is mapped to Arial. To draw characters in a font, you must first create an object of the class Font. You specify the font name, the font style, and the point size. Here is an example of how you construct a Font object:

Font helvb14 = new Font("Helvetica", Font.BOLD, 14);

The third argument is the point size. You can use a logical font name in the place of a font face name in the Font constructor. You specify the style (plain, bold, italic, or bold italic) by setting the second Font constructor argument to one of the following values:

Font.PLAIN Font.BOLD Font.ITALIC Font.BOLD + Font.ITALIC

Here is an example:

Font sansbold14 = new Font("SansSerif", Font.BOLD, 14)

Java graphics notes_icon

Prior versions of Java used the names Helvetica, TimesRoman, Courier, and ZapfDingbats as logical font names. For backward compatibility, these font names are still treated as logical font names even though Helvetica is really a font face name and TimesRoman and ZapfDingbats are not font names at all-the actual font face names are "Times Roman" and "Zapf Dingbats."Java graphics exclamatory_icon

Starting with SDK version 1.3, you can read TrueType fonts. You need an input stream for the font-typically from a disk file or URL. (See for more information on streams.) Then call the static Font.createFont method:

URL url = new URL("http://www.fonts.com/Wingbats.ttf");
InputStream in = url.openStream();
Font f = Font.createFont(Font.TRUETYPE_FONT, in);

The font is plain with a font size of 1 point. Use the deriveFont method to get a font of the desired size:

Font df = f.deriveFont(14.0F);
Java graphics caution_icon

There are two overloaded versions of the deriveFont method. One of them (with a float parameter) sets the font size, the other (with an int parameter) sets the font style. Thus, f.deriveFont(14) sets the style and not the size! (The result is to make an italic font because it happens that the binary representation of 14 sets the ITALIC bit but not the BOLD bit).The Java fonts contain the usual ASCII characters as well as symbols. For example, if you print the character '\u2297' in the Dialog font, then you get a Screenshotcharacter. Only those symbols that are defined in the Unicode character set are available. (See the sidebar at the end of this section for more information about available symbols and adding more fonts.) Here's the code that displays the string "Hello, World!" in the standard sans serif font on your system, using 14-point bold type:

Font sansbold14 = new Font("SansSerif", Font.BOLD, 14);
g2.setFont(sansbold14);
String message = "Hello, World!";
g2.drawString(message, 75, 100);

Next, let's center the string in its panel rather than drawing it at an arbitrary position. We need to know the width and height of the string in pixels. These dimensions depend on three factors:

To obtain an object that represents the font characteristics of the screen device, you call the getFontRenderContext method of the Graphics2D class. It returns an object of the FontRenderContext class. You simply pass that object to the getStringBounds method of the Font class:

FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = f.getStringBounds(message, context);

The getStringBounds method returns a rectangle that encloses the string. To interpret the dimensions of that rectangle, it is helpful to consider some basic typesetting terms (see ). The baseline is the imaginary line where, for example, the bottom of a character like "e" rests. The ascent is the distance from the baseline to the top of an ascender, which is the upper part of a letter like "b" or "k," or an uppercase character. The descent is the distance from the baseline to a descender, which is the lower portion of a letter like "p" or "g."

Typesetting terms illustrated

Java graphics 07fig13

Leading is the space between the descent of one line and the ascent of the next line. (The term has its origin from the strips of lead that typesetters used to separate lines.) The height of a font is the distance between successive baselines, which is the same as descent + leading + ascent. The width of the rectangle that the getStringBounds method returns is the horizontal extent of the string. The height of the rectangle is the sum of ascent, descent, and leading. The rectangle has its origin at the baseline of the string. The top y-coordinate of the rectangle is negative. Thus, you can obtain string width, height, and ascent as follows:

double stringWidth = bounds.getWidth();
double stringHeight = bounds.getHeight();
double ascent = -bounds.getY();

If you need to know the descent or leading, you need to use the getLineMetrics method of the Font class. That method returns an object of the LineMetrics class, which has methods to obtain the descent and leading:

LineMetrics metrics = f.getLineMetrics(message, context);
float descent = metrics.getDescent();
float leading = metrics.getLeading();

The following code uses all this information to center a string in its surrounding panel:

FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = f.getStringBounds(message, context);
// (x,y) = top left corner of text double x = (getWidth() - bounds.getWidth()) / 2;
double y = (getHeight() - bounds.getHeight()) / 2;
// add ascent to y to reach the baseline double ascent = -bounds.getY();
double baseY = y + ascent;
g2.drawString(message, (int)x, (int)(baseY));

To understand the centering, consider that getWidth() returns the width of the panel. A portion of that width, namely bounds.getWidth(), is occupied by the message string. The remainder should be equally distributed on both sides. Therefore the blank space on each side is half of the difference. The same reasoning applies to the height. Finally, the program draws the baseline and the bounding rectangle. shows the screen display; is the program listing.

Example FontTest.java

 1. import java.awt.*;
 2. import java.awt.font.*;
 3. import java.awt.geom.*;
 4. import javax.swing.*;
 5.
 6. public class FontTest
 7. {
 8. public static void main(String[] args)
 9. {
10. FontFrame frame = new FontFrame();
11. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
12. frame.show();
13. }
14. }
15.
16. /**
17. A frame with a text message panel
18. */
19. class FontFrame extends JFrame
20. {
21. public FontFrame()
22. {
23. setTitle("FontTest");
24. setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
25.
26. // add panel to frame
27.
28. FontPanel panel = new FontPanel();
29. Container contentPane = getContentPane();
30. contentPane.add(panel);
31. }
32.
33. public static final int DEFAULT_WIDTH = 300;
34. public static final int DEFAULT_HEIGHT = 200;
35. }
36.
37. /**
38. A panel that shows a centered message in a box.
39. */
40. class FontPanel extends JPanel
41. {
42. public void paintComponent(Graphics g)
43. {
44. super.paintComponent(g);
45. Graphics2D g2 = (Graphics2D)g;
46.
47. String message = "Hello, World!";
48.
49. Font f = new Font("Serif", Font.BOLD, 36);
50. g2.setFont(f);
51.
52. // measure the size of the message
53.
54. FontRenderContext context = g2.getFontRenderContext();
55. Rectangle2D bounds = f.getStringBounds(message, context);
56.
57. // set (x,y) = top left corner of text
58.
59. double x = (getWidth() - bounds.getWidth()) / 2;
60. double y = (getHeight() - bounds.getHeight()) / 2;
61.
62. // add ascent to y to reach the baseline
63.
64. double ascent = -bounds.getY();
65. double baseY = y + ascent;
66.
67. // draw the message
68.
69. g2.drawString(message, (int)x, (int)(baseY));
70.
71. g2.setPaint(Color.GRAY);
72.
73. // draw the baseline
74.
75. g2.draw(new Line2D.Double(x, baseY,
76. x + bounds.getWidth(), baseY));
77.
78. // draw the enclosing rectangle
79.
80. Rectangle2D rect = new Rectangle2D.Double(x, y,
81. bounds.getWidth(),
82. bounds.getHeight());
83. g2.draw(rect);
84. }
85. }

Drawing the baseline and string bounds

Java graphics 07fig14

java.awt.Font 1.0

Java graphics api_icon