-2

i have to make a program that has the user input 3 letters and then the program prints it out in asterisks. EX for 1 letter : https://i.stack.imgur.com/jkAIX.jpg Obvously I can make an array with the shapes for each char in the alphabet but I want to know if there is an easier way to do something link this. Is there some method that get a char and a symbol and prints out the shape of the char in that symbol.

Anatoliy Sokolov
  • 347
  • 1
  • 4
  • 10
  • Well ... yes and no. You can include FreeType (or, on Windows, draw the character into a bitmap) but that really sounds beyond what you are asked to do. Rather than creating the bitmap array yourself, you could search the web for a pre-made list. And of course there is no method to do *exactly* what your assignment is to write! – Jongware Nov 07 '15 at 17:04

2 Answers2

10

Here's a neat solution using good old AWT to render your text in Comic Sans, for kicks and giggles:

import static java.awt.image.BufferedImage.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;

public class ASCIIRenderer {
    public static void main(String[] args) throws IOException {
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(System.in))) {
            String text = null;
            while ((text = reader.readLine()) != null) {
                // Dummy image to calculate bitmap width / height of your text
                BufferedImage img = new BufferedImage(1, 1, TYPE_INT_ARGB);
                Graphics2D g2d = img.createGraphics();
                Font font = new Font("Comic Sans MS", Font.PLAIN, 24);
                g2d.setFont(font);
                FontMetrics fm = g2d.getFontMetrics();
                int width = fm.stringWidth(text);
                int height = fm.getHeight();
                g2d.dispose();
                // Real image
                img = new BufferedImage(width, height, TYPE_INT_ARGB);
                g2d = img.createGraphics();
                g2d.setFont(font);
                fm = g2d.getFontMetrics();
                g2d.drawString(text, 0, fm.getAscent());
                g2d.dispose();
                for (int y = 0; y < img.getHeight(); y++) {
                    for (int x = 0; x < img.getWidth(); x++) {
                        System.out.print(0 == img.getRGB(x, y) ? "  " : "**");
                    }
                    System.out.println();
                }
            }
        }
    }
}

(credits to most of the above code go to MadProgrammer)

Run it, type something like ABC and get this:

                  **                   ************                            ************
                ******                 ****************                    ****************
                ******                 ****        ******                ******        ****
              ********                 ****          ******            ******          ****
              ********                 ****            ****            ****                
            ******  ****               ****            ****          ******                
          ********  ****               ****            ****        ******                  
          ******    ****               ****          ******        ****                    
        ********      **               ****        ******          ****                    
        ******        ****             ****************          ****                      
        ******************             ****************          ****                      
      ********************             ****        ********      ****                      
    **********          ****           ****            ******    ****                      
    ******              ****           ****              ****    ****                ****  
    ******              ****           ****              ****      ****            ******  
  ******                ******         ****            ****        ******      ********    
  ******                  ****         ****      **********          ****************      
  ****                    ****         ****************                **********          
                                       ************                                        
Community
  • 1
  • 1
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
4

There is no easy way to do this. A char does not have a shape. A char is just a number between 0 and 65535 that happens to get displayed as a single character when you print it.

You would have to design your own pattern for each letter of the alphabet, and using arrays would be the easiest way.

Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
  • Yeah i figured I would have to do that, I know I can do that in that way its just tedious. – Anatoliy Sokolov Nov 07 '15 at 17:10
  • 1
    @AnatoliySokolov It is tedious I agree. I'd be tempted to do it for 5 letters of the alphabet to prove you can do it and then tell your professor you got bored. – Paul Boddington Nov 07 '15 at 17:11
  • 3
    @PaulBoddington Total overkill. User only inputs **3** letters, so why do 5? Are you trying for extra points, and make the rest of us look bad? OP already designed the `D`, so design an `A`, and show that it can print `DAD`, or maybe `ADD` which is why you stopped there. LOL *(oops, can't print that yet)* – Andreas Nov 07 '15 at 17:16