java围绕中心旋转矩形 [英] java rotate rectangle around the center

查看:950
本文介绍了java围绕中心旋转矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想围绕其中心点旋转一个矩形,它应该保留在它应该绘制的位置并在该空间中旋转

I would like to rotate a rectangle around its center point and it should remain in the place that it is supposed be drawn and rotate in that space

这是我的代码:

AffineTransform transform = new AffineTransform();

    transform.rotate(Math.toRadians(45),rectangle.width/2, rectangle.height/2);
    Shape transformed = transform.createTransformedShape(rectangle);
    g2.fill(transformed)

该矩形被旋转,但它被绘制在一个不同的屏幕的一部分,我该如何解决这个问题?

the rectangle is rotated but it is drawn at a different part of the screen, how can I correct this?

推荐答案

我还没有试过这个,但看起来你不是获得矩形的正确中间。尝试:

I haven't tried this, but it seems you aren't getting the correct middle of the rectangle. Try:

AffineTransform transform = new AffineTransform();
transform.rotate(Math.toRadians(45), rectangle.getX() + rectangle.width/2, rectangle.getY() + rectangle.height/2);
g2.fill(transformed);

现在,您将宽度添加到起始X点并将高度添加到开始Y点,因此矩形的中间。

The difference is now you're adding the width to the starting X point and adding the height to the starting Y point, hence the middle of the rectangle.

希望这有助于。

这篇关于java围绕中心旋转矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆