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

stringWidth on rotated Graphics returns the transformed advance

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.0
    • 1.4.0
    • client-libs
    • None
    • 2d
    • beta
    • sparc
    • solaris_7

      FontMetrics.stringWidth() should return the natural advance of the string
      along its baseline.

      The program below when run against 2D merlin builds from after Oct 27th
      prints stringWidth values which are actually the advance of the measured string
      in the transformed (rotated) coordinate system.
      ie strings rotated by 90 or 270 degrees are claimed to be of zero-width.

      The stringBounds method returns correct results.

      import java.awt.*;
      import java.awt.geom.*;

      public class TextRotatedLength extends Component {

        public static void main(String args[]) {
           Frame f = new Frame();
           TextRotatedLength td = new TextRotatedLength();
           f.add("Center", td);
           f.pack();
           f.show();
        }

          public Dimension getPreferredSize() {
              return new Dimension (100, 100);
          }

          public Dimension getMinimumSize() {
              return getPreferredSize();
          }

        public void paint(Graphics g) {
            FontMetrics fm;
            int len;
            Rectangle2D bounds;
            int ypos = 0;

            String astr = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
            Font f = new Font("serif", Font.PLAIN, 12);
            g.setFont(f);

            Graphics2D g2d = (Graphics2D)g;

            int rots = 8;
            double totalAngle = 0.0;
            double angle = (2.0*Math.PI)/rots;
            for (int i=0; i<9; i++) {
                fm = g.getFontMetrics();
                len = fm.stringWidth(astr);
                bounds = fm.getStringBounds(astr, g);
                System.out.println("Angle of Rotation="+totalAngle);
                System.out.println("stringWidth()="+len);
                System.out.println("getStringBounds() from="+bounds);
                g2d.rotate(angle);
                totalAngle+=angle;
            }
       }
      }

            dougfelt Doug Felt (Inactive)
            prr Philip Race
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: