Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Explain this piece of java code?

  • 08-08-2012 04:32PM
    #1
    Registered Users, Registered Users 2 Posts: 14


    public static void main(String[] args) {
            
         String s = "D";
            if (s != null) {
                BufferedImage bi = new BufferedImage(80,20,BufferedImage.TYPE_INT_BGR);
                Graphics2D g2 = (Graphics2D)bi.getGraphics();
               g2.setFont(new Font("Serif", Font.BOLD, 12));
                g2.drawString(s, 0, 16);
                Raster ras = bi.getData();
               
                for (int row = 0; row < 20; row++) {
                    String line = "";
                    for (int col = 0; col < 80; col++) {
                        if (ras.getSample(col,row,1) > 128) {
                            line += "*";
                        } else {
                            line += " ";
                        }
                    }
                    System.out.println(line);
                 
            
            
            
        }
        }
        }
    

    I know it prints out the character in ascii, what I want to know (being a noob) is how do I tell what each field does and how does the program function in general? I have a general gist but I'm not quite there.

    Say the 80 and the 20? Where do I look, what do I do to see that? What happens if I change it etc. I can see that it is width, height, type etc, but I dont know how much of an affect changing them has, and what values I should set. Is it measured in pixels? i can see it is coupled with the loop further down. I assume 12 is the size of the serif font? Then how does the 12 relate to the 80 and 20?

    Similarly for g2.drawString(s, 0, 16);

    Clearly s is d. What is 0 and what is 16? Just the location that I want to draw s? I just have trouble finding/understanding this information from documentation.


Comments

  • Registered Users, Registered Users 2, Paid Member Posts: 8,548 ✭✭✭RedXIV


    Can you execute the code?

    If you can execute it and change the values a few times, I'm pretty sure you'd know what each of those numbers were for :)


  • Registered Users, Registered Users 2 Posts: 7,157 ✭✭✭srsly78


    The font is size 12, so the program only scans 20 rows - covers the char and some padding at top and bottom. Similarly for cols. It's set to 80 because the program was originally used with a longer string than just "D". These are just hardcoded values, a better way to do it would be to estimate the width from strlen somehow.

    Yes the numbers refer to pixels.

    The 0 and 16 refers to position for drawing (without this there would be no padding on the left). A simple google returns loads of api documentation.

    The code is pretty poor and confusing tbh, lots of hackery and little consistency (see treatment of vertical vs horizontal padding).


  • Registered Users, Registered Users 2, Paid Member Posts: 2,032 ✭✭✭lynchie


    Code is quite simple. Draws the letter D on an image, gets the raw bitmap matrix and then scans through it. If the pixels have been affected by the drawing of the D it prints an * out for that line. The end result - ascii art!

    Btw google Graphics2D and you will find the javadocs.


  • Registered Users, Registered Users 2 Posts: 7,182 ✭✭✭Genghiz Cohen


    As above it looks like a basic ASCII art generator.
    It draws a D to an image then scans the image pixel by pixel and checks to see if the RGB values pass a threshold (128) if they do write a '*' to file, else write a ' '

    If you wanted to make it more advanced, you'd use a switch and a few other characters for the different amount of colour in each pixel. Then have a few different thresholds.


  • Registered Users, Registered Users 2 Posts: 14 AdultCaution


    Thanks guys that is great, very helpful. I cant execute the code at the moment so this helped a lot. I was just wondering the best way to do this, say take any user input and make it into ascii. But srsly78 says this code is bad, it certainly was confusing. I am quite crap at reading the docs as of yet.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 7,157 ✭✭✭srsly78


    You should really try to execute it, setting up java is trivial (and free). Staring at code is not a good way to learn, you have to tinker with it and run it.


Advertisement