AffineTransform tfm = new AffineTransform();
// There is no need to do this:
tfm.rotate(0,0,0);
g2.setTransform(tfm);
// unless you are planning to rotate the background image.
// In such a case you do not use the setTransform method.
// As mentioned in the Method Detail for this method you use
// this only one time to restore the origingal transform after
// you are finished with all transform operations on the
// graphics context (g). However, this may not be necessary in
// AWT drawing since the graphics context may not be used after
// your are finsihed with it. Restoring the original transform
// with setTransform is usually necessary in Swing drawing,
// preferably done in the paintComponent method, in which
// the graphics context is later passed to the paintChildren and
// paintBorder methods (see JComponent api Method Detail
// for the paint method).
// The recommended way to do this then would be:
// Save the original transform for later restoration:
AffineTransform origXform = g2.getTransform();
// apply a rotation transform to the graphics context:
g2.rotate(theta, x, y);
// draw the background image.
g2.drawImage(backImage, 0, 0, this);
// Reverse the last transform:
g2.rotate(-theta, x, y);
// Apply rotation transform to graphics context for player image:
g2.rotate(Math.toRadians(player.angle+90), player.x+32, player.y+32);
//g2.setTransform(tfm);
g2.drawImage(tank, player.x, player.y, this);
// and then after you are done with all transform ops, reset
// the transform to the original (use this method only one time):
g2.setTransform(origXform);
// or you could reverse the rotation transform applied above
// for the player image. You want the transform at the end of
// this method to be the same as it was at the beginning.
// Of course, this may be unnecessary, as mentioned above.
// If so, you could omit the setTransform calls and use
// the g2.rotate/translate/etc. – type methods to
// transform/draw/reverse_transform the graphics context as
// shown above. |