Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Length of string in pixels depending on font

Status
Not open for further replies.

jem122974

Programmer
Nov 1, 2001
114
US
Is there a way to get the length of a string in pixels depending on the font?

I want to center or right justify some strings on the PDF I'm drawing so I need to determine how much to adjust the beginning coordinate.

Thanks,
Jon Mitchell
 
Look at java.awt.FontMetrics which you obtain from java.awt.Graphics.getFontMetrics()

-pete
 
Okay, I looked at this, but these are all abstract. Anymore ideas?
 
ummm don't know what you mean. For example:
Code:
public void draw(Graphics g){

   // FontMetrics research
   FontMetrics fm = g.getFontMetrics();
   int pixelwidth = fm.stringWidth("Hello World");

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top