Skip to content
Advertisement

setTransform to rotate Images produces unexpected results

I looked everywhere to see with anyone had the same problem than me but it seems I’m the only one getting this error.

So I’m in Java with Swing. I have a class Player that draws an image of the player. However, each time I tried to use setTransform to rotate my image, the second instance of player is scaled down by two.

Here is my code for the draw method:

AffineTransform transform = new AffineTransform();
transform.rotate(this.getOrientationRadians(), getX()+getWidth()/2,getY()+getHeight()/2);

g.setTransform(transform);
g.drawImage(image, (int)(getX()), (int)(getY()), null);

g.setTransform(new AffineTransform());

Advertisement

Answer

By overwriting the transform of the Graphics object you are also overwriting the scaling imposed by your system scale (which I suppose is set to 200%).

Either restrict to using Graphics2D::rotate or pass the transform the drawImage call.

AffineTransform transform = new AffineTransform();
transform.translate(getX(), getY());
transform.rotate(getOrientationRadians());

g.setTransform(transform);
g.drawImage(image, transform, null);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement