What is the best and easiest way to get the width of a String using Slick2D?

I'm working on the achievement interface menu in my game that displays some images. On each image I need to draw a string such as "Reach Level 5.". Only problem is that I don't know how to get the string width to properly center the string on the image.


1 Answer 1


The Font class is the base class for all the fonts used in Slick2D. It has a method called getWidth(string) (documentation). This function returns an integer the represents the width of the string as it would be drawn. This goes for the main two classes that extend Font

If using TrueTypeFont you can use getWidth(string), documentation.

If using UnicodeFont you can use getWidth(string), documentation.

  • \$\begingroup\$ Perfect! Thank you very much. Quick and detailed answer. \$\endgroup\$
    – mbreen
    Commented May 1, 2012 at 21:15
  • \$\begingroup\$ No problem, remember to up vote if it was useful and accept if it worked for you. And welcome to GDSE! \$\endgroup\$
    – House
    Commented May 1, 2012 at 21:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .