Is there a way to calculate the length of a string in pixels, given a certain java.awt.Font object, that does not use any GUI components?
Is there a way to calculate the length of a string in pixels, given
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It depends on what you mean here. I’m assuming you mean you want to do it without receiving a
HeadlessException.The best way is with a
BufferedImage. AFAIK, this won’t throw aHeadlessException:Other than using something like this, I don’t think you can. You need a graphics context in order to create a
FontMetricsand give you font size information.