Ascii Art - Java [closed]

佐手、 提交于 2019-12-03 18:30:40

问题


I want to make a program, where the user enters their name and the program generates an ascii picture of their name. Is it possible to do that without using 3rd party tools? What I came up with was to create each letter in the alphabet manually and then convert the string into a char array. Then return each image that corresponds with the letter. Is there a better or more efficent way of doing this?

Thanks.


回答1:


You can write text to a bitmap in any font you want and scan the bitmap to produce the ASCII art. You can even use anti-aliasing for effect. ;)


The last time I did this was about tens years ago. ;)

BufferedImage image = new BufferedImage(144, 32, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setFont(new Font("Dialog", Font.PLAIN, 24));
Graphics2D graphics = (Graphics2D) g;
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
        RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphics.drawString("Hello World!", 6, 24);
ImageIO.write(image, "png", new File("text.png"));

for (int y = 0; y < 32; y++) {
    StringBuilder sb = new StringBuilder();
    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);
}

writes out an image

and prints

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

change to BOLD

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

in ITALIC

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

Change the Font to SERIF BOLD

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



回答2:


This problem is solved in four steps.

  1. Create an ascii art representation (as a 2d rectangular array) for each letter you intend to support. Perhaps these are loaded from a resource file.
  2. Parse the input string into character tokens and acquire the art for those characters.
  3. Make a rectangular array that is as tall as the tallest token, as wide as the sum of each tokens' width (plus space between characters if not included in the images themselves).
  4. Copy each one into the giant array at the correct location.


来源:https://stackoverflow.com/questions/7098972/ascii-art-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!