Share this page 

Display Ascii Banner Tag(s): String


Draw a string to an Image and then examine the Image created and output " ", "#" or "*" depending of the pixel value.

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.RenderingHints;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;



import javax.imageio.ImageIO;



public class AsciiBanner {



  public static void main (String ... args) throws IOException {

     // need to adjust for width and height

    BufferedImage image = new BufferedImage(144, 32, BufferedImage.TYPE_INT_RGB);

    Graphics g = image.getGraphics();

    g.setFont(new Font("Dialog", Font.PLAIN, 20));

    Graphics2D graphics = (Graphics2D) g;

    graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,

            RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

    // the banner text may affect width and height

    graphics.drawString("Real's HowTo", 6, 24);

    ImageIO.write(image, "png", File.createTempFile("AsciiBanner.png", null));



    // need to adjust for width and height

    for (int y = 0; y < 32; y++) {

        StringBuilder sb = new StringBuilder();

        // need to adjust for width and height

        for (int x = 0; x < 144; x++)

            sb.append(image.getRGB(x, y) == -16777216 ? " " :

               image.getRGB(x, y) == -1 ? "#" : "*");

        if (sb.toString().trim().isEmpty()) continue;

        System.out.println(sb);

    }

  }

}

Output is :

        ######***                          ##  ##                  ##       ##                           ############

        #########*                         ##  ##                  ##       ##                           ############

        ##    **#*                         ##  **                  ##       ##                                ##

        ##     *#*                         ##  **                  ##       ##                                ##

        ##     *#*    *******    *******   ##  **   ******         ##       ##   ******* *#*    *#*    *#*    ##       *******

        ##    **#*    *#####**  *######*   ##      *######*        ##       ##  **#####** ***   *#*   ****    ##      **#####**

        ########**   *#** **#*  *#****#*   ##      *#****#*        ###########  *#** **#* *#*  **#*   *#*     ##      *#** **#*

        ######***    *#*   *#*        #*   ##      *#*             ###########  *#*   *#* *#*  *#*#*  *#*     ##      *#*   *#*

        ##  ****     *#######*    ****##   ##      *##****         ##       ##  *#*   *#*  *#* *#*#* *#*      ##      *#*   *#*

        ##   *#**    *#######*  **######   ##      ***###**        ##       ##  *#     #*  *#**** ** *#*      ##      *#     #*

        ##    *#*    *#*        *#****##   ##        ****#*        ##       ##  *#*   *#*  ****#* *#****      ##      *#*   *#*

        ##    *##*   *#*        *#*  *##   ##           *#*        ##       ##  *#*   *#*   *#*#* *#*#*       ##      *#*   *#*

        ##     *#*   *#** **#*  *#* **##*  ##      *#****#*        ##       ##  *#** **#*   *#*#* *###*       ##      *#** **#*

        ##     **#*  **#####**  *#######*  ##      *######*        ##       ##  **#####**    *#*   *#**       ##      **#####**

        ##      *#*   *******   *******#*  ##       ******         ##       ##   *******     *#*   *#*        ##       *******