1,假设字体文件为 “宋体.ttf”
private static Font getSelfDefinedFont(String filename)
{
Font font = null;
//字体文件在conf下面
String filepath = RandomCodeChinese.class.getResource("/").getFile().replaceAll("%20", " ") + "../conf/" + filename;
File file = new File(filepath);
try
{
FileInputStream fi = new FileInputStream(file);
BufferedInputStream fb = new BufferedInputStream(fi);
font = Font.createFont(Font.TRUETYPE_FONT, fb);
}
catch (FontFormatException e)
{
return null;
}
catch (FileNotFoundException e)
{
return null;
}
catch (IOException e)
{
return null;
}
if("宋体.ttf".equals(filename))
{
font = font.deriveFont(Font.BOLD, 25);
}
else
{
font = font.deriveFont(Font.PLAIN, 25);
}
return font;
}