1. Overview
1.概述
In this article, we’ll discuss creating a graphical print of ASCII characters or Strings in Java, using concepts from the 2D graphics support of the language.
在这篇文章中,我们将讨论在Java中创建ASCII字符或Strings的图形打印,使用语言的2D图形支持中的概念。
2. Drawing Strings With 2D Graphics
2.用二维图形绘制字符串
With the help of the Graphics2D class, it’s possible to draw a String as an image, achieved invoking the drawString() method.
在Graphics2D类的帮助下,我们有可能将一个String画成一个图像,实现调用drawString()方法。
Because Graphics2D is abstract, we can create an instance by extending it and implementing the various methods associated with the Graphics class.
因为Graphics2D是抽象的,我们可以通过扩展它并实现与Graphics类相关的各种方法来创建一个实例。
While this is a tedious task, it’s often done by creating a BufferedImage instance in Java and retrieving its underlying Graphics instance from it:
虽然这是一项繁琐的工作,但通常是通过在Java中创建一个BufferedImage实例并从中检索其底层Graphics实例来完成的。
BufferedImage bufferedImage = new BufferedImage(
width, height,
BufferedImage.TYPE_INT_RGB);
Graphics graphics = bufferedImage.getGraphics();
2.1. Replacing Image Matrix Indices With ASCII Character
2.1.用ASCII字符替换图像矩阵索引
When drawing Strings, the Graphics2D class uses a simple matrix-like technique where regions which carve out the designed Strings are assigned a particular value while others are given a zeroth value.
当绘制Strings时,Graphics2D类使用了一个简单的类似矩阵的技术,其中划分出所设计的Strings的区域被赋予一个特定的值,而其他区域则被赋予一个零值。
For us to be able to replace the carved area with desired ASCII character, we need to detect the values of the carved region as a single data point (e.g. integer) and not the RGB color values.
为了使我们能够用所需的ASCII字符替换雕刻区域,我们需要将雕刻区域的数值检测为单一的数据点(如整数),而不是RGB颜色值。
To have the image’s RGB color represented as an integer, we set the image type to integer mode:
为了让图像的RGB颜色以整数表示,我们将图像类型设置为整数模式。
BufferedImage bufferedImage = new BufferedImage(
width, height,
BufferedImage.TYPE_INT_RGB);
The fundamental idea is to replace the values assigned to non-zero indices of the image matrix with the desired artistic character.
其基本思想是将分配给图像矩阵非零指数的数值替换为所需的艺术特征。
While indices of the matrix representing the zero value will be assigned a single space character. The zero equivalent of the integer mode is -16777216.
而代表零值的矩阵的索引将被分配一个空格字符。整数模式的零值相当于-16777216。
3. ASCII Art Generator
3.ASCII艺术发生器
Let’s consider a case where we need to make an ASCII art of the “BAELDUNG” string.
让我们考虑一种情况,我们需要对 “BAELDUNG “字符串进行ASCII艺术处理。
We begin by creating an empty image with desired width/height and the image type set to integer mode as mention in section 2.1.
我们首先创建一个空图像,其宽度/高度为所需,图像类型设置为2.1节中提到的整数模式。
To be able to use advanced rendering options of 2D graphics in Java, we cast our Graphics object to a Graphics2D instance. We then set the desired rendering parameters before invoking the drawString() method with the “BAELDUNG” String:
为了能够在Java中使用2D图形的高级渲染选项,我们将Graphics对象转换为Graphics2D实例。然后我们在调用drawString()方法之前设置所需的渲染参数,并使用 “BAELDUNG” String:。
Graphics2D graphics2D = (Graphics2D) graphics;
graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphics2D.drawString("BAELDUNG", 12, 24);
In the above, 12 and 24 represent respectively, the x and y coordinates for the point on the image where the text printing should start from.
在上面,12和24分别代表图像上文本打印开始的点的x和y坐标。
Now, we have a 2D graphics whose underlying matrix contains two types of discriminated values; non-zero and zero indices.
现在,我们有一个二维图形,其底层矩阵包含两种类型的判别值;非零和零指数。
But for us to get the concept, we will go through the 2-dimensional array (or matrix) and replace all values with the ASCII character “*” by:
但为了让我们得到这个概念,我们将通过二维数组(或矩阵),用ASCII字符 “*”替换所有数值。
for (int y = 0; y < settings.height; y++) {
StringBuilder stringBuilder = new StringBuilder();
for (int x = 0; x < settings.width; x++) {
stringBuilder.append("*");
}
if (stringBuilder.toString().trim().isEmpty()) {
continue;
}
System.out.println(stringBuilder);
}
The output of the above shows just a block of asterisks (*) as seen below:
如下图所示,上面的输出结果只显示了一个星号(*)块。
If we discriminate the replacement with “*” by replacing only the integer values equal to -16777216 with “*” and the rest with ” “:
如果我们用 “*”来区分替换,只用 “*”替换等于-16777216的整数值,其余的用””替换。
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 ? "*" : " ");
}
if (stringBuilder.toString().trim().isEmpty()) {
continue;
}
System.out.println(stringBuilder);
}
We obtain a different ASCII art which corresponds to our string “BAELDUNG” but in an inverted carving like this:
我们得到一个不同的ASCII艺术,它与我们的字符串 “BAELDUNG “相对应,但在一个倒置的雕刻中是这样的。
Finally, we invert the discrimination by replacing the integer values equal to -16777216 with ” ” and the rest with “*”:
最后,我们通过将等于-16777216的整数值替换为””,其余的替换为 “*”来反转歧视。
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 ? " " : "*");
}
if (stringBuilder.toString().trim().isEmpty()) {
continue;
}
System.out.println(stringBuilder);
}
This gives us an ASCII art of the desired String:
这给了我们一个所需的ASCII艺术字符串:。
4. Conclusion
4.结论
In this quick tutorial, we had a look at how to create ASCII art in Java using the inbuilt 2D graphics library.
在这个快速教程中,我们看了一下如何使用内置的2D图形库在Java中创建ASCII艺术。
While we have shown specifically for the text; “BAELDUNG”, the source code on Github provides a utility function that accepts any String.
虽然我们特别展示了文本;”BAELDUNG”,但Github上的源代码提供了一个接受任何String.的实用函数。
Source code, as always, can be found over on GitHub.
像往常一样,源代码可以在GitHub上找到超过。