|
Here is the latest incarnation of my
scale/translate code for which I solicited suggestions a couple of days
ago.
This code allows the BufferedImage to grow and
shrink as it is zoomed and always keeps it centered in the JPanel.
Several people suggested code like
this:
>
translate(panelw/2.0,
panelh/2.0);
> scale(scalex, scaleh); > translate(-imgw/2.0, -imgh/2.0); where there is a pre-scale translate and a post-scale translate with a scale sandwiched in between. I was not able to get this to work. Note that some of my images are scaled in the x direction, some in the y direction and some in neither direction in addition to the zoom scaling which is the same in both directions. In the code below does what I want it to do, I do a
single scale followed by a single translate and the translate is
'scaled'.
Any comments or suggestion for improvements would
be welcome.
Thank you,
Ted Hill
protected void paintComponent(Graphics g)
{ super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; Point2D.Float scaleValue = image.getXYScalingFactor( ); double halfPanelWidth =
getWidth( ) / 2;
double halfPanelHeight = getHeight( ) /2; double halfImageWidth =
image.getWidth( ) / 2;
double halfImageHeight = image.getHeight( ) / 2; final double zoomFactor =
image.getZoomFactor( );
final double scaleX = scaleValue.x * zoomFactor; final double scaleY = scaleValue.y * zoomFactor; double
halfScaledImageWidth = halfImageWidth *
scaleX;
double halfScaledImageHeight = halfImageHeight * scaleY; double translateX =
(halfPanelWidth - halfScaledImageWidth) *
(1/scaleX);
double translateY = (halfPanelHeight - halfScaledImageHeight) * (1/scaleY); g2.scale(scaleX,
scaleY);
g2.translate(translateX, translateY);
g2.drawImage(image.getBufferedImage( ), 0, 0,
this);
} |
- Re: [JAVA2D] scale and translate with zoom Ted Hill
- Re: [JAVA2D] scale and translate with zoom Jean Adams
