用Java生成ASCII艺术字

2015-01-20 Brian Li 更多博文 » 博客 » GitHub »

原文链接 https://bigosg.github.io/java/2015/01/20/ascii-art-java-example.html
注:以下为加速网络访问所做的原文缓存,经过重新格式化,可能存在格式方面的问题,或偶有遗漏信息,请以原文为准。


这是一个有趣的java列子,先来看下效果吧:

{% highlight java %}

          $$$$$$$       $$$$                                                                    
         $$$$$$$$$$     $$$$                                                                    
        $$$$$$$$$$$$    $$$$                                                                    
        $$$$$  $$$$$    $$$$                                                                    
       $$$$$    $$$$$   $$$$        $$            $$                                            
       $$$$$    $$$$$   $$$$     $$$$$$$$      $$$$$$$$$$$                                      
       $$$$$    $$$$$   $$$$    $$$$$$$$$$    $$$$$$$$$$$$                                      
       $$$$$    $$$$$   $$$$   $$$$$$$$$$$   $$$$$$$$$$$$$                                      
       $$$$$$  $$$$$$   $$$$   $$$$$  $$$$$  $$$$$   $$$$$                                      
        $$$$$$$$$$$$$   $$$$   $$$$    $$$$  $$$$$    $$$$                                      
        $$$$$$$$$$$$$   $$$$   $$$$$$$$$$$$  $$$$     $$$$                                      
          $$$$$$$$$$$   $$$$   $$$$$$$$$$$$  $$$$     $$$$                                      
                $$$$$   $$$$   $$$$$$$$$$$$  $$$$     $$$$                                      
                $$$$    $$$$   $$$$$         $$$$$   $$$$$                                      
               $$$$$    $$$$   $$$$$         $$$$$$ $$$$$$                                      
        $$$  $$$$$$     $$$$   $$$$$$$$$$$$  $$$$$$$$$$$$$                                      
        $$$$$$$$$$$     $$$$    $$$$$$$$$$$   $$$$$$$$$$$$                                      
        $$$$$$$$$       $$$$     $$$$$$$$$$    $$$$$$ $$$$                                      
         $$$$$$                     $$$$             $$$$$                                      
                                              $$    $$$$$$                                      
                                              $$$$$$$$$$$                                       
                                              $$$$$$$$$$$                                       
                                              $$$$$$$$$                                         

{% endhighlight %}

实现代码如下:

{% highlight java %} public class ASCIIArt {

public static void main(String[] args) throws IOException {

int width = 100;
int height = 30;

//BufferedImage image = ImageIO.read(new File("/users/zhujiajun/ascii-art.png"));

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setFont(new Font("SansSerif", Font.BOLD, 24));

Graphics2D graphics = (Graphics2D) g;
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphics.drawString("9leg", 10, 20);

//save this image
//ImageIO.write(image, "png", new File("/users/zhujiajun/ascii-art.png"));

for (int y = 0; y < height; y++) {
    StringBuilder sb = new StringBuilder();
    for (int x = 0; x < width; x++) {
        sb.append(image.getRGB(x, y) == -16777216 ? " " : "$");
    }

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

    System.out.println(sb);
}

}

}

{% endhighlight %}

-16777216这是什么玩意? 其实是颜色代码(256 * 256 * 256),在这个列子中"-16777216"被替换为空的字符串“ ”。可以读取保存的图片,并且打印出rgb color,会发现不同的颜色会有不同的代码。

英文原文链接{:target="_blank"}