Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Master Font Manipulation in Java

Any application that uses text, uses fonts. And depending on how wisely you plan, your fonts can either enhance your application or work against it. To get started, you need to understand how Java handles font objects and learn how to predict what your font-altering decisions will look like, regardless of the client system. Before you know it, you'll be a master of Java font manipulation.

hether you think very much about fonts or not, as an application user, you are constantly interacting with and manipulating fonts. Particularly in word processing programs, where it's common to change typestyles and typefaces for different parts of your document.

So too, the ability to change typestyles and typefaces is useful as an application programmer. As a simple example, think of the "About" screen, which most every program has. It is common to see the program name in large text, to help it stand out, and legal jargon and copyright notices in smaller sized text. You can see this in Microsoft Word's Help/About menu.

When writing computer programs, you want your user interface to be as intuitive as possible. This means you will use radio buttons and checkboxes and other visual components that your end users already understand. However, it also means that you want written information to stand out as clearly as possible.

As well as varying the size of text, you may want to enhance specific words—by rendering them in bold, perhaps, or italics—to draw readers' attention.

Also, you might want to consider how the reader will use the textual information you have provided. For instance, for information displayed on the screen, you might wish to use a sans-serif font such as Arial. However, for information that the user will print out from your application onto paper, you might wish to use a serif font such as Times Roman. These simple changes can help your user read and digest the information you want to convey.

So, with that in mind, let's see how you can manage your textual information in Java, to enhance your own applications.

Anatomy of a Java Font Object
In Java, fonts are represented by instances of the java.awt.Font class. A Font object comprises three parts: a font name (e.g. serif), style identifier (e.g. bold) and a point size (e.g. 10).

As with any Java object, we can create an instance of the Font object in memory at any time. Of course, a visual item like a font is only meaningful if we actually apply it to something displayed on the screen, such as the text on a button or a label. Just because we can create a font object—as in the following examples—doesn't necessarily mean anything is seen on the screen.

Here are some examples of declaring fonts:

Font littleFont = new Font("Monospaced", Font.PLAIN, 10); Font bigFont = new Font("Serif", Font.BOLD, 24);

In word processing programs we're accustomed to selecting specific fonts, by name, such as Arial, Garamond, Times New Roman, etc. However, because it was designed to run on as many different operating systems as possible, Java does not use specific font names because those names may be specific to a particular system. Rather, it uses symbolic names that refer to a particular type of font—"Monospaced" and "Serif." These names are translated to a specific font on the executing computer by the Java interpreter.

Table 1 describes the five font classes that are available.

Table 1. The Five Font Classes


Contains serifs on the edges of characters, e.g. Times Roman


A font without serifs, e.g. Helvetica


A non-proportional font, such as Courier, where the letter "i" takes as much space as the letter "w"


A font used for dialog screens, such as opening a file


A font used for user input on dialog screens, such as typing in a file name

The way the Java interpreter maps the font is through a file called fonts.properties. This file will easily be found in the lib subdirectory of your Java development environment, whether you're using Sun's JDK or a third-party Java implementation such as Borland JBuilder.

There are three style identifiers, namely PLAIN, BOLD, and ITALIC. These are constant values that belong to the Font object. You can apply both BOLD and ITALIC to a font at the same time. (Actually, you can apply any combination, but as you will appreciate, PLAIN has no effect unless it is used by itself.)

To actually use a Font object, you simply specify it as an argument to the setFont() method of any component—such as a button—or to an applet's Graphics object, which is used to paint the canvas. Any subsequent text-drawing command for that applet, such as drawString(),will use the specified font.

A second useful class is java.awt.FontMetrics. This object can give us detailed size and spacing information about text that has been rendered in a specific font. As you might imagine, different systems will have different fonts available, and so careful programmers will never presume that a word will take up the exact same amount of space on one screen that it will on another. So, FontMetrics is useful for retrieving information about how the font you choose is sized on a specific system.

The Graphics object has a getFontMetrics() method that gives us the information we need about how the font is sized for the font currently in use:

public void paint (Graphics g) { FontMetrics fm = g.getFontMetrics (); ... }

Listing 1 is a Java applet that draws a word in a particular font and then draws reference lines showing certain characteristics, namely, the font's baselines and its descent and ascent properties (described below).

Figure 1: Running the FontShow applet in Sun's AppletViewer.
Figure 2 : Running the FontShow applet in Internet Explorer.

The applet requires two parameters be passed to it: "word" (the word to be displayed) and "fontname" (the font style to be used). Once you've written the code to pass these parameters, try entering different values in the HTML page (see Listing 2) and re-running the applet.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date