Basic HTML version of Foils prepared 13 July 98

Foil 14 Centering a String Horizontally

From Java Academy II:Intermediate Java NPAC/ECS Java Academy -- February to April 98. by Tom Scavo

The FontMetricsTest Applet
Here's how to center a string s horizontally in the applet window: int appletWidth = getSize().width; FontMetrics fm = g.getFontMetrics(); int stringWidth = fm.stringWidth( s ); int x = (appletWidth - stringWidth)/2; g.drawString( s, x, y ); where y is arbitrary (to center the string vertically, we have to work a bit harder... )



© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Sat Nov 28 1998