Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.util.List;
- import java.awt.RenderingHints;
- import java.awt.image.BufferedImage;
- import java.util.ArrayList;
- import java.util.Random;
- public class AsciiPrcessing
- {
- static int width = 300;
- static int height = 300;
- private static String randomChar()
- {
- Random random = new Random();
- int index = random.nextInt(129);
- List<Integer> specialChars = new ArrayList<Integer>();
- for(int code = 0; code < 257; code++)
- {
- if(!Character.isLetterOrDigit((char) code))
- {
- specialChars.add(code);
- }
- }
- int charIndexNum = specialChars.get(index);
- String asciiString = Character.toString((char) charIndexNum);
- return CheckForWhiteSpaces(asciiString);
- }
- private static String CheckForWhiteSpaces(String asciiString)
- {
- if(asciiString == null)
- {
- randomChar();
- return null;
- }
- else
- {
- return asciiString;
- }
- }
- public static void BuildImage()
- {
- //Makes an empty image
- BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- Graphics g = image.getGraphics();
- g.setFont(new Font("SansSerif", Font.BOLD, 24));
- //Converts image to a flat 2d image
- Graphics2D g2 = (Graphics2D) g;
- g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
- //set the text
- g2.drawString("Hello", 30, 30);
- //Iterates height and width to write ASCII art
- for(int y = 0; y < height; y++)
- {
- StringBuilder builder = new StringBuilder();
- for(int x = 0; x < width; x++)
- {
- builder.append(image.getRGB(x, y) == -16777216 ? " " : randomChar());
- }
- //Prints the image
- System.out.println(builder);
- }
- }
- public static void main(String[] args)
- {
- BuildImage();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement