Created
April 16, 2018 08:47
-
-
Save hellokaton/5a517d1a1beba64d6a59176d0b9c9dbb to your computer and use it in GitHub Desktop.
java 版本 文字生成 ascii 字符
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Font; | |
import java.awt.Graphics; | |
import java.awt.Graphics2D; | |
import java.awt.RenderingHints; | |
import java.awt.image.BufferedImage; | |
public class AsciiArt { | |
public void drawString(String text, String artChar, Settings settings) { | |
BufferedImage image = getImageIntegerMode(settings.width, settings.height); | |
Graphics2D graphics2D = getGraphics2D(image.getGraphics(), settings); | |
graphics2D.drawString(text, 6, ((int) (settings.height * 0.67))); | |
for (int y = 0; y < settings.height; y++) { | |
StringBuilder stringBuilder = new StringBuilder(); | |
for (int x = 0; x < settings.width; x++) { | |
stringBuilder.append(image.getRGB(x, y) == -16777216 ? " " : artChar); | |
} | |
if (stringBuilder.toString() | |
.trim() | |
.isEmpty()) { | |
continue; | |
} | |
System.out.println(stringBuilder); | |
} | |
} | |
private BufferedImage getImageIntegerMode(int width, int height) { | |
return new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); | |
} | |
private Graphics2D getGraphics2D(Graphics graphics, Settings settings) { | |
graphics.setFont(settings.font); | |
Graphics2D graphics2D = (Graphics2D) graphics; | |
graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); | |
return graphics2D; | |
} | |
public class Settings { | |
public Font font; | |
public int width; | |
public int height; | |
public Settings(Font font, int width, int height) { | |
this.font = font; | |
this.width = width; | |
this.height = height; | |
} | |
} | |
public static void main(String[] args) { | |
AsciiArt asciiArt = new AsciiArt(); | |
String text = "群主 666"; | |
Settings settings = asciiArt.new Settings(new Font("Ping Hei", Font.PLAIN, 18), text.length() * 30, 30); // 30 pixel width per character | |
asciiArt.drawString(text, "*", settings); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment