Flip Image with Graphics2D

前端 未结 5 989
一个人的身影
一个人的身影 2020-12-08 10:27

I\'ve been trying to figure out how to flip an image for a while, but haven\'t figured out yet.

I\'m using Graphics2D to draw an Image with

5条回答
  •  猫巷女王i
    2020-12-08 11:02

    From http://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);
    

提交回复
热议问题