package others;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class FontTest
{
public static void main (String[] args)
{
BufferedImage image = new BufferedImage(130, 50, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D)image.getGraphics();
//抗锯齿
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, 130, 50);
g2d.setColor(Color.blue);
//System.out.println(new Font("幼圆", Font.BOLD, 18));
g2d.setFont(/* new Font("幼圆",Font.BOLD,18) */getFont());
g2d.drawString("张三", 0, 40);
g2d.dispose();
File file = new File("c:/newimage.png");
try
{
ImageIO.write(image, "png", file);
}
catch (IOException e)
{
e.printStackTrace();
}
listAllFont();
}
public static Font getFont(){
File ttfFile = new File("c:/SIMYOU.TTF");
Font font = null;
try
{
font = Font.createFont(Font.TRUETYPE_FONT, ttfFile);
font = font.deriveFont(Font.BOLD, 40f);
}
catch (FontFormatException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return font;
}
public static void listAllFont ()
{
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String fontNames[] = ge.getAvailableFontFamilyNames();
for (String fontName : fontNames)
{
System.out.println(fontName);
}
System.out.println("---------------------\n\n");
Font fonts[] = ge.getAllFonts();
for (Font font : fonts)
{
System.out.println(font);
}
}
}