   [java] Finding the width in pixels of text drawn with Sys fonts
  Hi guys,It sounds like a really simple thing, but its been stumping me for a few hours.So I have some various strings (menu items) than i want to draw centered in my draw space. To do this I need 2 things. The width of my draw space. Thats easy.The width in pixels of my strings. Something that seems quiet hard to do in Java.I though I had cracked the problem when I found Font font = Font.getFont("Helvetica-bold-italic");FontRenderContext frc = g2D.getFontRenderContext();TextLayout layout = new TextLayout("This is a string", font, frc);But TextLayout doesnt seem to have a function to return the width of the string. Anybody know of a way to do this kind of thing? I dont believe that Java would not have this kind of functionality.   
Quote:Original post by SetantaaTextLayout doesnt seem to have a function to return the width of the string. Anybody know of a way to do this kind of thing? I dont believe that Java would not have this kind of functionality.You can get the width of a string using any available graphics context and the method getFontMetrics(), like so:Graphics g; // the graphics context in your paint() method, perhaps, or from an Image object// get width of stringint string_width = g.getFontMetrics().stringWidth("How wide is this string in pixels?");GetFontMetrics() uses the current system font to determine the width of the specified string. You may alternatively pass a Font object to the getFontMetrics() call to specify a particular font:Font font = new Font("Arial", Font.PLAIN, 18);int string_width = g.getFontMetrics(font).stringWidth("Width of string in Arial, font size 18");;  Thanks a lot Omega147, I will implement that now and see how it works. I've still so much to learn in Java :oThanks again,-Set   ;Quote:Original post by Omega147GetFontMetrics() uses the current system font to determine the width of the specified string. You may alternatively pass a Font object to the getFontMetrics() call to specify a particular font:Just a note of clarification, g.getFontMetrics() uses the current font that is set in graphics contetxt, which need not be a "system" font. In other words, it is the same as g.getFontMetrics(g.getFont()). This may have been what you meant Omega147, but I thought I'd clarify it.Also, from the java docs, stringWidth() doesn't take into account the rendering context. In other words, you won't get a "perfect" value if things such as anti-aliasing are enabled. If you'd like to get a more correct number, use the getStringBounds method in FontMetrics like so:    double stringWidth = g.getFontMetrics().getStringBounds(someString, g).getWidth();Note that it returns a Rectangle2D, so you could also get the height too.   ;Quote:Original post by aryxJust a note of clarification, g.getFontMetrics() uses the current font that is set in graphics contetxt, which need not be a "system" font. In other words, it is the same as g.getFontMetrics(g.getFont()). This may have been what you meant Omega147, but I thought I'd clarify it.Also, from the java docs, stringWidth() doesn't take into account the rendering context. In other words, you won't get a "perfect" value if things such as anti-aliasing are enabled.Excellent clarification, aryx. And, the getStringBounds() usage is new to me; learned something new. Thanks! :)   