You can reduce the number of calculations you are doing a bit.

   final int   halfWidth      = bufImage.getWidth( ) / 2;
   final int   halfHeight     = bufImage.getHeight( ) / 2;
   float translateX       = halfWidth   - halfWidth / zoomFactor;
   float translateY       = halfHeight - halfHeight / zoomFactor;
   g2.translate( -translateX, -translateY );

At 09:11 AM 8/30/00 -0500, Ted Hill wrote:
>I posted a question yesterday on scaling and translating, I think I have
>found my answer.
>
>I don't understand all the details, but it looks like after calling the
>scale( ), the translate( ) call must also be scaled:
>
>     protected void paintComponent(Graphics g)
>     {
>         super.paintComponent(g);
>
>         Graphics2D g2 = (Graphics2D) g;
>
>         final int   width      = bufImage.getWidth( );
>         final int   height     = bufImage.getHeight( );
>         final float zoomFactor = getZoomFactor( );
>
>         // translate by half the 'growth'
>         float translateX       = 0.5f * ((width  * zoomFactor) - width);
>         float translateY       = 0.5f * ((height * zoomFactor) - height);
>
>         // make image larger
>         g2.scale(zoomFactor, zoomFactor);
>
>         // keep image center centered
>         g2.translate( - (translateX * (1/zoomFactor)), - (translateY *
> (1/zoomFactor)) );
>
>         g2.drawImage(bufImage, 0, 0, this);
>     }
>
>In the above code, I multiply my translateX and translateY by the inverse
>of the zoomFactor which was used to do the scaling. Now as the image gets
>bigger, it stays centered.

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