0

Code:

AffineTransform at = new AffineTransform();
at.scale(2, 1);
at.rotate(Math.toRadians(45));
at = new AffineTransform(at);
at.translate(-img.getWidth()/2, -img.getHeight()/2);
g2D.setTransform(at);
g2D.drawImage(img, 0, 0, null);
g2D.drawImage(img, 16, 0, null);
g2D.drawImage(img, 32, 0, null);
g2D.dispose();

This draws my image (original size 16x16) at locations [0,0], [16,8], [32,16]. i.e. it takes the original axis which is now transformed and draws it on the transformed coordinates.

However, I do not always want this. How can I get the image to display at the exact coordinates I am feeding into it, ignoring the transformed X axis and Y axis?

gamesaucer
  • 113
  • 1
  • 10

1 Answers1

0

You need to save the transform of the Graphics2d object before you set it to a new transform, this way you can just switch to the original reference system when you are done with your transforms.

AffineTransform at = new AffineTransform();
AffineTransform g2dAffineTransform = g2d.getTransform();
at.scale(2, 1);
at.rotate(Math.toRadians(45));
at = new AffineTransform(at);
at.translate(-img.getWidth()/2, -img.getHeight()/2);
g2D.setTransform(at);
g2D.drawImage(img, 0, 0, null);
g2D.drawImage(img, 16, 0, null);
g2D.drawImage(img, 32, 0, null);

//Reset the transform
g2d.setTransform(g2dAffineTransform);
//All rendering with this g2d is now at the original coordinate system
//g2d.draw...
g2D.dispose();
arynaq
  • 6,710
  • 9
  • 44
  • 74
  • That works fine, but isn't exactly an answer to my question. What this does, is resetting the transformation after drawing the images. However, that's not my problem. My problem is that if I apply my `AffineTransformation` on my `Graphics2D`, the x and y axis become transformed too. I want to draw a transformed image on a non-transformed axis. I do appreciate your reply, though. – gamesaucer Jun 09 '13 at 20:47
  • Your answer did lead me to `AffineTransformOp` because you use a few things I haven't thought of before, which lead me there. I'm now transforming my images using `AffineTransformOp`, which only transforms the images and not the graphics. Thanks! – gamesaucer Jun 09 '13 at 21:33