关于字符的转换,输入一个字,让程序自动输出这个字,打字

package test;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

public class ArtString {

    public static String getArtStr(String str, Font font) {
        if (str == null || str.trim().length() == 0) return "";
        if (font == null) font = new Font("SansSerif", Font.PLAIN, 30);
        BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
        Graphics2D gd = bi.createGraphics();
        TextLayout tl = new TextLayout(str, font, gd.getFontRenderContext());
        Rectangle2D rect = tl.getBounds();
        int width = (int) Math.ceil(rect.getWidth()) + 3;
        int height = (int) Math.ceil(rect.getHeight());
        gd.dispose();
        bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        gd = bi.createGraphics();
        gd.setFont(font);
        gd.setBackground(Color.WHITE);
        gd.setColor(Color.WHITE);
        gd.fillRect(0, 0, width, height);
        gd.setColor(Color.BLACK);
        tl.draw(gd, 0f, (float) -rect.getY());
        bi.flush();
        gd.dispose();
        gd = null;
        int[] rgbs = bi.getRGB(0, 0, width, height, null, 0, width);
        bi = null;
        StringBuilder sb = new StringBuilder(rgbs.length);
        final int INTB = Color.BLACK.getRGB();
        final int INTW = Color.WHITE.getRGB();
        for (int i = 0, c = 0; i < rgbs.length;) {
            c = rgbs[i++];
            if (c == INTB) {
                sb.append("小");
            } else if (c == INTW) {
                sb.append(' ');
            }
            if (i % width == 0) sb.append('\n');
        }
        return sb.toString();
    }

    public static void main(String[] args) {
//        System.out.print(getArtStr("SageZk", null));
        System.out.print(getArtStr("点", null));
    }

}

 

黑色头发  http://heisetoufa.iteye.com

你可能感兴趣的:(java,C++,c,C#)