Advertisement
Abbin21

Untitled

Feb 19th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 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. //set the text
  59. g2.drawString("Hello", 30, 30);
  60.  
  61. //Iterates height and width to write ASCII art
  62. for(int y = 0; y < height; y++)
  63. {
  64. StringBuilder builder = new StringBuilder();
  65.  
  66. for(int x = 0; x < width; x++)
  67. {
  68. builder.append(image.getRGB(x, y) == -16777216 ? " " : randomChar());
  69. }
  70. //Prints the image
  71. System.out.println(builder);
  72. }
  73. }
  74.  
  75. public static void main(String[] args)
  76. {
  77. BuildImage();
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement