Hello,
I have a question relating to using the AffineTransform's preconcatenate
method to increase and decrease scaling.
------------------------------------------------
Background:
I have a panel that presents a buffered image to the user. The user can
increase the scale (zoom in) by dragging
the mouse up in the panel.
The user can decrease the scale by dragging down.
I want to be able to set a min and a max zoom limit.
What I do on mouse drag is to convert the mouse event's delta-y value to
a zoom/scale increment.
Then, if I have not yet reached the min/max scale values, I create a new
AffineTransform, based
on the delta-Y val of the mouse event, like this:
double zoomIncrement = // some value based on mouse delta-Y
AffineTransform scaleIncrementAtx =
AffineTransform.getScaleInstance(zoomIncrement, zoomIncrement);
Then I preconcatenate this atx with the existing atx, like this:
currentTransform.preConcatenate(scaleIncrementAtx);
So my current logic is like this:
if(currentScaleY < SCALE_MAX)
{
currentTransform.preConcatenate(scaleIncrementAtx);
}
This works very well. I get nice smooth zoom in/out as the user is
dragging the mouse.
However, the problem is that when the user gets close to either the min
or max limits,
the last preconcatenation can put the above the max or below the min.
This is because I can't predict what the scaleY value will be
following the preConcatenation operation. I can only test it
after the fact.
So my logic should be like this
if(currentScaleY < SCALE_MAX)
{
AffineTransform tempAtx = new AffineTransform(currentTransform);
tempAtx.preConcatenate(scaleIncrementAtx);
if(getScaleYElement(getScaleYElement(tempAtx)) < SCALE_MAX)
{
currentTransform.preConcatenate(scaleIncrementAtx);
}
else // calc the increment that will result in max scale
value
{
// ?? how to calc zoomIncrement in this case ??
zoomIncrement = ??
AffineTransform adjustedIncrementAtx =
AffineTransform.getScaleInstance(zoomIncrement, zoomIncrement);
currentTransform.preConcatenate(adjustedIncrementAtx);
}
}
-------------------------------------------------
Question:
Is there a way to calculate the zoomIncrement value in:
AffineTransform scaleIncrementAtx =
AffineTransform.getScaleInstance(zoomIncrement, zoomIncrement);
Such that we can control the value that will be returned by
getScaleYElement
following the preconcatenation as shown above and in the snippet below?
For example:
------------------ start snippet -----------------
static final double SCALE_MAX = 10;
double currentScaleY = getScaleYElement(currentTransform);
// ?? how to code calculateScaleIncrement(double, double) ??
double scaleIncrement = calculateScaleIncrement(currentScaleY,
SCALE_MAX);
AffineTransform scaleIncrementAtx =
AffineTransform.getScaleInstance(scaleIncrement, scaleIncrement);
currentTransform.preConcatenate(scaleIncrementAtx);
// !! now getScaleYElement() should return a value very close to
SCALE_MAX
double newScaleY = getScaleYElement(currentTransform);
------------------ end snippet ---------------------
---------------------------------
Other methods referred to above:
/**
* Calculate and return the y scale factor for the specified
AffineTransform.
*
* @param atx the AffineTransform whose Y scale element we want.
*
* @return the Y scale value.
*/
public static double getScaleYElement(final AffineTransform atx)
{
double scaleYElement = 0;
final double scaleY = atx.getScaleY();
final double shearY = atx.getShearY();
scaleYElement = Math.sqrt(Math.pow(scaleY, 2) + Math.pow(shearY,
2));
return scaleYElement;
}
/**
* Concatenate the user's current action (pan, zoom, rotate) to the
current
* image transform state and repaint the image.
* Note use of preconcatenate here: apply the user's atx *after*
applying
* the existing mouseBaseTransform.
* @param atx the 'delta' affine transform, i.e. pan or scale
'increment'.
*/
protected void updateImageTransform(final AffineTransform atx) //
see FusionPanel
{
userTransform = new AffineTransform(mouseBaseTransform);
userTransform.preConcatenate(atx);
repaint();
}
===========================================================================
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".