tom_corc
asked on
Graphics-how to tell width of a string
I want to tell how wide a string (say "abcde") would be in pixels using a particular font.
Is it possible to do this?
Is it possible to do this?
Obviously, if you havent set a font, then you can do:
public void paint( Graphics p )
{
FontMetrics fm = getFontMetrics( getFont() ) ;
int width = fm.stringWidth( "abcde" ) ;
}
public void paint( Graphics p )
{
FontMetrics fm = getFontMetrics( getFont() ) ;
int width = fm.stringWidth( "abcde" ) ;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
{
Font f = new Font( "Helvetica", Font.BOLD, 12 ) ;
g.setFont( f ) ;
FontMetrics fm = getFontMetrics( f ) ;
int width = fm.stringWidth( "abcde" ) ;
}
should do it...(for an applet)
Good Luck!!
Tim