1

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?

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
dqwih2
  • 9
  • 4
  • The `getXxx` `static` methods give you a starting point, as apposed to having to use the constructors directly, they are convince methods. Once you have the instance of the the class, you can then apply additional transformations – MadProgrammer Feb 03 '22 at 20:18
  • You "could" do something like `AffineTransform at = new AffineTransform();` and then `at.rotate(this.winkel, center.getX(), center.getY());`, but then you'd have to ask the question "why"? When you could just use `getRotateInstance` on a single line ;) – MadProgrammer Feb 03 '22 at 20:21
  • Okay thank you, but how could I use for erxamle AffineTransform at = new AffineTransform() then at.rotate(this.winkel, center.getX(), center.getY()); and after this a concatenate? With for example getRotateInstance I now how I use concatenate, but How I use it for example with at.rotate? – dqwih2 Feb 03 '22 at 21:22
  • Each additional operation on the transformation will be concatenated, just because you started with a `getXxx` method makes no difference, it's just a convince method – MadProgrammer Feb 03 '22 at 21:31
  • Sorry, for the dumb question, but could you "translate" the code in my post as a answer normal, without that get but with concatenat, that I can see that one time? (Sorry, I am a full noob) – dqwih2 Feb 03 '22 at 21:34
  • So I mean without the get etc. ANd I had one question more, can we also write the code withour concatenate? Because we dont have concatenate in our script – dqwih2 Feb 03 '22 at 21:46

1 Answers1

1

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));
trashgod
  • 203,806
  • 29
  • 246
  • 1,045