[英]Rotating rectangle and adjust positon
I'm currently struggling with the rotation of some rectangles. 我目前正在为某些矩形的旋转而苦苦挣扎。 I have a rectangle which consists of 9 small rectangles. 我有一个由9个小矩形组成的矩形。 http://i57.tinypic.com/msn8ue.jpg http://i57.tinypic.com/msn8ue.jpg
Now I rotated the big rectangle accordingly with affine transformation, which worked pretty fine. 现在,通过仿射变换相应地旋转了大矩形,效果很好。 http://i61.tinypic.com/25phlqp.jpg http://i61.tinypic.com/25phlqp.jpg
My problem is that i now need to move the rectangles, so that the top left position of each rectangle is at point x,y. 我的问题是我现在需要移动矩形,以便每个矩形的左上角位置都在x,y点。
I don't know how to do that and I hope that you can help me out. 我不知道该怎么做,希望您能帮助我。
Thanks! 谢谢!
public class MainClass {
public static void main(String[] args) {
JFrame jf = new JFrame("Demo");
Container cp = jf.getContentPane();
MyCanvas tl = new MyCanvas();
cp.add(tl);
jf.setSize(800, 800);
jf.setVisible(true);
}
}
class MyCanvas extends JComponent {
private static final long serialVersionUID = 5703217428905757134L;
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int x = 400;
int y = 400;
int width = 100;
int height = 200;
final int OVERLAPPING_OUTLINE = 10;
Rectangle[] rect = new Rectangle[9];
rect[0] = new Rectangle(x + OVERLAPPING_OUTLINE, y + OVERLAPPING_OUTLINE, width - 2 * OVERLAPPING_OUTLINE, height - 2
* OVERLAPPING_OUTLINE);
rect[1] = new Rectangle(x - OVERLAPPING_OUTLINE, y - OVERLAPPING_OUTLINE, OVERLAPPING_OUTLINE * 2,
OVERLAPPING_OUTLINE * 2);
rect[2] = new Rectangle(x - OVERLAPPING_OUTLINE + width, y - OVERLAPPING_OUTLINE, OVERLAPPING_OUTLINE * 2,
OVERLAPPING_OUTLINE * 2);
rect[3] = new Rectangle(x - OVERLAPPING_OUTLINE, y + height - OVERLAPPING_OUTLINE, OVERLAPPING_OUTLINE * 2,
OVERLAPPING_OUTLINE * 2);
rect[4] = new Rectangle(x - OVERLAPPING_OUTLINE + width, y + height - OVERLAPPING_OUTLINE, OVERLAPPING_OUTLINE * 2,
OVERLAPPING_OUTLINE * 2);
rect[5] = new Rectangle(x - OVERLAPPING_OUTLINE, y + OVERLAPPING_OUTLINE, OVERLAPPING_OUTLINE * 2, height
- OVERLAPPING_OUTLINE * 2);
rect[6] = new Rectangle(x - OVERLAPPING_OUTLINE + width, y + OVERLAPPING_OUTLINE, OVERLAPPING_OUTLINE * 2, height
- OVERLAPPING_OUTLINE * 2);
rect[7] = new Rectangle(x + OVERLAPPING_OUTLINE, y - OVERLAPPING_OUTLINE, width - OVERLAPPING_OUTLINE * 2,
2 * OVERLAPPING_OUTLINE);
rect[8] = new Rectangle(x + OVERLAPPING_OUTLINE, y + height - OVERLAPPING_OUTLINE, width - OVERLAPPING_OUTLINE * 2,
2 * OVERLAPPING_OUTLINE);
for (Rectangle r : rect)
g2.draw(r);
g2.setColor(Color.RED);
AffineTransform af = new AffineTransform();
for (int i = 90; i < 360; i += 90) {
af.rotate(Math.toRadians(i), x - OVERLAPPING_OUTLINE, y - OVERLAPPING_OUTLINE);
for (Rectangle r : rect) {
Shape shape = af.createTransformedShape(r);
g2.draw(shape);
}
}
}
}
看来您需要使用AffineTransform.translate(double tx, double ty)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.