java如何使用Graphics2D围绕两个不同的点旋转两个形状?
我想做一个类似四直升机的形状,所以我必须围绕不同的点旋转不同的形状
以下代码段适用于第一个矩形,而不是第二个矩形
public void render(Graphics2D g) {
// cx and cy is the center of the shape these spin near
// add prop rotation
at = g.getTransform();
at.rotate(Math.toRadians(prop_rotation), cx, cy-42);
g.setTransform(at);
// Rect 1 spins correctly!
g.fillRect(cx-14, cy-45, 28, 6);
at = g.getTransform();
at.rotate(Math.toRadians(prop_rotation), cx, cy+38);
g.setTransform(at);
// Rect 2 spins around rect 1
g.fillRect(cx-14, cy+35, 28, 6);
}
那么,我如何利用多个中心做到这一点呢
# 1 楼答案
转变是一种权责
首先抓取
Graphics
上下文的副本并单独修改它这基本上复制了
Graphics
属性,但仍然允许您绘制相同的“曲面”。更改副本属性不会影响原件另一种可能是改变形状本身
这样,你就不会弄乱
Graphics
上下文(这总是很好的),你会得到一个方便的小片段,可以重复使用,例如,用于另一面