Graphics2D - 在 Graphics2D 对象上旋转形状

发布于 2024-12-18 20:29:10 字数 454 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒废 2024-12-25 20:29:10

不要围绕其中心点旋转形状,而是旋转然后平移画布。要围绕形状中心 (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.

素衣风尘叹 2024-12-25 20:29:10
    AffineTransform afx = new AffineTransform();
    afx.rotate(angleRad, s.getCenter().x, s.getCenter().y);
    //afx.rotate(angleRad);
    java.awt.Shape ss = afx.createTransformedShape(s.getPrimativeShape());
    return ss;   

s 是我的 java.awt.Shape 的包装类,并用它做了一些事情......但你想要的是在第 2 行。
afx.rotate(角度,xAnchorPoint,yAnchorPoint);
afx.rotate 围绕点 (xAnchorPoint;yAnchorPoint) 旋转对象。

希望这是你想要的

    AffineTransform afx = new AffineTransform();
    afx.rotate(angleRad, s.getCenter().x, s.getCenter().y);
    //afx.rotate(angleRad);
    java.awt.Shape ss = afx.createTransformedShape(s.getPrimativeShape());
    return ss;   

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

那支青花 2024-12-25 20:29:10

要旋转形状,请使用 Graphics2D.rotate 方法之一。

缓存用于 shape 1shape 3 的变换。在绘制 shape 3 之前,请确保将变换重置为缓存的变换,因为对 shape 2 使用 rotate 将更改当前变换坐标。

步骤:

  1. 缓存当前变换
  2. 绘制 shape 1
  3. 旋转
  4. 绘制 shape 2
  5. 将变换设置为缓存的一个
  6. 绘制 shape 3

In order to rotate a shape, use one of the Graphics2D.rotate methods.

Cache the transform used for both shape 1 and shape 3. Before drawing shape 3, ensure that you reset the transform to the cached one, since using rotate for shape 2 will alter the current transform coordinates.

Steps:

  1. Cache current transform
  2. Draw shape 1
  3. Rotate
  4. Draw shape 2
  5. Set transform to cached one
  6. Draw shape 3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文