What is the difference between FontMetrics.stringWidth () and FontMetrics.getStringBounds ()?

I need to find the width of the text drawn on the screen. This thread assumes that FontMetrics.stringWidth () will sometimes not be as accurate as FontMetrics.getStringBounds ().

Does anyone know if this is correct, and if so, in what context is this difference visible? Most likely, FontMetrics.stringWidth () is used, including in SwingUtilities.computeStringWidth (). In addition, the obvious advantage is that it does not require a graphic object, but this in itself, combined with the fact that anti-aliasing and similar settings are defined in the Graphics object, can explain why FontMetrics.getStringBounds () can be more accurate.

+5
source share
2 answers

getStringBoundshas more information that you can work with as it receives graphics. He can use it to calculate the location of a subpixel glyph and to take into account the smoothing effects on glyph sizes. From the documentation for the FontRenderContext :

The anti-aliasing and fractal metrics specified by the application can also affect the size of the character due to rounding to pixel borders.

+5
source

I think the answer to your question. From javadocs:

stringWidth

     

, , , . . getStringBounds (String, Graphics). .

+4

All Articles