LetsGoBrandon
LetsGoBrandon

Reputation: 564

How to do a symmetry - Graphics java

So, I have these lines of code :

BufferedImage bufferedImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
    Graphics g = bufferedImage.getGraphics();
    g.setColor(Color.cyan);
    g.fillRect(0, 0, 10, 10);

(In reality I have different shappes drawn on this image..)

But how to to a (vertical) symmetry of this image? Is there a method to do that? Or I have to code the whole code...

Thank you in advance

Upvotes: 0

Views: 725

Answers (1)

donnoh
donnoh

Reputation: 160

From https://examples.javacodegeeks.com/desktop-java/awt/image/flipping-a-buffered-image/

// Flip the image vertically
AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
tx.translate(0, -image.getHeight(null));
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
image = op.filter(image, null);


// Flip the image horizontally
tx = AffineTransform.getScaleInstance(-1, 1);
tx.translate(-image.getWidth(null), 0);
op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
image = op.filter(image, null);

// Flip the image vertically and horizontally; equivalent to rotating the image 180 degrees
tx = AffineTransform.getScaleInstance(-1, -1);
tx.translate(-image.getWidth(null), -image.getHeight(null));
op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
image = op.filter(image, null);

Upvotes: 3

Related Questions