简体   繁体   中英

Java, Rotating an image around itselft

I know how to rotate an image in java, but I can't find a way to rotate an image around its center using the Graphics2D.rotate method. Here is what I have.

public void rotateLeft(Graphics2D g) {
    rotateLeft++;

    g.rotate(Math.toRadians(rotateLeft), charX, charY);
}

charX and charY are coordinates by the way... So, can anyone help me?

您可以使用:

g.rotate(angle, (imageWidth / 2) + 1, (imageHeight / 2) + 1);

This is how I am doing it:

        AffineTransform oldTrans = g2d.getTransform();
        g2d.rotate(-theta,xNow+bufferedBox.getWidth()/2, this.getHeight() - groundY - yNow - bufferedBox.getHeight()/2);
        g2d.drawImage(bufferedBox, xNow, this.getHeight() - groundY - yNow - bufferedBox.getHeight(), null);
        g2d.setTransform(oldTrans);

xNow an yNow are my coordinates where my box will be. "this" refers to the jPanel, groundY is the offset (it's on top of the ground). bufferedBox refers to my image

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