2

In Java, instead of using photoshop to transform my images(that I use in the program), I want to use code to transform and save them.

I have created an AffineTransform object "at" and called the rotate() method. I have a BufferedImage called "image".

I can draw the image on the screen with the desired transformation with this code:

g2d.drawImage(image, at, null);

What I want to do is to store the combination of at and image in a new BufferedImage image2. How can I do this so thatg2d.drawImage(image2,50,50, null); will show the rotated version of image?

edit: I've tweaked Ezequiel's answer a bit to get the effect I wanted. This did the trick:

BufferedImage image2= null;
AffineTransformOp affineTransformOp = new AffineTransformOp(at,AffineTransformOp.TYPE_BILINEAR);
image2 = affineTransformOp.filter(image, image2);
g2d.drawImage(image2, 50, 50, null);
WVrock
  • 1,694
  • 3
  • 21
  • 29

1 Answers1

2

With AffineTransformOp class:

BufferedImage original; //Instatiate with desired image.
BufferedImage transformed:  //Used to store transformed image.
AffineTransform at; //Transformations needed.

AffineTransformOp affineTransformOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
affineTransformOp.filter(original, transformed );
tmarwen
  • 14,219
  • 4
  • 40
  • 59
Ezequiel
  • 3,397
  • 1
  • 17
  • 27
  • This is probably what I need but I have some problems. If I initiate transformed image as null nothing happens and the screen is empty. If I initiate it with an image then the original image is on top of the chosen image of the transformed image. – WVrock Oct 21 '14 at 14:02
  • I've fixed it by doing this: `transformed = affineTransformOp.filter(original, transformed );` Thank you for the answer. – WVrock Oct 21 '14 at 14:17