How can I get Pixel Info (width, height, pixel colors) for a text string?

Scott Stanchfield

The width and height are actually very easy. All you need to do is get a FontMetrics object for the font you used to draw the string, and ask it:

FontMetrics fm = getFontMetrics(getFont());
System.out.println(fm.stringWidth("This is a sample string"));

Note that the height is the actual space the font requires, while leading is the space the font requests be used between lines of text on the screen.

To obtain the actual pixel colors, take a look at the PixelGrabber class in java.awt.image.