-1

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());
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • You should create a small example, that code doesn't even show the second "player" you mentioned. Also resetting the transformation to an old value doesn't work that way. – Beowolve Aug 13 '21 at 09:10
  • The second player has the same code as it is an instance of Player. "Also resetting the transformation to an old value doesn't work that way", I don't know how to do it... – MDavid power's my intelligence Aug 13 '21 at 09:21
  • AffineTransform oldTransform = g.getTransform() .... g.setTransform(oldTransform). Have to see the rest of the code to understand the problem – Beowolve Aug 13 '21 at 09:23
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. The code in [this answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Aug 13 '21 at 10:09

1 Answers1

2

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);
weisj
  • 932
  • 6
  • 19