I am trying to rotate a Rectangle from the vertices closest to the origin, but it is placing it way off the frame.
I have the following class:
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);
}
}
I am caling the rectangle with:
MyRectangle rect = new MyRectangle(10, 200, 30, 50, 70);
I included the original un-rotated rectangle. It should rotate from the bottom left vertex.