I am not asking how AffineTransform works, but how to use its translate method.
I read the API many times, but still do not understand how it works.
public void translate(double tx,double ty)
Concatenates this transform with a translation transformation. This is equivalent to calling concatenate(T), where T is an AffineTransform represented by the following matrix:
[ 1 0 tx ] [ 0 1 ty ] [ 0 0 1 ]
Questions:
affineTransform.translate(100, 0) somehow moves the image +100 pixel to the right and +100 pixel to the bottom. Why is this so?
Do I use translate() to move my images if I need to move my images very often? If not, what is the recommended method/ways to move my rotating/rotated images in a frame?
affineTransform.translate(100, 0) somehow moves the image +100 pixel to the right and +100 pixel to the bottom. Why is this so?
Generally speaking, yes, it should move the drawing offset to 100x and 0y, meaning that the 0x0
position of the Graphics
context will now be 100x0
from the origin point of the original Graphics
context.
Remember, translations are accumulative, this means that if you had previously translated the Graphics
context in some way, this will now add to it (first translation 0x100
, second translation 100x0
, you are now 100x100
from the original Graphics
contexts origin point...
Do I use translate() to move my images if I need to move my images very often? If not, what is the recommended method/ways to move my rotating/rotated images in a frame?
Generally, I use Graphics#create
to create a copy of the Graphics
context (this copies the current state/properties, but will still generate output back to the original output of the Graphics
context), apply the translations to the copy, paint whatever I want and then dispose
of the copy. This leaves the original context unchanged (in regards to the properties that I have changed)
This means you can do multiple translations in isolation which won't affect other translations performed later.
Another method would be to reverse the translations, but frankly, calling dispose
on a copy is just simpler and easier...