通过weight权重控制字体的笔画粗细

229 阅读1分钟

这个需要配合泰山JDK8。

package taishan;
 
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.font.TextAttribute;
import java.util.HashMap;
 
import javax.swing.JFrame;
 
@SuppressWarnings("serial")
public class WeightFontFrame extends JFrame
{
    private final static String FONT_NAME_SONGTI = "宋体";
    private final static int    FONT_SIZE        = 72;
    private final static float  FONT_WEIGHT      = 0.5F;
 
	private final static char[] TAISHAN = "泰山OFFICE".toCharArray();
 
	public WeightFontFrame()
	{
	    this.getContentPane().setBackground(Color.WHITE);
	    
	    HashMap<TextAttribute, Object> attrs = new HashMap<TextAttribute, Object>();
	    attrs.put(TextAttribute.FAMILY, FONT_NAME_SONGTI);
	    attrs.put(TextAttribute.SIZE,   FONT_SIZE);
	    attrs.put(TextAttribute.WEIGHT, FONT_WEIGHT);
	    Font testFont = new Font(attrs);
		this.setFont(testFont);
	}
 
	@Override
    public void paint(Graphics g)
    {
		super.paint(g);
	    g.drawChars(TAISHAN, 0, TAISHAN.length, 100, 100);
    }
	
	public static void main(String[] args)
    {
		WeightFontFrame frame = new WeightFontFrame();
        frame.setSize(600, 400);
        frame.setVisible(true);
    }
}