[SOLVED] String in the Middle off player Depending on String Length!

i want to make so player has a name that you type in to a JOptionPane

g.setFont(new Font("serif" , 32, 32));
g.drawString(name, (int)xpos - name.length()*name.length() , (int)ypos - 35);
i get the name to draw but i want it to be in the middle off the text eee... like this

[ ] = player

             Hello All
                [ ]

but it always does something like this
                    Hello All
                [ ]
    Hello All
                [ ]

Try having a look at font.getStringBounds() and use half of the width to offset your position.

Thank you!

Solved Like This!

			Font font = new Font("Serif", 32, 32);
			g.setFont(font);
			AffineTransform affinetransform = new AffineTransform();     
			FontRenderContext frc = new FontRenderContext(affinetransform,true,true);  
			int textwidth = (int)(font.getStringBounds(name, frc).getWidth());
			int textheight = (int)(font.getStringBounds(name, frc).getHeight());
			
			g.drawString(name, (int)xpos + width / 2 - textwidth / 2 , (int)ypos - textheight);