Activity
6 — Using fonts
Write a Java applet that displays the
font typefaces available to a program. When the user selects one of these typefaces, the
program should display some text in that typeface. The display should look something like
this (the user has clicked on the name 'serif.bolditalic'):

This exercise has a number of tricky features. Here
are a few hints to get you started (you may also wish to refer back to the FontList.java applet mentioned in this units Content
section):
- A Java program can get the list of available typefaces using
the line
String listOfFonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
- This puts the list of typeface names into the String
array listOfFonts[]. You can then add these
names to the font list (probably an object of class ‘List') using a for loop.
- When the user selects an item in a List object, Java sends
an event to the list's ‘item listerner' (ItemListener). The method addItemListener selects the object that will be the item listener and therefore
receive that event. Your applet itself can be the item listener, by specifying
‘implements ItemListener' in the class definition. ItemListener is
defined in java.awt.event.ItemListener, so your program will need to import this.
- If you make the text display an object of class label, you
can select its font easily by using the ‘setFont()' method. If you do this, your
applet probably won't need a paint method.
- You are also advised to look at the applet listing
AccountChooser.java, which illustrates how an array of Strings can be used to create a
List component:

Discussion of Activity 6
Back to Content
Back to top

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg |