zfollette
zfollette

Reputation: 487

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?

Upvotes: 1

Views: 200

Answers (2)

Brandon
Brandon

Reputation: 703

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

Upvotes: 0

Reimeus
Reimeus

Reputation: 159874

You can use:

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

Upvotes: 4

Related Questions