DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Center A Text In Java Swing
Draws a string centered by calculating its position depending on the size.
String s; int width, height; Graphics g; FontMetrics fm = getFontMetrics(ftDefault); Rectangle2D textsize = fm.getStringBounds(s, g); int xPos = (width - textsize.getWidth()) / 2; int yPos = (height - textsize.getHeight()) / 2 + fm.getAscent(); g.drawString(s, xPos, yPos);




