Tuesday, May 01, 2007

Round and Round we go; Netbeans WTF

I found the following code in a recent snapshot of netbeans. If anyone has a clue as to what the developer was thinking, let me know. The code is in org.openide.awt.HtmlLabelUI:


private static int textWidth(String text, Graphics g, Font f, boolean html) {
if (text != null) {
if (html) {
return Math.round(
Math.round(
Math.ceil(
HtmlRenderer.renderHTML(
text, g, 0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE, f, Color.BLACK,
HtmlRenderer.STYLE_CLIP, false
)
)
)
);
} else {
return Math.round(
Math.round(
Math.ceil(
HtmlRenderer.renderPlainString(
text, g, 0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE, f, Color.BLACK,
HtmlRenderer.STYLE_CLIP, false
)
)
)
);
}
} else {
return 0;
}
}

7 comments:

  1. Franky speaking I do not understand it from the very beginning: I have found the method in google:

    public static double renderPlainString

    Render a string to a graphics instance, using the same API as renderHTML... Returns the width in pixels successfully painted.

    Why the successful pixels amount is a double?

    ReplyDelete
    Replies
    1. For urgent Hot Shot Trucking Houston, Fast Trac Transportation provides rapid, industrial-grade services tailored to the oil and gas sector. Our dedicated hot shot fleet handles time-sensitive loads with precision, from equipment to supplies, 24/7. With a commitment to safety and customer service, we navigate complex routes efficiently, ensuring your critical deliveries arrive intact and on schedule across Texas and beyond.

      Delete
  2. Maybe the programmer is just a huge fan of Math.round() ^^

    I can imagine how this one developed, he just used
    Math.ceil() first, but then:

    possible loss of precision
    found : double
    required: int


    ok, so he was looking for a method to convert it to int, he had the javadoc for the class Math still open (he just looked up Math.ceil()) and found the method
    static long round(double a)
    "That's gonna work - long is also an integer right?"
    Math.round(Math.ceil())
    of course...

    possible loss of precision
    found : long
    required: int


    it didn't. But there is another round() method:
    static int round(float a)
    And being the clever programmer he obviously is ^^, he knew Java would convert his long to float. So, using round a second time, the result would now be int!
    "Hooray!"

    ReplyDelete
  3. This is precisely what I need to familiarize myself with this area. Looking forward to many interesting and useful articles from you. the article is very interesting and effective.

    DedicatedHosting4u.com

    ReplyDelete
  4. Nice post. It is really interesting. Thanks for sharing the post!

    craigslist For PC

    ReplyDelete
  5. It is a wonder article. Astroswmig is India's famous astrology prediction wbsite.here you know your horoscope and kundali as per your date of birth and ask questions to the top astrologers of India regarding your problems.

    Gemini Horoscope 2021
    Taurus Horoscope 2021
    Aries Horoscope 2021
    Talk To Astrologer
    Aaj ka Rashifal
    Online Astrologer

    ReplyDelete
  6. The best iOS development course delivers industry-relevant skills.It focuses on modern mobile standards.This best iOS development course supports professional growth.It is trusted.

    ReplyDelete