天天看點

通過weight權重控制字型的筆畫粗細

  這個需要配合泰山JDK8-u292-b273。

package taishan;
 
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.font.TextAttribute;
import java.util.HashMap;
 
@SuppressWarnings("serial")
public class WeightFontFrame extends TFrame
{
    private final static String FONT_NAME_DENGXIAN = "等線";
    private final static int    FONT_SIZE          = 54;
    
    private final static float  FONT_WEIGHT_05     = 0.5F;
    private final static float  FONT_WEIGHT_10     = 1.0F;
    private final static float  FONT_WEIGHT_15     = 1.5F;
    private final static float  FONT_WEIGHT_20     = 2.0F;
 
    private final static char[] TAISHAN = "泰山OFFICE".toCharArray();
 
    public WeightFontFrame()
    {
        this.getContentPane().setBackground(Color.WHITE);
    }
 
    @Override
    public void paint(Graphics g)
    {
        super.paint(g);
        
        float[] weights = new float[] {
                FONT_WEIGHT_05,
                FONT_WEIGHT_10,
                FONT_WEIGHT_15,
                FONT_WEIGHT_20
        };
        
        for (int i=0; i<weights.length; i++)
        {
            HashMap<TextAttribute, Object> attrs = new HashMap<TextAttribute, Object>();
            attrs.put(TextAttribute.FAMILY, FONT_NAME_DENGXIAN);
            attrs.put(TextAttribute.SIZE,   FONT_SIZE);
            attrs.put(TextAttribute.WEIGHT, weights[i]);
            Font testFont = new Font(attrs);
 
            g.setFont(testFont);
            g.drawChars(TAISHAN, 0, TAISHAN.length, 100, 80*(i+1));
        }
    }
    
    public static void main(String[] args)
    {
        WeightFontFrame frame = new WeightFontFrame();
        frame.setSize(600, 600);
        frame.setVisible(true);
    }
}      

效果:

通過weight權重控制字型的筆畫粗細

繼續閱讀