javaswingaffinetransform

Problems with 2D Graphics: equivalent AffineTransform codes?


I'm learning Java. I've worked my way through it, but I have a big problem with 2D graphics. In the lectures, regarding AffineTransform, we learned about these codes:

 1. translate(double x, doubley),
 2. scale(double x, double y),
 3. rotate(double theta),
 4. shear(double x, double y),
 5. transform(AffineTransform at) and
 6. setTransform(AffineTransform).

And my biggest problem now is our professor said that with these codes everything regarding transformations is feasible. That's all he showed us.

AffineTransform at = AffineTransform.getTranslateInstance(this.pos.getX(), this.pos.getY());
at.concatenate(AffineTransform.getScaleInstance(this.scaleX, this.scaleY));

Shape shape = at.createTransformedShape(this.form);
Point2D center = getCenter(shape);

at = AffineTransform.getRotateInstance(this.winkel, center.getX(), center.getY());
return at.createTransformedShape(shape);

My problem now is this, for example. Why don't we use for example at.rotate and we use at = AffineTransform.getRotateInstance?

Why do we use at=AffineTransform.getTranslateInstance and not at.translate?

I do not get it. Our prof said the commands are enough, and now the new one was added? Can I make what is in this code e.g. equivalent to my listed codes?

Because our professor said I can do everything with the codes I have listed, but the instructor never uses them! He uses these codes that I don't get, I have been practicing and training with these listed codes all the time and now I was so surprised.

My main question here: Can I somehow get the same result with the listed codes as with this pasted code? Is there any way to replace this?


Solution

  • Note that the AffineTransform method concatenate() uses matrix multiplication, which is not commutative. The advantage of concatenating transformations is that multiplication may be performed once and the result reused repeatedly. Focusing on the concrete example cited here, given an identity transform named at:

    AffineTransform at = new AffineTransform();
    

    The following three formulations are equivalent:

    at.translate(SIZE/2, SIZE/2);
    at.scale(60, 60);
    at.rotate(Math.PI/4);
    
    at.concatenate(AffineTransform.getTranslateInstance(SIZE / 2, SIZE / 2));
    at.concatenate(AffineTransform.getScaleInstance(60, 60));
    at.concatenate(AffineTransform.getRotateInstance(Math.PI / 4));
    
    at.preConcatenate(AffineTransform.getRotateInstance(Math.PI / 4));
    at.preConcatenate(AffineTransform.getScaleInstance(60, 60));
    at.preConcatenate(AffineTransform.getTranslateInstance(SIZE / 2, SIZE / 2));