OFFICE技术讲座:JDK绘制旋转字体的效果(水平)

Posted 柳鲲鹏

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OFFICE技术讲座:JDK绘制旋转字体的效果(水平)相关的知识,希望对你有一定的参考价值。

  • 效果图100%

 

  • 效果图300%

  •  测试源码
package taishan;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.TextAttribute;
import java.awt.geom.AffineTransform;
import java.util.HashMap;

@SuppressWarnings("serial")
public class RotateFontFrameHorizental extends TFrame

    private final static String FONT_NAME  = "宋体";
    private final static int    FONT_SIZE  = 16;
    private final static int    POS_OFFSET = 100;
    
	private final static Color COLOR_LINE = Color.BLUE;
	private final static Color COLOR_FONT = Color.BLACK;
	
	private final static char[] TAISHAN = "泰山一!Office-!".toCharArray();
	private final static AffineTransform ROTATE_0   = AffineTransform.getRotateInstance( 0);
	private final static AffineTransform ROTATE_90  = AffineTransform.getRotateInstance( Math.PI / 2);
	private final static AffineTransform ROTATE_180 = AffineTransform.getRotateInstance( Math.PI   );
	private final static AffineTransform ROTATE_270 = AffineTransform.getRotateInstance( Math.PI *1.5);

	private Font[] rotateFonts = new Font[4];
	
	public RotateFontFrameHorizental()
	
	    this.getContentPane().setBackground(Color.WHITE);
	    
	    HashMap<TextAttribute, Object> attrs = new HashMap<TextAttribute, Object>();
	    attrs.put(TextAttribute.FAMILY,         FONT_NAME);
	    attrs.put(TextAttribute.SIZE,           FONT_SIZE);
	    Font temp = new Font(attrs);
	    
	    rotateFonts[0] = temp.deriveFont(ROTATE_0);
	    rotateFonts[1] = temp.deriveFont(ROTATE_90);
	    rotateFonts[2] = temp.deriveFont(ROTATE_180);
	    rotateFonts[3] = temp.deriveFont(ROTATE_270);
	

	private void antiAliasing(Graphics2D g2d)
	
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,   RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,   RenderingHints.VALUE_FRACTIONALMETRICS_ON);
	
	
	private void drawRotateFonts(Graphics g)
	
		antiAliasing((Graphics2D)g);
		
		int xunit = 20;
		int yunit = 50;
		
		g.setColor(COLOR_LINE);
		g.drawLine(POS_OFFSET, POS_OFFSET, POS_OFFSET, POS_OFFSET+200);
		
		for (int i=0; i<rotateFonts.length; i++)
		
			if (rotateFonts[i] == null)
			
				continue;
			
			
	    	int y = POS_OFFSET+yunit*i + 30;
	    	g.setColor(COLOR_LINE);
	    	g.drawLine(POS_OFFSET, y, POS_OFFSET+250, y);
	    	
		    g.setFont(rotateFonts[i]);
		    for (int j=0; j<TAISHAN.length; j++)
		    
		    	int x = POS_OFFSET+xunit*j;
		    	
		    	g.setColor(COLOR_FONT);
		    	g.drawChars(TAISHAN, j, 1, x, y);		    	
		    
		
	

	@Override
    public void paint(Graphics g)
    
		super.paint(g);
		
		drawRotateFonts(g);
    
	
	public static void main(String[] args)
    
		RotateFontFrameHorizental frame = new RotateFontFrameHorizental();
        frame.setSize(600, 600);
        frame.setVisible(true);
    


以上是关于OFFICE技术讲座:JDK绘制旋转字体的效果(水平)的主要内容,如果未能解决你的问题,请参考以下文章

OFFICE技术讲座:JDK绘制旋转字体的效果(垂直)

OFFICE技术讲座:JDK绘制旋转字体的效果(水平)

OFFICE技术讲座:JDK绘制旋转字体的效果(水平)

OFFICE技术讲座:由-(减号)的绘制差异,看不同字体引擎的处理

OFFICE技术讲座:由-(减号)的绘制差异,看不同字体引擎的处理

泰山OFFICE技术讲座:一个字符,用哪个字体绘制