Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Explain this piece of java code?

Options
  • 08-08-2012 4:32pm
    #1
    Registered Users 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 Posts: 8,432 ✭✭✭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 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 Posts: 1,994 ✭✭✭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 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 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 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