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

FontMetrics.stringWidth & Graphics.drawString do not agree on length

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.2.0
    • 1.2.0
    • client-libs
    • 2d
    • 1.2beta4
    • x86
    • solaris_2.6, windows_95, windows_nt
    • Not verified



      Name: rm29839 Date: 01/06/98


      please compile and run

      import java.awt.*;
      import java.awt.event.*;
      import java.awt.swing.*;

      public class TestFont extends JFrame {

          public static void main(String[] args ) {
              TestFont tsb = new TestFont();
      tsb.show();
          }

          public TestFont() {
      setBounds(20,20,400,600);
      setBackground(Color.white);
      setFont( new Font("SansSerif",Font.BOLD,20));
      JPanel p = new FontPanel();

      getContentPane().add("Center",p);
          }
      }
      class FontPanel extends JPanel {
      public void paint(Graphics g) {
      int leftMargin = 20;
      int y = 120;
      //g.setFont( new Font("SansSerif",Font.BOLD,20)); tried this with same effect.
      g.drawString("AB CDI Ii iJWjk", leftMargin, y);

      int W = getFontMetrics(g.getFont()).stringWidth("AB CDI Ii iJWjk");
      g.drawLine(leftMargin,y,leftMargin + W,y);

      }
      }
      You will notice that the underline is much longer than the text string.
      If you replace the JPanel by Panel and JFrame by Frame and replace the getContentPane() by a simple add()
      you will get an AWT version which underlines to the same length as the drawn text.

      Have a Very Good Xmas good wishes for the New Year.

      Regards Peter Wilson
      (Review ID: 22445)
      ======================================================================

            jkaulorcl Jeet Kaul (Inactive)
            rmandelsunw Ronan Mandel (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: