Skip to content

Instantly share code, notes, and snippets.

@avh4
Created February 27, 2009 08:04
Show Gist options
  • Save avh4/71348 to your computer and use it in GitHub Desktop.
Save avh4/71348 to your computer and use it in GitHub Desktop.
Finding the width of rendered text - Wintriss Tech
// Create the font and pass it to the Graphics context
g2.setFont(new Font("Monospaced", Font.BOLD, 24));
// Get measures needed to center the message
FontMetrics fm = g2.getFontMetrics();
// How many pixels wide is the string
int msg_width = fm.stringWidth(msg);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment