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

Font.getStringBounds() returns invalid result for non-identity TRANSFORM attrib.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.3.0
    • client-libs
    • 2d
    • x86
    • windows_2000



      Name: boT120536 Date: 03/15/2001


      java version "1.3.1-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-beta-b15)
      Java HotSpot(TM) Client VM (build 1.3.1beta-b15, mixed mode)

      //
      //
      // Demonstration of Font bugs.
      //
      // This program opens a window on the screen and then draws a String
      // onto the window using a Font with a TRANSFORM attribute that
      // changes whenever the "rotate" button is pressed. The transform
      // used is a rotation is 90 degree increments and then 90 degree
      // rotations mirror image, for a total of 8 possible orientations. A
      // small red box is drawn at the anchor point and a yellow rectangle
      // is drawn according to the bounds returned by Font.getStringBounds.
      //
      // There are two problems:
      //
      // 1) The anchor point of the string varies depending on the
      // orientation.
      //
      // 2) The bounding box has bad values for all transforms other than
      // the identity transform.
      //
      //
      //
      //

      import javax.swing.*;
      import java.util.*;

      import java.io.*;
      import java.awt.*;
      import java.awt.event.*;
      import java.awt.image.*;
      import java.awt.geom.*;
      import java.awt.font.*;


      public class FontTest extends JComponent
      {
         int type = 0;
         

         FontTest()
         {
            JButton b = new JButton("Rotate");
            this.add(b);
            b.setBounds(0, 0, 200, 50);
            b.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e)
                  {
                     type++;
                     repaint();
                  }
               });
         }
         

         private void doPaint(Graphics g)
         {
            Graphics2D g2 = (Graphics2D)g;

            double matrix[] = new double[4];
            switch(type % 8) {
            case 0:
               matrix[0] = 1d;
               matrix[1] = 0d;
               matrix[2] = 0d;
               matrix[3] = 1d;
               break;
            case 1:
               matrix[0] = 0d;
               matrix[1] = -1d;
               matrix[2] = 1d;
               matrix[3] = 0d;
               break;
            case 2:
               matrix[0] = -1d;
               matrix[1] = 0d;
               matrix[2] = 0d;
               matrix[3] = -1d;
               break;
            case 3:
               matrix[0] = 0d;
               matrix[1] = 1d;
               matrix[2] = -1d;
               matrix[3] = 0d;
               break;
            case 4:
               matrix[0] = -1d;
               matrix[1] = 0d;
               matrix[2] = 0d;
               matrix[3] = 1d;
               break;
            case 5:
               matrix[0] = 0d;
               matrix[1] = 1d;
               matrix[2] = 1d;
               matrix[3] = 0d;
               break;
            case 6:
               matrix[0] = 1d;
               matrix[1] = 0d;
               matrix[2] = 0d;
               matrix[3] = -1d;
               break;
            case 7:
               matrix[0] = 0d;
               matrix[1] = -1d;
               matrix[2] = -1d;
               matrix[3] = 0d;
               break;
            }
            
            Point pos = new Point(250, 250);

            AffineTransform t = new AffineTransform(matrix);

            HashMap m = new HashMap();
               
            m.put(TextAttribute.FAMILY, "SansSerif");
            m.put(TextAttribute.SIZE, new Float(72));
            m.put(TextAttribute.TRANSFORM, new TransformAttribute(t));
            Font f = new Font(m);

            

            g2.setFont(f);
            FontRenderContext frc = g2.getFontRenderContext();
            String s = "Qater";

            Rectangle2D border = f.getStringBounds(s, frc);
            System.out.println("Border: " + border);
            
            Rectangle2D.Double br = new Rectangle2D.Double(border.getMinX() + pos.x,
                                                           border.getMinY() + pos.y,
      0d, 0d);
            br.add(border.getMaxX() + pos.x, border.getMaxY() + pos.y);
            

            g2.drawString(s, pos.x, pos.y);

            g2.setColor(Color.red);
            g2.fillRect(pos.x, pos.y, 3, 3);

            g2.setColor(Color.yellow);
            g2.draw(br);
         }

         public void paint(Graphics g)
         {
            super.paint(g);
            doPaint(g);
         }
            

         public static void main(String args[])
         {
            FontTest p1 = new FontTest();

            JFrame f1 = new JFrame("Spinner");
            f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f1.getContentPane().add(p1, BorderLayout.CENTER);
            f1.setBounds(50, 50, 600, 600);
            f1.show();
         }
      }
      (Review ID: 118900)
      ======================================================================

            Unassigned Unassigned
            bonealsunw Bret O'neal (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: