简体   繁体   中英

How to rotate an image around a point in Java

I have been looking at some similar questions but none answer exactly what I need. In the solutions I found, everyone rotated the image without moving, but what I need is for this image to rotate around the initial position, and not for it to rotate in position.

Code I was using (what I found):

        AffineTransform transform = new AffineTransform();
        transform.rotate(Math.toRadians(angle), img.getWidth() / 2, img.getHeight() / 2);
        AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
        img = op.filter(img, null);

        g.drawImage(img, getX(), getY(), null);

I need the image to rotate in relation to the first pixel of the image.

The rotate method you're calling passes in the anchor point as the center of the image: https://docs.oracle.com/javase/7/docs/api/java/awt/geom/AffineTransform.html#rotate(double,%20double,%20double)

Try just passing in the rotation angle itself and it should rotate around the top-left:

transform.rotate(Math.toRadians(angle));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM