title: Java - Draw text in the center of an image
body: <p>I need to write text in the center of an image. The text to write is not always the same.</p>
  
  <p>The code I'm using is here:</p>
  
  <pre><code>// Here I first draw the image
  g.drawImage(img, 22, 15, 280, 225, null);
  // I get the text 
  String text = photoText.getText();
  // Set the text color to black
  g.setColor(Color.black);
  // I draw the string 
  g.drawString(text, 79.5F, 220.0F);
  </code></pre>
  
  <p>The problem is that the text isn't at the center of the image, what can I do? </p>
  
  <p>I only need to draw the text at the horizontal center. </p>