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

Posted 柳鲲鹏

tags:

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

  • 效果图100%

  • 效果图300%

  •  测试源码-RotateFontFrameAbstract
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 abstract class RotateFontFrameAbstract extends TFrame

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

    protected Font[] rotateFonts = new Font[4];
	
	protected abstract void drawRotateFonts(Graphics g);

	public RotateFontFrameAbstract()
	
	    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);
	

	protected final void antiAliasing(Graphics2D g2d)
	
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,   RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,   RenderingHints.VALUE_FRACTIONALMETRICS_ON);
	
	
	@Override
    public final void paint(Graphics g)
    
		super.paint(g);
		
		drawRotateFonts(g);
    
	

  • 测试源码-RotateFontFrameHorizental
package taishan;

import java.awt.Graphics;

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


	@Override
	protected void drawRotateFonts(Graphics g)
	
		antiAliasing((java.awt.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);		    	
		    
		
	

	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技术讲座:一个字符,用哪个字体绘制