Question

In: Computer Science

Write a simple java program that produces any text art ("ASCII art") picture. Your program can...

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.

Solutions

Expert Solution

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:

  1. The program uses the BufferedImage class to manipulate image data.
  2. The Graphics class is the super class for drawing to the screen.
  3. The Graphics2D class provides control over the image co-ordinates.
  4. The StringBuilder Class is used to handle changing sequence of characters.

Hope this helps.


Related Solutions

create a program that outputs a picture that represents halloween in ascii art (jack-o-lantern, witch's hat,...
create a program that outputs a picture that represents halloween in ascii art (jack-o-lantern, witch's hat, etc) ASSEMBLY LANGUAGE PROGRAM
In C++, write a program that accepts a text file of ASCII words from standard input...
In C++, write a program that accepts a text file of ASCII words from standard input and store them and the amount of times the word appears in the file in a hash table using external chaining. Then print the words and their counts sorted based on alphabetical order and print them again in decreasing numerical order based on the amount of times the word appears in the file. Space, tab, and new line all count as space characters. The...
In JAVA Write a brief program that writes your name to a file in text format...
In JAVA Write a brief program that writes your name to a file in text format and then reads it back. Use the PrintWriter and Scanner classes.
In Java language. Write a brief program that writes your name to a file in text...
In Java language. Write a brief program that writes your name to a file in text format and then reads it back. Use the PrintWriter and Scanner classes.
Write the program in java Write a program that does basic encrypting of text. You will...
Write the program in java Write a program that does basic encrypting of text. You will ask the user the filename of a text file which contains a few sentences of text. You will read this text file one character at a time, and change each letter to another one and write out to an output file. The conversion should be done a -> b, b->c , … z->a, A->B, B->C, …. Z->A. This means just shift each letter by...
Write Java program Lab52.java which reads in a line of text from the user. The text...
Write Java program Lab52.java which reads in a line of text from the user. The text should be passed into the method: public static String[] divideText(String input) The "divideText" method returns an array of 2 Strings, one with the even number characters in the original string and one with the odd number characters from the original string. The program should then print out the returned strings.
Write a Java program that will encode plain text into cipher text and decode cipher text...
Write a Java program that will encode plain text into cipher text and decode cipher text into plain text. Create following methods and add them to your class: • a method named encode(String p, int key) that takes a plain text p and encodes it to a cipher text by adding the key value to each alphabet character of plain text p using the ASCII chart. The method should return the encoded String. For example, if p = "attack at...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces another text file in Which blank lines are removed, multiple blanks are replaced with a single blank, and no lines are longer than some given length (let say 80). Put as many words as possible on the same line (as close as possible to 80 characters). You will have to break some lines of the given file, but do not break any words or...
write a java program of mickey mouse it can be simple with just ears face eyes...
write a java program of mickey mouse it can be simple with just ears face eyes and mouth
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT