In: Computer Science
Write a simple java program that produces any text art ("ASCII
art") picture. Your program can produce any picture you like, with
the following restrictions:
• The picture should consist of between 3 and 200 lines of output,
with no more than 100 characters per line.
• The code must use at least one for loop or static method but
should not contain infinite loops.
SOLUTION:
The following is an example of Simple ASCII art using Java Programming, that prints a text using ASCII character.
PROGRAM
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
public class AsciiArt {
public static void main(String[] args) throws IOException {
// Setting The Width and Height of the ASCII Image
int width = 100;
int height = 100;
// Setting the Image and Graphics Property
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
Graphics g = img.getGraphics();
g.setFont(new Font("Arial", Font.PLAIN, 28));
// Setting the Graphics2D property to control the image co-ordinates
Graphics2D graphics = (Graphics2D) g;
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
// Positioning the Output on screen
graphics.drawString("PEACE", 10, 30);
for (int y = 0; y < height; y++) {
// StringBuilder Class is used to append data of any type
StringBuilder s = new StringBuilder();
for (int x = 0; x < width; x++)
// Check for color pixel value match to include a space or ASCII symbol
s.append(img.getRGB(x, y) == -16777216 ? " " : "#");
if (s.toString().trim().isEmpty())
continue;
System.out.println(s);
}
}
}
SCREENSHOT
OUTPUT
NOTE:
Hope this helps.