From 25a118deb3ce44f7b8957d212350f4284446bb39 Mon Sep 17 00:00:00 2001 From: ocheja Date: Sun, 4 Mar 2018 01:42:05 +0900 Subject: [PATCH] Implement examples for ASCII Art in Java (#3753) --- .../java/com/baeldung/asciiart/AsciiArt.java | 62 +++++++++++++++++++ .../com/baeldung/asciiart/AsciiArtTest.java | 20 ++++++ 2 files changed, 82 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/asciiart/AsciiArt.java create mode 100644 core-java/src/test/java/com/baeldung/asciiart/AsciiArtTest.java diff --git a/core-java/src/main/java/com/baeldung/asciiart/AsciiArt.java b/core-java/src/main/java/com/baeldung/asciiart/AsciiArt.java new file mode 100644 index 0000000000..081717b4fa --- /dev/null +++ b/core-java/src/main/java/com/baeldung/asciiart/AsciiArt.java @@ -0,0 +1,62 @@ +package com.baeldung.asciiart; + +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 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; + } + } +} diff --git a/core-java/src/test/java/com/baeldung/asciiart/AsciiArtTest.java b/core-java/src/test/java/com/baeldung/asciiart/AsciiArtTest.java new file mode 100644 index 0000000000..103681894e --- /dev/null +++ b/core-java/src/test/java/com/baeldung/asciiart/AsciiArtTest.java @@ -0,0 +1,20 @@ +package com.baeldung.asciiart; + +import java.awt.Font; + +import org.junit.Test; + +import com.baeldung.asciiart.AsciiArt.Settings; + +public class AsciiArtTest { + + @Test + public void givenTextWithAsciiCharacterAndSettings_shouldPrintAsciiArt() { + AsciiArt asciiArt = new AsciiArt(); + String text = "BAELDUNG"; + Settings settings = asciiArt.new Settings(new Font("SansSerif", Font.BOLD, 24), text.length() * 30, 30); // 30 pixel width per character + + asciiArt.drawString(text, "*", settings); + } + +}