Graphics2D - 在 Graphics2D 对象上旋转形状
我有一个 Graphics2D
对象,用它在我的 Canvas
上绘图。我在 Canvas
上绘制了多个形状,并且只想变换其中一个(或部分)。
我会尽量保持简单:
void render(Graphics2D g) {
... // Draw shape 1
... // Draw shape 2
... // Draw shape 3
}
如何在保持形状 1 和 3 不变的情况下旋转形状 2? 我所说的“旋转”是指围绕其中心点旋转,我们可以将其定义为 <例如,code>x 和y
。
我一直在寻找一种方法来做到这一点,但找不到任何可以按照我想要的方式工作的方法。
有什么简单的方法可以做到这一点吗?
I have a Graphics2D
object which I use to draw on my Canvas
. I draw multiple shapes on the Canvas
and want to transform only one (or part) of them.
I'll try to keep this simple:
void render(Graphics2D g) {
... // Draw shape 1
... // Draw shape 2
... // Draw shape 3
}
How would I go about rotating shape 2 while leaving shape 1 and 3 intact? By "rotate" I mean rotating around its center point, which we can define as x
and y
for example.
I've been looking for a way to do this for a while now, but couldn't find anything that works the way I want it to.
Is there any simple way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要围绕其中心点旋转形状,而是旋转然后平移画布。要围绕形状中心
(x, y)
旋转,请首先将画布平移(-x, -y)
,然后旋转画布- d
度并在 (0,0) 处正常绘制形状。完成后,旋转回来,然后平移回来(请注意,对于这些几何变换,顺序很重要,平移然后旋转会给你一个完全不同的结果)。
这意味着您仍然可以以任何旋转方式绘制对象,而无需自己重新计算坐标。
Rather than rotating the shape around it's centre point, rotate and then translate the canvas. To rotate around the centre of the shape at
(x, y)
, first translate the canvas by(-x, -y)
and then rotate the canvas-d
degrees and draw the shape as normal at (0,0).When you're done, rotate back then translate back (note that with these geometric transformations the order is important, translating and then rotating will give you a completely different outcome).
This means that you can still draw an object at any rotation without having to recalculate the coordinates yourself.
s
是我的 java.awt.Shape 的包装类,并用它做了一些事情......但你想要的是在第 2 行。afx.rotate(
角度,
xAnchorPoint,yAnchorPoint);
afx.rotate 围绕点 (xAnchorPoint;yAnchorPoint) 旋转对象。
希望这是你想要的
s
is my wrapper class for java.awt.Shape and does some stuff with it.... But what you want is in line 2.afx.rotate(
Angle,
xAnchorPoint,yAnchorPoint);
afx.rotate rotates the object about the point (xAnchorPoint;yAnchorPoint).
Hope this is what you wanted
要旋转形状,请使用 Graphics2D.rotate 方法之一。
缓存用于
shape 1
和shape 3
的变换。在绘制shape 3
之前,请确保将变换重置为缓存的变换,因为对shape 2
使用rotate
将更改当前变换坐标。步骤:
shape 1
shape 2
shape 3
In order to rotate a shape, use one of the
Graphics2D.rotate
methods.Cache the transform used for both
shape 1
andshape 3
. Before drawingshape 3
, ensure that you reset the transform to the cached one, since usingrotate
forshape 2
will alter the current transform coordinates.Steps:
shape 1
shape 2
shape 3