title: How to get the correct String width from FontMetrics in JAVA
body: <p>I calculate the width of a string by using the <code>stringWidth("str")</code> method on the <code>FontMetrics</code> object. This method only gives me the width from the starting point to the ending point on the <strong>baseline</strong>. Not the overall width of the string.</p>
  
  <p>Any ideas on how to calculate the overall width?</p>
  
  <p>Most documentation says that I can't rely on the result by adding the width of each char in the string.
  Here is my code until now:</p>
  
  <pre><code>BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
  Graphics2D g = image.createGraphics();
  FontMetrics fm = g.getFontMetrics(new Font("Arial", Font.PLAIN, 6));
  int width = fm.stringWidth("Product name");
  </code></pre>