Search code examples
javaaffinetransform

why move position that i use affine transform in java 1.6se


i want to scale shape.

so i use setToScale method in affine transform.

then, not only shape's length is trans, but also shape's starting points are moved

why that?

public void initResize(int x, int y) {
    oldX = x;
    oldY = y;
}

public void resize(int x, int y) {
    double xratio = (double)(x - shape.getBounds().x) / (shape.getBounds().width);
    double yratio = (double)(y - shape.getBounds().y) / (shape.getBounds().height);

    af.setToScale(xratio, 1);
    shape = af.createTransformedShape(shape);
    anchor.resize(shape.getBounds());

    oldX = x;
    oldY = y;
}

method call's order is MousePress : initResize, MouseDrgged : resize

x,y is mouse's coord


Solution

  • You have to apply operations on the AffineTransform object. In your case, you want to scale:

    af.scale(xratio, 1);
    

    The idea is that you multiply the current matrix with the matrix that represents the scale. That is how combining transformations works: by multiplying the matrixes (Be aware! the order of the multiplications are important and also A*B != B*A when multiplying matrixes).

    This, instead of clearing the whole matrix and inserting scaling values following the default linear scale template for a matrix.