Using "Affine Transformation" I can rotate imageA
easily. As well, imageA
will move along with imageB
. However, I cannot seem to find a way to move imageA
back to its original position after I have rotated it.
(I have done some research on some sites and apparently the best method is to move the image back to its original position so that it looks like its rotating from an anchor point.)
Heres my code so far:
public void paintComponent(Graphics g) {
super.paintComponent(g);
AffineTransform af = new AffineTransform();
Graphics2D g2d = (Graphics2D) g;
af.translate(imageBx, imageBy); // moves ImageA to imageb's position
af.rotate(Math.toRadians(angle), imageB.getHeight(this) / 2, imageB.getWidth(this) / 2);
g2d.drawImage(imageA, af, null);
g2d.drawImage(imageB, imageBx, imageBy, null);
}
If anyone can help me move imageA
back to its original position (which is right on imageB
) that would extremely helpful!
I looked that over, but the code rotates the entire panel; I just want to rotate one
Image
on a fixed rotate point.
Two things may help guide your understanding:
The example cited uses rotate(double theta)
; it is preceeded by a translation to the origin and succeeded by a translation the panel's center. Note that the operations are performed in the apparent reverse of the declared order. Your example (may have meant to) invoke rotate(double theta, double anchorx, double anchory)
. The two effect the same result, the latter being a convenient alternative to the former.
This example contrasts how one can transform the graphics context (g2d) or the image itself. Your example invokes drawImage(Image img, AffineTransform xform, ImageObserver obs)
, which concatenates xform
to the existing graphics transform; this affects all subsequent drawing. It may be easier to keep them separate.