public static void drawStringCentered ( Graphics g , String str , int x , int y , int width , int height ) { FontMetrics fm = g . getFontMetrics ( g . getFont ( ) ) ; int xpos = x + ( ( width - fm . stringWidth ( str ) ) / 2 ) ; int ypos = y + ( ( height + fm . getAscent ( ) ) / 2 ) ; g . drawString ( str , xpos , ypos ) ; }
Draw a string centered within a rectangle . The string is drawn using the graphics context s current font and color .