How would I go about rotating a jpg sprite image I have on my Graphics2D Canvas?
Would I use an AffineTransform? How?
There are a lot of these images so I can’t just rotate the Canvas, I have to rotate each individually.
Here is one method I use:
Write a draw(Graphics2D) method for the Sprite class.
In the painting method, you can store the current AffineTransfrom of the Graphics2D with
AffineTransform emptyXform = g2d.getTransform();
for( all the sprites ){
g2d.setTransform(emptyXform);
sprite.draw(g2d)
}
In the draw method of the sprite you can do any transformation necessary on the Graphics2D without wrecking anything. emptyXform simply resets the transform to one which doesn’t do anything (it’s probably just the identity transformation). Simply use
g2d.rotate(angle, centerX, centerY);
inside the draw() methods of the sprites.
Awesome! Thanks! 
So why will it not work? I’m really new to this, so it is probably either a major mistake or just some silly typo.
this is what i call i my game loop:
AffineTransform emptyXform = g.getTransform();
for (int i=0;i<entities.size();i++) {
Entity entity = (Entity) entities.get(i);
g.setTransform(emptyXform);
entity.draw(g);
}
and then I draw my sprites in the entity class:
public void draw(Graphics g) {
if (this.getHeading() == 1) {
g.rotate(45,0,0);
sprite.draw(g,(int) x,(int) y);
}
//and so on, I need to cycle around the eight different headings and rotate
else {
sprite.draw(g,(int) x,(int) y);
}
}
It wont compile
Thanks
Normally best, if it won’t compile, to post the compile error?
I’d suspect though, looking at your code, its because the rotate() method does not exist on Graphics. You’d need to cast it to a Graphics2D first.
Kev
I got it now, and it great. thx