Jim, Ted,

> The typical way that I do this kind of "transforming about a point
> other than the origin" is:
>
>         imgx = imgw/2;
>         imgy = imgh/2;
>         translate(imgx, imgy);
>         scale(scalex, scaley);
>         translate(-imgx, -imgy);
>
> That lets the transform object do the calculations for you.  Actually
> to ensure that the image is always centered in the panel even if it
> doesn't match the size of the image, I would do:
>
>         translate(panelw/2.0, panelh/2.0);
>         scale(scalex, scaleh);
>         translate(-imgw/2.0, -imgh/2.0);

     We had a lot of fun with this basic problem, except we were
trying to rotate, translate, and scale, and the image height and width
were different, so we had to translate to center as well as to retaint
he correct alignment.

     Two gotchas to watch out for are:

     1) rounding errors will creep up on you if you do a lot of
        calculations

     2) the process is very sensitive to order-of-operation

     We had a long and painful experience.  Order of operations turned
out to be a bigger pain than rounding errors.  We were about to rip
everything apart and recode all our calculations using BigDecimal when
we figured out that most of our error was due to order-of-operation. I
thought I'd tested order-of-operation but it only turned up as a
problem when stopped, tore the whole thing apart and rebuilt it one
step at a time, testing as I added each step.  So I guess that part is
a standard programming cautionary tale, though I suspect if I could
wrap my head around the Affine Transform math better I would have had
a better feel for it and maybe caught it earlier.

     Once we got that straightened out, the rounding errors were small
enough that we decided we could live with them until we had time to go
back and recode all of that.

     The big pain was that although AffineTransform.rotate() has a
convenience method to specify which point to rotate around (we just
rotated around the center), scale does not.  So we:

     1) rotated first,

     2) translated (calculating the offset with some clever math that
        took into account the planned scaling factor)

     3) scaled (both X and Y by the same scaling factor).

     As I vaguely recall, leaving scaling till last was the important
part of the order-of-operation.

Steven J. Owens
[EMAIL PROTECTED]

===========================================================================
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