Browse DevX
Sign up for e-mail newsletters from DevX


Master Font Manipulation in Java  : Page 2

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.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

The program might look a bit complicated, but there's not really much to it. Most of the code is in the applet's paint() method, which merely sets the font, draws our word, and overlays a few lines that help illustrate some of the font's metrics. You can make use of the metrics information to format a screen without making any prior assumptions about the font.

By default, text is rendered above and to the right of the coordinates specified in the drawString() method. If that starting point is the origin of a coordinate system, then the axes are the baselines of the font, and here these lines are drawn in white.

In the sample application the ascent, where characters stretch above the baseline (such as the letter "l") is shown with a green line. Some fonts also have parts of letters that fall below the baseline (such as the letter "g"). This is called the descent and is shown in red.

The FontMetrics object has getMaxAscent() and getMaxDescent() methods that yield the lengths of the ascent and descent. The applet centers our word vertically in the display area by averaging the influence of the ascent and descent. FontMetrics also has a useful stringWidth() method which advises the width of the string when rendered in this font—we use this information for centering our word horizontally.

There are three other FontMetrics methods that I haven't used here—getLeading() gives the vertical spacing between multiple lines in the font, getHeight() gives the total line height (namely, ascent + descent + leading) in the font, and getMaxAdvance() returns the maximum character width of any character in the font.

That's really all there is to font handling in Java—or, at least as far as the native language goes. Sun has developed more font handling features in its new 2D graphics API libraries, including portable fonts. With portable fonts the font itself is actually taken and bundled into the Java program. This guarantees that the font used will be exactly the one that you selected, regardless of the operating system. However, this is a separate download and is not yet in widespread use or widely supported by Web browsers.

Listings 3 and 4 contain a second applet, AllFonts.java, which lists a sample of each font class. You can use this list to see the appearance of a given message in different fonts and across different operating systems.

Figure 3: Running the AllFonts applet in Sun's AppletViewer. Pictured is a sample run of the AllFonts applet, running in Sun's AppletViewer on a Sun Solaris computer.
Figure 4 : Running the AllFonts applet in Internet Explorer. Pictured is a sample run of the AllFonts applet, running in Internet Explorer on a Windows PC.

Less Can Be More
If you can apply everything demonstrated in this article in a live application, then you can consider yourself a Java font master! You should have all the information you need to change the type and style of fonts in your own Java programs and applets to enhance your presentation and help the user.

However, do use this wisely; overuse of font styles is distracting. Many of us remember the rise of desktop publishing back in the '80s and all the kitsch fliers, brochures, and other documents that tried to cram as many possible font changes as possible into a single page. The purpose of changing your font type and style is to enhance communications, making your programs clearer and more intuitive. So, keep in mind then, that too many font changes can have the opposite effect.

One thing I like to do when considering a new visual technique is to explore how other programmers have implemented it. For example, if you're making a menu bar, it makes sense to follow the established conventions of software manufacturers who generally use the menu names "File," "Edit," and "View" and always put the "Help" menu at the far right.

You can follow similar logic when selecting fonts. Where have other developers varied the font type or style? Did they use a particular type of font for screen information as opposed to print information? The more your application conforms to commonly-accepted standards, the more natural and intuitive your users will find it.

David M. Williams is an Australian IT writer, trainer, and consultant. He is the author of Win32 Font Lister and other shareware programs. Reach him via e-mail at .
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