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用字符写字符的更多相关文章

  1. java IO流 之 字符流

    字符是我们能读懂的一些文字和符号,但在计算机中存储的却是我们看不懂的byte 字节,那这就存在关于字符编码解码的问题.所以在学习Io流的字符流前我们先了解些关于编码问题. 一.字符集与字符编码 1.什 ...

  2. Java:IO流之字符流Reader、Writer详解

    java.io包中:字符流   字符流的两个抽象基类:   Reader         Writer   文件的读取:Reader抽象类(java.io包中) 直接子类的构造方法: FileRead ...

  3. java中字节流和字符流的区别

    流分类: 1.Java的字节流   InputStream是所有字节输入流的祖先,而OutputStream是所有字节输出流的祖先.2.Java的字符流  Reader是所有读取字符串输入流的祖先,而 ...

  4. java把html标签字符转普通字符(反转换成html标签)(摘抄)

    下面是java把html标签字符转换,我用了spring 包中的 org.springframework.web.util.HtmlUtils 了解了源代码并且进步了使用,发现写得真不错...同时也可 ...

  5. 【Java基础】【21IO(字符流)&字符流其他内容&递归】

    21.01_IO流(字符流FileReader) 1.字符流是什么 字符流是可以直接读写字符的IO流 字符流读取字符, 就要先读取到字节数据, 然后转为字符. 如果要写出字符, 需要把字符转为字节再写 ...

  6. JAVA是是如何处理字符的。

    String s = "fs123fdsa";//String变量 byte b[] = s.getBytes();//String转换为byte[] String t = new ...

  7. 缓冲字符流 java.io.BufferedWriter ,java.io.BufferedReader,缓冲字符输出流:PrintWriter

    package seday07; import java.io.IOException;import java.io.PrintWriter; /*** @author xingsir * 缓冲字符流 ...

  8. Java IO---字节流和字符流

    一.IO流简介 流 流是一个抽象概念,Java程序和外部设备(可以是硬盘上的文件,也可以是网络设备)之间的输入输出操作是基于流的. 流就好比水管中的水流,具有流入和流出,类比数据的输入和输出. Jav ...

  9. Java IO: 字节和字符数组

    原文链接  作者: Jakob Jenkov   译者:homesick 内容列表 从InputStream或者Reader中读入数组 从OutputStream或者Writer中写数组 在java中 ...

随机推荐

  1. ELF Format 笔记(九)—— Elf32_Sym 结构的 st_value 和 st_shndx 成员

    ilocker:关注 Android 安全(新手) QQ: 2597294287 前面的笔记中提到过 Elf32_Sym 结构,本篇笔记再写一下其中的 st_value 和 st_shndx 成员. ...

  2. nodejs模块——Event模块

    Node.js中,很多对象会发出事件.如,fs.readStream打开文件时会发出一个事件. 所有发出事件的对象都是events.EventEmitter的实例,可以通过require(" ...

  3. Linux 中如何卸载已安装的软件(转载)

            Linux 中如何卸载已安装的软件. Linux软件的安装和卸载一直是困扰许多新用户的难题.在Windows中,我们可以使用软件自带的安装卸载程序或在控制面板中的“添加/删除程序”来实 ...

  4. Superpixel Based RGB-D Image Segmentation Using Markov Random Field——阅读笔记

    1.基本信息 题目:使用马尔科夫场实现基于超像素的RGB-D图像分割: 作者所属:Ferdowsi University of Mashhad(Iron) 发表:2015 International ...

  5. webdriver的工作原理

    selenium1的原理就是使用js来驱动浏览器,因为现在基本不用,所以不做过多讨论,下面是我整理的webdriver的工作原理,大致就是通过命令请求webdriver,然后webdriver通过浏览 ...

  6. POJ 2540 Hotter Colder --半平面交

    题意: 一个(0,0)到(10,10)的矩形,目标点不定,从(0,0)开始走,如果走到新一点是"Hotter",那么意思是离目标点近了,如果是"Colder“,那么就是远 ...

  7. extra增强延迟加载

    这种配置和配置为lazy=true是一样的,但它的好处在于调用size/contains等方法时,并不查询整个集合的数据,而是发送一条sql语句来处理,只有真正在使用时才全部去查询整个集合

  8. 这几天在搞UNITY3D,感觉回到了AS2

    这几天在搞UNITY3D,感觉回到了AS2 代码没有MAIN, 代码在屏幕上,和元件绑定

  9. UBUNTU添加新的分辨率

    首先,直接运行xrandr查看下分辨率的情况: $ xrandr Screen 0: minimum 320 x 200, current 1280 x 1024, maximum 4096 x 40 ...

  10. oracl中的集合操作符

    1:union(并集)     union连接两条sql语句,并且去除两条sql语句重复的记录 2.union all(并集) 接两句sql语句,两句sql语句的和不用去掉重复的记录. 3:inter ...