码迷,mamicode.com
首页 > 其他好文 > 详细

e652. Getting the Font Faces for a Font Family

时间:2018-09-03 00:06:03      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:lsp   nbsp   from   inf   order   environ   iter   cts   ons   

To create a Font object to draw text, it is necessary to specify the font face name. This example demonstrates how to retrieve all the font face names from a font family name. Unfortunately, the method is somewhat inefficient since it involves creating one-point size Font objects for every available font in the system. The example caches all the information by 建立一个散列表 that maps a font family name to an array of font face names.

See also e651 列出所有可用字体. Note: J2SE 1.4 only support True Type fonts.

    Map fontFaceNames = new HashMap();
    
    // Get all available font faces names
    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);
    
        }
        list.add(faceName);
    }
    
    // Replace the face name lists with string arrays,
    // which are more compact and convenient to use
    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 Examples

e652. Getting the Font Faces for a Font Family

标签:lsp   nbsp   from   inf   order   environ   iter   cts   ons   

原文地址:https://www.cnblogs.com/borter/p/9575444.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!