How can I get Pixel Info (width, height, pixel colors) for a text string?
Created Dec 28, 2001
Scott Stanchfield
To obtain the actual pixel colors, take a look at the PixelGrabber class in java.awt.image.
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.getHeight());
System.out.println(fm.getLeading());
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.