Frage

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?

War es hilfreich?

Lösung

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();
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top