您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes

Introduction

Content

Apply

Reflect

Extend

previous.gif
 (3087 bytes)

next.gif
 (2959 bytes)

 

Content Index

Content Page # 19

Fonts available for a particular system

Java provides a method that will return a list of the fonts available for the system Java is running on. A statement to invoke the method is as follows:

GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

The reply from getAvailableFontFamilyNames()is an array of Strings.

An example of an applet that uses this method outputs the following:

The code for the applet showing the contents of this String array of fonts is as follows:

import java.applet.Applet;

import java.awt.*;

public class FontList extends Applet
{

  public void paint( Graphics g )
  {
    int x = 20;
    int y = 20;
    String listOfFonts[]= GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
    int i;
    for( i = 0; i < listOfFonts.length; i++)
    {
      g.drawString(listOfFonts[i], x, y);
      y += 20;
    }
  }

} // class

As can be seen in the code, a simple forloop is used (with a loop variablei) to retrieve and display each of the String is in the array.

Back to top

basicline.gif (169 bytes)

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