Java character write characters

Source: Internet
Author: User

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 = ' 0 ', writing = ' blessing '; BufferedImage pic; Font[] Font;int from, to, cur;//font from where to start to where to end int width = max, height; BufferedImage getpic () {font f = new Font (Font[cur].getname (), Font.Bold, 150); BufferedImage bit = new BufferedImage (+, +, 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);p ic.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 ();d Raw (pic, file);} File.close ();} catch (Exception e) {e.printstacktrace ();}} Charwritteninchar () {graphicsenvironment E = graphicsenvironment.getlocalgraphicsenvironment (); font = E.getallfonts (); from = 0;to = Font.length;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 foreground word \ n "); cout (" \t\tback background word \ n "), cout (" \t\twriting is writing \ n "), cout (" \t\twidth width \ n "), cout (" \t\treset resets all characters and size \ n ");} else if (cmd.equals ("reset")) {Forechar = '; Backchar = ' 0 '; writing = ' blessing '; 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);}}

This program is used to "write characters by character". Given a character, use a number of characters to spell the character together.

The principle is to draw the character on the picture (with the Graphics.DrawString function) and then draw the picture on a fixed-size image.

Now there are only two colors on the picture: the foreground and background color.

This is equivalent to a two-dimensional matrix in which there are both characters: foreground character and background character.

There are three things that can be set: foreground characters, background characters, characters being written

Java character write characters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.