daG*_*vis 7 java graphics fonts awt custom-font
如何解决Java中自定义字体的问题?
例如,我的应用程序使用的字体不在所有计算机上.我可以以某种方式将它包含在已编译的可执行文件中,然后从那里调用它,如果它在客户端计算机上不存在?
还有什么其他选择?我可以将所有字体字符作为图像(之前,在某些图形应用程序中),然后显示每个字符的图像......是否可以?
Boz*_*zho 18
这是我用来从.ttf文件加载字体文件的实用程序方法(可以捆绑):
private static final Font SERIF_FONT = new Font("serif", Font.PLAIN, 24);
private static Font getFont(String name) {
Font font = null;
if (name == null) {
return SERIF_FONT;
}
try {
// load from a cache map, if exists
if (fonts != null && (font = fonts.get(name)) != null) {
return font;
}
String fName = Params.get().getFontPath() + name;
File fontFile = new File(fName);
font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
ge.registerFont(font);
fonts.put(name, font);
} catch (Exception ex) {
log.info(name + " not loaded. Using serif font.");
font = SERIF_FONT;
}
return font;
}
Run Code Online (Sandbox Code Playgroud)
您可以在应用程序中包含该字体并"在运行中"创建它
InputStream is = this.getResourceAsStream(font_file_name);
Font font = Font.createFont(Font.TRUETYPE_FONT, is);
Run Code Online (Sandbox Code Playgroud)