Java:旋转矩形

时间:2016-09-15 21:13:36

标签: java swing graphics

我正在尝试从距离原点最近的顶点旋转一个矩形,但它将它从框架上移开。

我有以下课程:

class MyRectangle {

    MyRectangle(int x, int y, int w, int h, int a){
         xPos = x;
         yPos = y;
         width = w;
         height = h;
         angle = a;
    }

    public int xPos;
    public int yPos;
    public int width;
    public int height;
    public int angle;


    public void paintComponent(Graphics g) {
        g.drawRect(xPos, yPos, width, height);

        Rectangle rect2 = new Rectangle(xPos, yPos, width, height);
        Graphics2D gg = (Graphics2D) g.create();
        AffineTransform transform = new AffineTransform();
        transform.rotate(angle, rect2.getX() + rect2.width/2, rect2.getY() + rect2.height/2);
        AffineTransform old = gg.getTransform();
        gg.transform(transform);
        gg.rotate(Math.toDegrees(-angle));
        gg.draw(rect2);
    }
}

我用:

来调整矩形
MyRectangle rect = new MyRectangle(10, 200, 30, 50, 70);

它显示如下: enter image description here

我包含原始的未旋转矩形。它应该从左下角旋转。

0 个答案:

没有答案