ASCII art in Java

A memorandum that I played with because I wanted to decorate the log. Make the letters ASCII art. All can be made with the Java standard library. [Reference] https://www.baeldung.com/ascii-art-in-java

1. Class creation

ASCIIService.java


import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
 
public class ASCIIService {
 
    /**
     *Method to make ASCII art.
     * @param str Characters you want to make ASCII art
     * @return StringBuilder Characters that became ASCII art
     */
    public static StringBuilder createAscii(String str) {
 
        //The size of the graphic to generate
        int height =30;
        int width = 200;
        //Drawing start position
        int startX = 2;
        int stratY = 20;
        //Set font
        Font font = new Font("Arial", Font.BOLD, 14);
        StringBuilder sysout = new StringBuilder("\n");
         
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = bufferedImage.getGraphics();
         
        //Convert text to graphic
        Graphics2D g = (Graphics2D) graphics;
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.setFont(font);
        g.drawString(str, startX, stratY);
         
        //Transcription into ASCII art based on graphic
        for (int y = 0; y < height; y++) {
            StringBuilder stringBuilder = new StringBuilder();
          
            for (int x = 0; x < width; x++) {
                stringBuilder.append(bufferedImage.getRGB(x, y) == -16777216 ? " " : "@");
            }
            if (stringBuilder.toString().trim().isEmpty()) {
                continue;
            }
            sysout.append("\n" + stringBuilder);
        }
        sysout.append("\n");
        return sysout;
    }
 
    public static void main(String[] args) {
        //Run
        System.out.println(createAscii("PROCESS START!"));
    }
}

2. Run

If you were using eclipse Right click on class> Run> Java application image.png

You can also execute commands if the path is correct

How to pass the path https://www.javadrive.jp/start/install/index4.html How to execute the command https://pentan.info/java/command_line.html image.png

Remarks. I want to do it more easily

It seems that you can make it in one line by using the library https://qiita.com/hoge390281/items/808b9e9afddf5db41f20 image.png simple! cute!

Recommended Posts

ASCII art in Java
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
TCP communication (socket communication) in Java (ASCII, Binary)
[java] sort in list
Read JSON in Java
Make Blackjack in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
Fizzbuzz with ASCII art.
Comments in Java source
Azure functions in java
Format XML in Java
Simple htmlspecialchars in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
Try using RocksDB in Java
Avoid Yubaba's error in Java
Get EXIF information in Java
Save Java PDF in Excel
Edit ini in Java: ini4j
Java history in this world
Try calling JavaScript in Java
Try functional type in Java! ①
I made roulette in Java.
Create hyperlinks in Java PowerPoint
Implement two-step verification in Java
Refactoring: Make Blackjack in Java
Topic Analysis (LDA) in Java
NEologd preprocessing in Java neologdn-java
Change java encoding in windows
Java Stream API in 5 minutes
Cannot find javax.annotation.Generated in Java 11
Read standard input in Java
Implement Basic authentication in Java
Find a subset in Java
[Java] Get KClass in Java [Kotlin]
Create Azure Functions in Java
Import Excel data in Java