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.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.

To obtain the actual pixel colors, take a look at the PixelGrabber class in java.awt.image.
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact