java用字符写字符

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class CharWrittenInChar {
	public static void main(String[] args) {
		new CharWrittenInChar();
	}

	Scanner cin = new Scanner(System.in);
	char foreChar = '●', backChar = '○', writing = '福';
	BufferedImage pic;
	Font[] font;
	int from, to, cur;// 字体从哪里开始到哪里结束
	int width = 40, height;

	BufferedImage getPic() {
		Font f = new Font(font[cur].getName(), Font.BOLD, 150);
		BufferedImage bit = new BufferedImage(1000, 1000, BufferedImage.TYPE_3BYTE_BGR);
		Rectangle2D rec = bit.getGraphics().getFontMetrics(f).getStringBounds("" + writing, bit.getGraphics());
		bit = new BufferedImage((int) rec.getWidth() + 1, 1 + (int) rec.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
		Graphics gg = bit.getGraphics();
		gg.setColor(Color.black);
		gg.fillRect(0, 0, bit.getWidth(), bit.getHeight());
		gg.setFont(f);
		gg.setColor(Color.RED);
		gg.drawString("" + writing, 0, gg.getFontMetrics(f).getAscent());
		return bit;
	}

	void draw(BufferedImage bit, FileOutputStream file) throws IOException {
		height = bit.getHeight() * width / bit.getWidth();
		BufferedImage pic = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
		pic.getGraphics().drawImage(bit, 0, 0, width, height, null);
		String s = "\n\n\n" + font[cur].getName() + "------------\r\n\r\n";
		for (int j = 0; j < pic.getHeight(); j++) {
			for (int i = 0; i < pic.getWidth(); i++) {
				int k = pic.getRGB(i, j);
				if (k == -65536) {
					s += foreChar;
				} else {
					s += backChar;
				}
			}
			s += "\r\n";
		}
		cout(s);
		file.write(s.getBytes());
	}

	void output() {
		try {
			FileOutputStream file = new FileOutputStream(new File("result.txt"));
			for (cur = from; cur < to; cur++) {
				pic = getPic();
				draw(pic, file);
			}
			file.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	CharWrittenInChar() {
		GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
		font = e.getAllFonts();
		from = 0;
		to = 8;
		cur = 0;
		pic = getPic();
		output();
		run(); 
	}

	void run() {
		while (true) {
			cout("weidiao >");
			Scanner str = new Scanner(cin.nextLine());
			if (!str.hasNext()) {
				output();
				continue;
			}
			String cmd = str.next();
			if (cmd.equals("help")) {
				cout("Character Generater---made by weidiao.neu\n");
				cout("\t\tfore 前景字\n");
				cout("\t\tback 背景字\n");
				cout("\t\twriting 正在写\n");
				cout("\t\twidth 宽度\n");
				cout("\t\treset 重置全部字符及大小\n\n");
			} else if (cmd.equals("reset")) {
				foreChar = '●';
				backChar = '○';
				writing = '福';
				width = 40;
				output();
			} else {
				if (str.hasNext()) {
					switch (cmd) {
					case "fore":
						foreChar = str.next().charAt(0);
						output();
						break;
					case "back":
						backChar = str.next().charAt(0);
						output();
						break;
					case "writing":
						writing = str.next().charAt(0);
						output();
						break;
					case "width":
						width = new Integer(str.next());
						output();
						break;
					default:
						cout("illegal command\n");
					}
				} else {
					cout("illegal command\n");
				}
			}
			str.close();
		}
	}

	void cout(String s) {
		System.out.print(s);
	}
}

 

本程序用来"用字符写字符".给定一个字符,用很多个字符把这个字符拼起来.

原理是,把字符画在图片上(用Graphics.drawString函数),然后把这张图片画在一个固定大小的图片上.

现在,图片上只有两种颜色:前景色和背景色.

这就相当于一个二维矩阵,矩阵中有两种字符:前景字符和背景字符.

有三个东西可以进行设置:前景字符,背景字符,正在写的字符

你可能感兴趣的:(java用字符写字符)