0

Can someone explain to me how to use FontMetrics in a JPanel? I need to figure out the width and height in pixels of a String when using drawString() to paint it on a JPanel

Thanks!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Cin316
  • 651
  • 1
  • 9
  • 19

1 Answers1

2

you can define a Graphics object as your method's input variable. or, you can use getFontMetrics() method of your JFrame or JPanel.

for example to get a character's width:

JFrame frame = new JFrame();
int charWidth = frame.getFontMetrics( frame.getFont() ).charWidth() ;