在java中旋转矩形对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18860700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rotating the Rectangle object in java
提问by Chris
Is it possible to rotate the Rectangle
object to a certain degree around it's axis? Is it as easy as Rectangle rect = new Rectangle(x,y,w,h,r)
?
是否可以将Rectangle
对象围绕其轴旋转一定程度?有那么容易Rectangle rect = new Rectangle(x,y,w,h,r)
吗?
If it is not possible to rotate the object, what would be a way I could get similar results?
如果无法旋转对象,我可以获得类似结果的方法是什么?
Edit:for clarity here is my dilemma, I have images that rotate but when they colide with other images the collisions only work at 90 and 180 degree rotations because their hit box Rectangle objects don't rotate.
编辑:为了清楚起见,这是我的困境,我有旋转的图像,但是当它们与其他图像碰撞时,碰撞仅在 90 度和 180 度旋转时起作用,因为它们的命中框 Rectangle 对象不旋转。
采纳答案by Hovercraft Full Of Eels
Edit: for clarity here is my dilemma, I have images that rotate but when they colide with other images the collisions only work at 90 and 180 degree rotations because their hit box Rectangle objects don't rotate.
编辑:为了清楚起见,这是我的困境,我有旋转的图像,但是当它们与其他图像碰撞时,碰撞仅在 90 度和 180 度旋转时起作用,因为它们的命中框 Rectangle 对象不旋转。
You can rotate a Shape-derived object, such as a Rectangle2D by using the AffineTransform method, createTransformedShape(...)
.
您可以使用 AffineTransform 方法旋转 Shape 派生对象,例如 Rectangle2D createTransformedShape(...)
。
Rectangle2D myRect = new Rectangle2D.Double(100, 100, 200, 200);
AffineTransform at = AffineTransform.getRotateInstance(Math.PI / 4, 150, 150);
Shape rotatedRect = at.createTransformedShape(myRect);
Note: code not compiled nor tested.
注意:代码未编译或测试。
回答by tbodt
To rotate the rectangle, you give the graphics context an AffineTransform
for rotation. Here's an example:
要旋转矩形,您可以为图形上下文指定AffineTransform
旋转。下面是一个例子:
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
AffineTransform oldTransform = g2d.getTransform();
g2d.setTransform(AffineTransform.getRotateInstance(radians);
g2d.draw(rectangle);
g2d.setTransform(oldTransform);
}
You can also use g2d.rotate(radians)
for rotation.
您也可以g2d.rotate(radians)
用于旋转。
Note that the angle must be in radians. To convert degrees into radians, use degrees * (Math.PI/180)
for the angle.
请注意,角度必须以弧度为单位。要将度数转换为弧度,请使用degrees * (Math.PI/180)
角度。
回答by Radiodef
There's also another way to do this (besides createTransformedShape
) which creates fewer temporary objects, if that's desirable.
如果需要,还有另一种方法可以做到这一点(除此之外createTransformedShape
),它会创建更少的临时对象。
Instead of keeping a Rectangle2D
for the bounding box, you can keep a Path2D
and then do the transform in place, using Path2D.transform(AffineTransform)
:
Rectangle2D
您可以保留 aPath2D
然后使用Path2D.transform(AffineTransform)
以下方法进行变换,而不是为边界框保留 a :
import java.awt.geom.*;
public class Example {
private final Path2D hitBox;
public Example(Rectangle2D initialHitBox) {
this.hitBox = new Path2D.Double(initialHitBox);
}
public void transform(AffineTransform tx) {
path.transform(tx); // In-place transformation.
}
// ...
}
This is very similar to what AffineTransform
actually does under the hood.
这是非常相似的东西AffineTransform
引擎盖下实际执行。