Marco -

I hope you won't take a little code analysis the wrong way:

>     FontMetrics fm = gc.getFontMetrics(); // gc = Graphics2D instance
>     Rectangle2D box = fm.getStringBounds(theText.trim(), gc);
>     float incrX = (float)(box.getWidth()/2.0);
>     float incrY = (float)(box.getHeight()/2.0);
>     gc.setPaint(Color.black);
>
>     // apply transform first
>     AffineTransform aft =
>(gc.getDeviceConfiguration()).getDefaultTransform();

## This is useless.  You might as well say:
##
##    AffineTransform aft = new AffineTransform();
##
## The next line (setToTranslation()) will obliterate ANY transform
## setting that was in aft.

>
>     // apply translation then rotate
>     aft.setToTranslation(currX, currY); // position of centre of text box
>     gc.transform(aft);
>     aft.setToRotation(Math.toRadians(-currAngle)); // rotation for this
>string
>     gc.transform(aft);

## There is a much more convenient way to do this:
##
##    gc.translate(currX, currY);
##    gc.rotate(Math.toRadians(-currAngle));
##

>
>        gc.drawString(theText.trim(), -incrX, incrY);
>
>     // undo the transform
>     aft.setToRotation(Math.toRadians(currAngle));
>     gc.transform(aft);
>     aft.setToTranslation(-currX, -currY);
>     gc.transform(aft);

## A better way to undo the transform is to do one of the following:
## 1) Put this at the start of the drawing code:
##
##    AffineTransform originalAT = gc.getTransform();
##
## and put this after the drawing code:
##
##    gc.setTransform(originalAT);
##
## 2) If you will be setting and resetting other gc state, consider
## putting this at the start of the code:
##
##    gc = (Graphics2D)gc.create();
##
## and closing your code with:
##
##    gc.dispose();
##

For more info on using Affine Transforms in Java2D, see my tutorial:

        http://www.glyphic.com/transform/

Specifically, sections 5 & 6 deal with these kinds of code issues.  (But
don't miss the animated applets in sections 1, 2, 3, 4 and 7.)

>Although the first character of the text string is postioned & rotated
>correctly, each subsequent character seems to be slightly offset from the
>previous character below the (rotated) string baseline giving a stepped
>appearance.
>
>Has anyone any thoughts on how to cure this.

I believe that turning on anti-aliasing will cure it:

        gc.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);

        - Mark

-------------------
Mark Lentczner
President
Glyphic Technology
444 Castro Street, Suite 811
Mtn. View, CA 94041

[EMAIL PROTECTED]
http://www.glyphic.com/
650/964-5311 voice
650/967-4379 fax
=====================================================================
To subscribe/unsubscribe, send mail to [EMAIL PROTECTED]
Java 2D Home Page: http://java.sun.com/products/java-media/2D/

Reply via email to