I am trying to draw a rectangle with java awt and make it rotate with mouse cursor by mouse dragging.
When I was testing it out, the rectangle was rotating ridiculously fast.
My Rectangle():
private Rectangle2D rec = new Rectangle2D.Float(x0,y0,w,h);
AffineTransform recTrans = new AffineTransform();
int pivotX = x0+w/2, pivotY = y0+h;
// (0,0) is at the top-left corner
My paintComponent():
public void paintComponent(Graphics g) {
Graphics2D graph = (Graphics2D) g;
graph.translate(x,y);
graph.transform(recTrans);
graph.fill(rec);
graph.setColor(Color.blue);
graph.draw(rec);
}
My mouse dragging event:
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
previousX = e.getX();
previousY = e.getY();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
currentX = e.getX();
currentY = e.getY();
double angle1 = Math.atan2(currentY-pivotY, currentX-pivotX);
double angle2 = Math.atan2(previousY-pivotY, previousX-pivotX);
double theta = angle2 - angle1;
recTrans.rotate(theta, pivotX, pivotY);
}
});
So supposedly the scenario looks like:
But when I slightly drag(theta less than 10 degree) the rectangle to the right side, the rectangle rotates even to the bottom of the pivot point.
Another note, the rectangle rotates but the coordinates of the four corners of the rectangle yet have changed.
I am quite lost when doing those transformation tasks with java..