如何使用 AffineTransform 的翻译?

How to use AffineTransform's translate?

我不是在问 AffineTransform 是如何工作的,而是在问如何使用它的 translate 方法。

API我读了很多遍,但还是不明白它是如何工作的。

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   ]

问题:

  1. affineTransform.translate(100, 0) 以某种方式将图像向右移动 +100 像素,向底部移动 +100 像素。为什么会这样?

  2. 如果我需要经常移动我的图像,我是否使用 translate() 来移动我的图像?如果不是,建议 method/ways 将我的 rotating/rotated 图像移动到帧中?

affineTransform.translate(100, 0) somehow moves the image +100 pixel to the right and +100 pixel to the bottom. Why is this so?

一般来说,是的,它应该将绘图偏移移动到 100x 和 0y,这意味着 Graphics 上下文的 0x0 位置现在将从原点 100x0原始 Graphics 上下文。

请记住,翻译是累积的,这意味着如果您之前以某种方式翻译了 Graphics 上下文,现在将添加到其中(第一次翻译 0x100,第二次翻译 100x0,你现在 100x100 从原来的 Graphics 上下文原点...

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?

通常,我使用 Graphics#create 创建 Graphics 上下文的副本(这会复制当前的 state/properties,但仍会生成返回 Graphics 上下文的原始输出的输出=11=] 上下文),将翻译应用于副本,绘制我想要的任何内容,然后 dispose 副本。这使原始上下文保持不变(关于我已更改的属性)

这意味着您可以单独进行多项翻译,而不会影响以后执行的其他翻译。

另一种方法是反转翻译,但坦率地说,在副本上调用 dispose 更简单、更容易...