Advertisement
Abbin21

Untitled

Feb 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. import java.awt.Font;
  2. import java.awt.Graphics;
  3. import java.awt.Graphics2D;
  4. import java.util.List;
  5. import java.awt.RenderingHints;
  6. import java.awt.image.BufferedImage;
  7. import java.util.ArrayList;
  8. import java.util.Random;
  9.  
  10. public class AsciiPrcessing
  11. {  
  12.     static int width = 300;
  13.     static int height = 300;   
  14.    
  15.      private static String randomChar()
  16.      {
  17.             Random random = new Random();
  18.             int index = random.nextInt(129);
  19.            
  20.             List<Integer> specialChars = new ArrayList<Integer>();
  21.             for(int code = 0; code < 257; code++)
  22.             {
  23.                 if(!Character.isLetterOrDigit((char) code))
  24.                 {
  25.                     specialChars.add(code);
  26.                 }
  27.             }
  28.            
  29.             int charIndexNum = specialChars.get(index);
  30.             String asciiString = Character.toString((char) charIndexNum);
  31.            
  32.             return CheckForWhiteSpaces(asciiString);
  33.      }
  34.    
  35.      private static String CheckForWhiteSpaces(String asciiString)
  36.      {
  37.             if(asciiString == null)
  38.             {
  39.                 randomChar();
  40.                 return null;
  41.             }
  42.             else
  43.             {
  44.                 return asciiString;
  45.             }
  46.      }
  47.    
  48.      public static void BuildImage()
  49.         {
  50.             //Makes an empty image
  51.             BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  52.             Graphics g = image.getGraphics();
  53.             g.setFont(new Font("SansSerif", Font.BOLD, 24));
  54.  
  55.             //Converts image to a flat 2d image
  56.             Graphics2D g2 = (Graphics2D) g;
  57.             g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  58.             g2.drawString("Thanks", 30, 30);
  59.  
  60.             //Iterates height and width to write ASCII art
  61.             for(int y = 0; y < height; y++)
  62.             {
  63.                 StringBuilder builder = new StringBuilder();
  64.  
  65.                 for(int x = 0; x < width; x++)
  66.                 {
  67.                     builder.append(image.getRGB(x, y) == -16777216 ? " " : randomChar());                  
  68.                 }
  69.                 //Prints the image
  70.                 System.out.println(builder);
  71.             }
  72.         }
  73.    
  74.     public static void main(String[] args)
  75.     {  
  76.          BuildImage();
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement