Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4248641

FontMetrics.stringWidth() incorrect for antialiased bold text

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 1.3.0
    • client-libs
    • None
    • 2d
    • sparc
    • solaris_2.6

      The string width isn't computed correctly for antialiased bold text. Appears on Solaris & win32 : jdk1.2, jdk1.2.2, jdk1.3.

      import java.awt.*;

      public class StringWidth extends Panel {

          public StringWidth() {
              setBackground(Color.white);
          }
          
          public void paint(Graphics g) {
              Dimension d = getSize();
              Graphics2D g2 = (Graphics2D) g;
              g2.clearRect(0, 0, d.width, d.height);
              g2.setColor(Color.black);

              Font font = new Font("serif", Font.BOLD, 24);
              g2.setFont(font);

              String text = "Text should be right justified.";
              int xx = d.width-getFontMetrics(font).stringWidth(text);
              g2.drawString(text, xx, (int) (d.height*0.3));

              g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
              g2.drawString(text, xx, (int) (d.height*0.6));
          }
              
          public static void main(String args[]) {
      Frame f = new Frame("FontMetrics.stringWidth() w/ BOLD Font ");
      f.add("Center", new StringWidth());
      f.pack();
              f.setSize(500, 300);
      f.show();
          }
      }

            Unassigned Unassigned
            blichtensunw Brian Lichtenwalter (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: