Text and Fonts


To draw text in a frame, call the graphics object's drawString() method. The method's arguments are the string to be drawn followed by its x and y position. The x position is the leftmost pixel of the first character in the string. The y position is the location of the baseline. The baseline of a string is the bottom of all characters except g, j, p, q, and y, which descend below the baseline. When you write on lined paper, the lines are baselines. Figure 14.12 shows a string containing two characters that descend below the baseline.

click to expand
Figure 14.12: The baseline

The following code draws a string that contains six descending characters. It also draws the baseline in light gray:

 1. import java.awt.*;  2.  3. public class FontAndBaseline extends Frame  4. {  5.   FontAndBaseline()  6.   {  7.     setSize(200, 150);  8.   } 9. 10.   public void paint(Graphics g) 11.   { 12.     int x = 25; 13.     int yBaseline = 100; 14.     g.setColor(Color.lightGray); 15.     g.drawLine(x, yBaseline, 125, yBaseline); 16.     g.setColor(Color.black); 17.     g.drawString("just a gaping quay", x, yBaseline); 18.   } 19. 20. 21.   public static void main(String[] args) 22.   { 23.     FontAndBaseline fabl = new FontAndBaseline(); 24.     fabl.setVisible(true); 25.   } 26. }

The drawstring() call is on line 17. Figure 14.13 shows the frame.


Figure 14.13: Text and baseline in a frame

You can use the graphics object's setFont() method to control the font in which text is displayed. Calling setFont() before calling drawString() is a bit like calling setColor() before drawing or filling a shape. The setFont() call determines the font of all subsequent drawString() calls, until the next setFont() call.

A font has three properties:

  • Style

  • Size

  • Family

The style can be either plain, bold, italic, or bold- italic. The size is in pixel units.

The font families that are available to you vary from one machine to the next, but there are three that you can always count on:

  • Monospaced

  • Serif

  • Sans Serif

In Monospaced font, all characters are spaced equally. The spacing is determined by the font's size. It's difficult to read a dense block of text in Monospaced font, but it's ideal for source code. All the code listings in this book appear in Monospaced font.

Serif is a variable-width font, which means that characters have different widths. For example, i is narrower than m. This book is printed in a variable-width font. Notice how iiiiiiiiii is much narrower than mmmmmmmmmm, even though both are 10 letters long. Variable-width fonts are designed for easy reading of dense text, such as you see in a book or newspaper. This font uses serifs, which are small decorations on the tips of letters. Serifs improve readability in medium to large fonts, but are annoying in small fonts.

Sans Serif is a variable-width font that does not use serifs. ("Sans" is French for "without".) This font works best when the font is small enough that serifs would interfere with readability.

To use a font in Java, you must first construct an instance of the java.awt.Font class. The constructor takes three arguments: the family, the style, and the size. The family is a string; the style and size are ints. For these three families, the strings are Monospaced, Serif, and SansSerif. For plain, bold, and italic, the Font class provides public final static ints named Font.PLAIN, Font.BOLD, and Font.ITALIC. For bold-italic style, use Font.BOLD + Font.ITALIC.

After you construct an instance of Font, you can pass it into the setFont() method of a graphics object. The following code sets a 36-point bold-italic Serif font:

Font f = new Font("Serif", Font.PLAIN+Font.BOLD, 36); g.setFont(f);

When these lines are inserted between lines 16 and 17 in the previous example (that is, just before the drawString() call), the result is as shown in Figure 14.14.


Figure 14.14: Text in a frame

Your computer probably has dozens of fonts in addition to the three standard ones. Many of them may be more interesting and playful than Monospaced, Serif, and Sans Serif. The more stylized fonts tend to be appropriate in more limited situations.

When you use a non-standard font in a Java application, be aware that you're taking a risk. It's possible that the font won't be available on all computers that will be running your application. When this happens, all characters will appear on the screen as small empty rectangles.

The GraphicsEnvironment class contains information about a computer's graphics system, including the names of all available fonts. The class has a static method called getLocalGraphicsEnvironment(),which returns an instance of the class with all the data fields set to reflect the capabilities of the underlying computer. Another call is getAvailableFontFamilyNames(), which is not static. It returns the font families as an array of strings. So, to retrieve the array, you can do something like the following:

GraphicsEnvironment grenv =   GraphicsEnvironment.getLocalGraphicsEnvironment(); String[] names = grenv.getAvailableFontFamilyNames();

The Font Lab program lets you see all the fonts that are available on your computer. To run it, type java visual.FontLab. You will see the GUI shown in Figure 14.15.

click to expand
Figure 14.15: Font Lab

The pull-down menu in Font Lab's control panel lets you choose from among all of the fonts on your machine, as detected by getAvailableFontFamilyNames(). You can change the family, style, and size. Some families do not support all styles. Some have only plain and italic, and others have only plain. Figure 14.16 shows one of the more exotic fonts.

click to expand
Figure 14.16: Font Lab with an exotic font

Play around with the various fonts. Find one that seems serious and one that seems playful. How do their shapes differ? Select 16-point plain Serif. Reduce the size one point at a time until the font becomes hard to read. Do the same for SansSerif. You will probably find that SansSerif remains readable down to a slightly smaller size than Serif.




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net