title: How to fix text quality in Java graphics?
body: <p>Code:</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Font;
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  import java.awt.RenderingHints;
  
  import javax.swing.JFrame;
  
  public class TextRectangle extends JFrame {
  
      private static final long serialVersionUID = 1L;
  
      public static void main(String[] a) {
          TextRectangle f = new TextRectangle();
          f.setSize(300, 300);
          f.setVisible(true);
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
  
      public void paint(Graphics g) {
          Graphics2D g2d = (Graphics2D) g;
          g2d.setRenderingHint(
              RenderingHints.KEY_TEXT_ANTIALIASING,
              RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
          g2d.setRenderingHint(
              RenderingHints.KEY_RENDERING,
              RenderingHints.VALUE_RENDER_QUALITY);
  
          g2d.setColor(Color.WHITE);
          g2d.fillRect(0, 0, getWidth(), getHeight());
  
          g2d.setColor(Color.BLACK);
          g2d.setFont(new Font("Calibri", Font.BOLD, 18));
          g2d.drawString("Why does this text look so ugly?", 30, 150);
      }
  
  }
  </code></pre>
  
  <p>Result:</p>
  
  <p><img src="https://i.stack.imgur.com/rnZSQ.png" alt="result screenshot"></p>
  
  <p>As you can see the letter spacing is uneven. Sometimes it's 1px, sometimes it's 2 or even 3 pixels. Another issue is the dot above 'i' is wider than the bottom part.</p>
  
  <p><img src="https://i.stack.imgur.com/Pt8fl.png" alt="problem details"></p>
  
  <p>The same text written in a text editor looks much better. Do you know how to fix this problem?</p>
  
  <p>My environment is Windows 8.1, Java 1.8.</p>