How to calculate width of a String in pixels in Java? For e.g., I have a string say \"Hello World!\". What is its length in pixels, also considering its fon
String
You can try something like this
Graphics2D g2d = ... Font font = ... Rectangle2D r = font.getStringBounds("hello world!", g2d.getFontRenderContext()); System.out.println("(" + r.getWidth() + ", " + r.getHeight() + ")");
Refer this doc, may help you.