// Find the size of string s in font f in the current Graphics context g. FontMetrics fm = g.getFontMetrics(f); java.awt.geom.Rectangle2D rect = fm.getStringBounds(s, g); int textHeight = (int)(rect.getHeight()); int textWidth = (int)(rect.getWidth()); int panelHeight= this.getHeight(); int panelWidth = this.getWidth(); // Center text horizontally and vertically int x = (panelWidth - textWidth) / 2; int y = (panelHeight - textHeight) / 2 + fm.getAscent(); g.drawString(s, x, y); // Draw the string.