我不是在问 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 ]
问题:
affineTransform.translate(100, 0) 以某种方式将图像向右移动 +100 像素并向底部移动 +100 像素。为什么会这样?
如果我需要经常移动图像,是否可以使用 translate() 来移动图像?如果不是,在框架中移动旋转/旋转图像的推荐方法是什么?
最佳答案
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
上下文的副本(这会复制当前状态/属性,但仍会生成返回原始的输出Graphics
上下文的输出),将翻译应用于副本,绘制我想要的任何内容,然后 dispose
副本。这使原始上下文保持不变(关于我已更改的属性)
这意味着您可以单独进行多项翻译,而不会影响以后执行的其他翻译。
另一种方法是反转翻译,但坦率地说,在副本上调用 dispose
更简单、更容易...
关于java - 如何使用 AffineTransform 的翻译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28662463/