如何在Java中找到锚点/旋转点?
好吧,我一直在尝试旋转位于图像顶部的矩形。我有一个显示图像的可滚动类。我可以在图像上绘制矩形。我的问题是在旋转图像时尝试旋转矩形。矩形丢失并放置在错误的位置。
我已经尝试起诉 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定目标,但我看到两种可能的替代方法:
使用逆变换,如此处所示.
将直立图像和矩形渲染为
BufferedImage
并旋转合成图像,如此处所示。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.如果你想让它像一个在顶部滚动的盒子,你必须找到行进方向的底角。如果您向右滚动,则需要右下角。
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.