Getting the Font Faces for a Font Family - Java 2D Graphics

Java examples for 2D Graphics:Font

Description

Getting the Font Faces for a Font Family

Demo Code

import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Main {
  public void m() {
    Map fontFaceNames = new HashMap();
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Font[] fonts = ge.getAllFonts();

    // Process each font
    for (int i = 0; i < fonts.length; i++) {
      // Get font's family and face
      String familyName = fonts[i].getFamily();
      String faceName = fonts[i].getName();

      // Add font to table
      java.util.List list = (java.util.List) fontFaceNames.get(familyName);
      if (list == null) {
        list = new ArrayList();
        fontFaceNames.put(familyName, list);

      }/*from  ww w .  j a v  a  2s .c  om*/
      list.add(faceName);
    }

    for (Iterator it = fontFaceNames.keySet().iterator(); it.hasNext();) {
      String familyName = (String) it.next();
      java.util.List list = (java.util.List) fontFaceNames.get(familyName);
      fontFaceNames.put(familyName, list.toArray(new String[list.size()]));
    }

    // Use the table
    String[] faces = (String[]) fontFaceNames.get("Verdana");
  }
}

Related Tutorials