1 36 37 40 41 42 package java2d; 43 44 import java.awt.Font ; 45 import java.util.Map ; 46 import java.util.concurrent.ConcurrentHashMap ; 47 import java.io.File ; 48 import java.io.InputStream ; 49 import java.io.FileInputStream ; 50 51 52 55 public class DemoFonts { 56 57 private static String [] names = { "A.ttf" }; 59 private static Map <String ,Font > cache = 60 new ConcurrentHashMap <String ,Font >(names.length); 61 static { 62 for (String name : names) { 63 cache.put(name, getFont(name)); 64 } 65 } 66 67 68 public static Font getFont(String name) { 69 Font font = null; 70 if (cache != null) { 71 if ((font = cache.get(name)) != null) { 72 return font; 73 } 74 } 75 String fName = "/fonts/" + name; 76 try { 77 InputStream is = DemoFonts.class.getResourceAsStream(fName); 78 font = Font.createFont(Font.TRUETYPE_FONT, is); 79 } catch (Exception ex) { 80 ex.printStackTrace(); 81 System.err.println(fName + " not loaded. Using serif font."); 82 font = new Font ("serif", Font.PLAIN, 24); 83 } 84 return font; 85 } 86 } 87 | Popular Tags |