如何在Java中找到锚点/旋转点?

发布于 2024-10-23 15:45:40 字数 602 浏览 6 评论 0原文

好吧,我一直在尝试旋转位于图像顶部的矩形。我有一个显示图像的可滚动类。我可以在图像上绘制矩形。我的问题是在旋转图像时尝试旋转矩形。矩形丢失并放置在错误的位置。

我已经尝试起诉 Graphics2D、AffineTransform、createTransformedShape() 但没有成功。

我现在想做的是手动旋转矩形。我正在尝试使用以下公式获取矩形点(x,y):

double rectX = (Math.cos(Math.toRadians(90)) * (x - anchorX) - Math.sin(Math.toRadians(90)) * (y - anchorY)) + anchorY;

double rectY = (Math.sin(Math.toRadians(90)) * (x - anchorX) - Math.cos(Math.toRadians(90)) * (y - anchorY)) + anchorY;

How can I find theanchorX andanchorY value using Java?我尝试将图像高度降低 2,但不适用于所有旋转角度。我必须获得 JPanel 的宽度和高度吗? Java 是否有用于查找锚点的公式?

Ok I've been trying to rotate a Rectangle that sits on top of an image. I have a Scrollable class that displays the Image. I can draw Rectangles on top of the Image. My problem is trying to rotate the Rectangles when the Image is rotated. The Rectangle gets lost and placed in the wrong location.

I've already tried suing Graphics2D, AffineTransform, createTransformedShape() but no luck.

What I'm trying to do now is to rotate the Rectangle manually. I'm trying to get the Rectangle Point(x,y) using below formula:

double rectX = (Math.cos(Math.toRadians(90)) * (x - anchorX) - Math.sin(Math.toRadians(90)) * (y - anchorY)) + anchorY;

double rectY = (Math.sin(Math.toRadians(90)) * (x - anchorX) - Math.cos(Math.toRadians(90)) * (y - anchorY)) + anchorY;

How can I find the anchorX and anchorY values using Java? I've tried diving the Image height by 2 but doesn't work for all rotation angles. Do I have to get the JPanel width and height or something? Is there a formula that Java uses to find anchor points?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

汹涌人海 2024-10-30 15:45:40

我不确定目标,但我看到两种可能的替代方法:

I'm not sure of the goal, but I see two possible alternative approaches:

  • Use an inverse transform, as shown here.

  • Render the upright image and rectangle(s) into a BufferedImage and rotate the composite image, as shown here.

诗化ㄋ丶相逢 2024-10-30 15:45:40

如果你想让它像一个在顶部滚动的盒子,你必须找到行进方向的底角。如果您向右滚动,则需要右下角。

If you're trying to make it like a a box rolling across the top, you have to find the bottom corner in direction of travel. If you're rolling to the right, you need the bottom right corner.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文