I needed to flip a BufferedImage and i found this code to make it:
// Flip the image vertically
AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
tx.translate(0, -bufImage.getHeight(null));
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
bufImage = op.filter(bufImage, null);
Trying to understandi it i read the documentation but is not clear for me, Why is necessary this line tx.translate(0, -bufImage.getHeight(null));
Could anyone explain me this few lines in a simple way?
When the bufferedimage is transformed with the scaling factor the points are flipped around the origin, so (1,5) would for example become (1,-5), the translation is necessary to move the image data back to the original rectangle it would have occupied. So for example lets say that the image runs from (0,0) to (10,10), after the flip we have a rectangle from (0,-10) to (10,0) and we want to move that rectangle back into our original position by shifting it by 10.
When the .translate() method is called the resulting matrix is OriginalMatrix*TranslationMatrix as a result the translation is being modified by the flip as well.