Technical Support
Discussion Forum
Online Training
Read About Java
Java In-Depth
Product Discounts
Membership Information

Java Cup Logo

JDC Home Page


Working Applet
Help and Hints
Source Code
Table of Contents
Online Training
shadowSearchFAQFeedback

FontTest

Prerequisites

In this exercise you will write an applet that displays a list of the current fonts. To get the list of available fonts, ask for the current Toolkit (the class that maps AWT to the underlying native windowing toolkit) via Toolkit.getDefaultToolkit and then ask the toolkit for the font list via getFontList. In this example, you will be drawing in multiple fonts as opposed to building Label objects in multiple fonts. Unlike Label objects, you will need to know how tall letters are in each font, so that you can avoid drawing strings on top of one another.

Perform the following tasks:

  1. Create an applet called FontTest. In the init method, ask for, and store in an instance variable, the list of available fonts.
  2. In the paint method, construct a new Font from each name in the font list. Set the font of the graphics context passed in as an argument and get the font metrics (getFontMetrics). Draw the String 'fontList[i] + " 16 point Bold"' at 10 pixels over and n pixels below the last String where n is the height of the current font. Use a bold weight and 16 points.
The task numbers above are linked to the step-by-step help page. Also available is a complete solution that meets these requirements.

Related Exercises

  • None