All,

I'm getting some strange behavior with TextLayout.getBounds (), and I'm not
certain if it's my usage or not.  I have some peculiar scaling requirements
to satisfy, and may not be doing so correctly.

Here's a brief description of what I'm trying to do with the different
scalings:

System scaling: a drawing must appear the same in the editor regardless of
the size of the coordinate space.  System scaling represents a scaling
factor that enlarges or reduces small or large coordinate spaces to what
the editor expects.

User scaling: a user selectable scaling factor.  A user scaling of 200%,
for example, will cause a drawing to appear twice as large in the editor.
User scaling and system scaling combine to get a final scaling factor for
the graphics context.

Font scaling: normally fonts scale up by the user scaling factor.  But the
user may choose to have fonts scale less than or greater than the user
scaling factor.  The font scaling factor is the percentage of the user
scaling factor that fonts should appear.

Included below is sample code that demonstrates the problem, anytime the
ratio between system scaling and font scaling is not 1.0.  This means that
there will be a non-identity transform applied to the font used to build
the text layout, at which point the TextLayout.getBounds () method seems to
return incorrect values.  The larger the ration between system scaling and
font scaling, the greater the discrepancy.

If anyone has done this sort of thing already, and has tips on a better way
of doing it, I'd appreciate any advice.

Thanks,
Jay Shaffstall


package org.cas.casStructureGUI;

import javax.swing.*;
import java.awt.*;
import java.awt.Stroke;
import java.awt.Graphics2D;
import java.awt.geom.*;
import java.awt.font.*;
import java.util.*;

public class TestTextLayout extends JPanel
{
  private double mySystemScaling = 5.0;
  private double myUserScaling = 4.0;
  private double myFontScaling = 1.0;
  private Font myFont = new Font ("Times New Roman", Font.PLAIN, 16);
  private String myString = "H";
  private Point2D myLocation = new Point2D.Double (20, 20);

  public TestTextLayout()
  {
    setPreferredSize(new Dimension (500, 500));
    setBackground(Color.white);
  }

        public static void main(String[] argv)
        {
    JFrame theFrame = new JFrame ("Testing text layout");
                TestTextLayout app = new TestTextLayout ();

    theFrame.getContentPane ().add (app);
    theFrame.pack ();
    theFrame.setVisible (true);
        }

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

    Graphics2D g2d = (Graphics2D) g;

    g2d.scale (mySystemScaling * myUserScaling, mySystemScaling *
myUserScaling);

    // Keeps text displaying correctly at different scalings
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

    drawTextLayout (g2d);

    Stroke boxLine = new BasicStroke ((float) (0.5 / mySystemScaling));
    Rectangle2D bounds = getTextLayoutBounds (g2d);

    g2d.setStroke (boxLine);
    g2d.draw (bounds);
  }

  Rectangle2D getTextLayoutBounds (Graphics2D g2d)
  {
    TextLayout theLayout = getTextLayout (g2d);

    Rectangle2D bounds = theLayout.getBounds ();

    // Adjust for drawing location
    bounds.setRect (bounds.getX () + myLocation.getX (),
                        bounds.getY () + myLocation.getY (),
                        bounds.getWidth (), bounds.getHeight ());

    return bounds;
  }

  void drawTextLayout (Graphics2D g2d)
  {
    TextLayout theLayout = getTextLayout (g2d);

    g2d.setColor (Color.black);
    theLayout.draw (g2d, (float) myLocation.getX (), (float)
myLocation.getY());
  }

  TextLayout getTextLayout (Graphics2D g2d)
  {
    // Use a font transform so the font appears at the correct
    // size after system scaling.
    AffineTransform fontTransform = AffineTransform.getScaleInstance
(myFontScaling / mySystemScaling, myFontScaling / mySystemScaling);
    Font theFont = myFont.deriveFont (fontTransform);
    return new TextLayout (myString, theFont, g2d.getFontRenderContext ());
  }
}

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to