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

Rotated text is at wrong angles for drawString()

XMLWordPrintable

    • 2d
    • merlin
    • generic, x86, sparc
    • generic, linux, windows_nt



      Name: gsC80088 Date: 11/23/98


      // Test for rotated text using SWING and jdk1.2FCS.
      //
      // We draw a baseline and a String of text at the
      // following angles (in degrees):
      // 0 30 60 90 12 150 180 210 240 270 300 330 360
      //
      // JDK PROBLEMS:
      //
      // On Windows NT 4.0, this all works well with JDK1.2FCS.
      // (There are problems with JDK-1.2beta4, but not discussed here).
      //
      // On Solaris 2.5.1 on an Ultrasparc 2, with JDK1.2FCS,
      // using Classic VM (build JDK-1.2fcs-O, green threads, sunwjit):
      //
      // 1. Rotated text is extremely slow: about one line every 4.0 seconds
      // 2. For some angles, such as 120, the text is drawn
      // at a slightly different angle than the baseline. This makes
      // Java 2D look pretty silly in labeling lines in complex diagrams.




      import java.awt.BorderLayout;
      import java.awt.Canvas;
      import java.awt.Color;
      import java.awt.Dimension;
      import java.awt.Font;
      import java.awt.Graphics;
      import java.awt.Graphics2D;
      import java.awt.Rectangle;
      import java.awt.geom.AffineTransform;

      import javax.swing.JFrame;



      class Testrotate_swing extends JFrame
      {
      // create first object
      public static void main(String args[])
      {
      Testrotate_swing nvapp = new Testrotate_swing();
      }


      // constructor
      public Testrotate_swing()
      {
      // create the Frame with the specified title.
      super("test frame");

      getContentPane().setLayout( new BorderLayout());

      // Add the applet to the window. Set the window size. Pop it up.
      Testcanvas tcanvas = new Testcanvas();
      getContentPane().add( tcanvas, BorderLayout.CENTER);
      validate();
      this.setSize( new Dimension( 600, 600));
      this.show();
      }
      } // end class Testrotate_swing




      class Testcanvas extends Canvas
      {


      public void paint( Graphics grapha)
      {
      int idegrees;
      double theta;

      Graphics2D g2;
      g2 = (Graphics2D) grapha;
      System.out.println("g2: " + g2);

      // clear to white
      Rectangle brect = this.getBounds();
      g2.setColor( Color.white);
      g2.fill( brect);

      AffineTransform transa = new AffineTransform();

      // draw rotated strings
      g2.setFont( new Font("SansSerif", Font.PLAIN, 10));
      for (idegrees = 0; idegrees <= 360; idegrees += 30) {
      System.out.println("\nidegrees: " + idegrees);
      theta = (2 * Math.PI * idegrees) / 360;

      // reset transform
      g2.setTransform( new AffineTransform());
      transa.setToTranslation( 300.0, 300.0);
      System.out.println("transa Translation: " + transa);
      g2.transform( transa);
      transa.setToRotation( theta, 0.0, 0.0);
      System.out.println("transa Rotation: " + transa);
      g2.transform( transa);
      System.out.println("g2.getTransform: " + g2.getTransform());
      g2.setColor( Color.black);
      g2.drawString("abcdefghijklmnopqrstuvwxyz idegrees: " + idegrees,
      0, 0);


      g2.setColor( Color.red);
      g2.drawLine( 0, 0, 200, 0);
      }
      }

      } // end class Testlet
      (Review ID: 43221)
      ======================================================================

      Name: krT82822 Date: 05/06/99


      There are two problems with text rotation.

      (1) Compile and run the following sample app.
      You will see that, for angles other than multiples of 90,
      the text baseline angle is incorrect.

      (2) change the app so that ANTIALIASING is "OFF"
      and repeat the test. The angle of *individual* letters
      is incorrect
         
      -------------------------------------
      D:\Russell\projects\BetaJdk\AntiAlias>java -version
      java version "1.2.1"
      HotSpot VM (1.0rc2, mixed mode, build E)

      D:\Russell\projects\BetaJdk\AntiAlias>java -fullversion
      java full version "JDK-1.2.1-K"
      -------------------------------------
      public class Rotate {
        
        public static class MyCanvas extends javax.swing.JComponent
        {
          public void paintComponent(java.awt.Graphics p1)
          {
            java.awt.Graphics2D g2 = (java.awt.Graphics2D)p1;
            //
            g2.setRenderingHint(java.awt.RenderingHints.KEY_ANTIALIASING, java.awt.RenderingHints.VALUE_ANTIALIAS_ON ); //_OFF
            g2.setFont( new java.awt.Font( "Helvetica", java.awt.Font.BOLD, 12 ) );
            //
            for( double angle = 0; angle< 360; angle+=15 )
            {
              String text = "Test_rotate_0123456789_Test_rotate_end";
              java.awt.geom.AffineTransform af = new java.awt.geom.AffineTransform();
              af.translate( 400, 400 );
              af.rotate( Math.toRadians( angle ) );
              g2.setTransform( af );
              //
              g2.setColor( java.awt.Color.black );
              g2.drawString( text, 0, 0 );
              g2.setColor( java.awt.Color.red );
              g2.drawLine(0, 0, 400, 0 );
            }
            //
          }

        }
        public static void main(String s[]) {
            javax.swing.JFrame f = new javax.swing.JFrame();
            f.addWindowListener(new java.awt.event.WindowAdapter(){
                public void windowClosing(java.awt.event.WindowEvent e) {System.exit(0);}
            });
            MyCanvas dp = new MyCanvas();
            f.getContentPane().add("Center", dp);
            f.setSize( 900, 900 );
            f.show();
        }
      }
      (Review ID: 57921)
      ======================================================================

            pkejriwasunw Parry Kejriwal (Inactive)
            gstone Greg Stone
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: