[an error occurred while processing this directive]
Domain for sale!
Start Search Contents Index Links About

Font viewer

Managing fonts in Java

2002 - Week 35 - Havard Rast Blok

This week we will examine the font system of Java, and see how we obtain information about the fonts on the local system and then go on to use those fonts.

If you have not worked with fonts in Java before, it would be wise to study the related classes. The main class is java.awt.Font and if want to get the vector information from the fonts the neccesary classes are located in the package java.awt.font.

Now let's use the information from the local system to show a list of all available fonts. Another important class to study in this context is java.awt.GraphicsEnvironment. Look at the Java Doc, and it should be fairly simple to get an array of font names.

Hint: GraphicsEnvironment.getLocalGraphicsEnvironment()


FontNames.java

When you know the name, style and size of the font you want to use, it is possible to construct a Font object containing that information. Then you use that object to format your text. Make a small application that draws "Hello World" on a JFrame using font information from the command line.


FontViewer.java

Finally, this week, you have the chance to enhance the Java API with a class that is missing: the FontChooser. Bellow is a suggusted GUI for such a dialog box, made by Svein Ivar Kristiansen. You could also look at the javax.swing.colorchooser and javax.swing.filechooser packages for some guidance.



site: Håvard Rast Blok
mail:
updated: 16 July 2010