Example #1
0
 /**
  * Simple image flipper. Can flip image either vertically, horizontally or both directions
  *
  * @param source _
  * @param flipType _
  * @return BufferedImage
  * @throws NullPointerException _
  * @throws IllegalArgumentException _
  */
 public static <T> BufferedImage flipImage(final T source, final FlipType flipType)
     throws NullPointerException, IllegalArgumentException, IOException {
   if (verifyNotNull(source, flipType)) {
     BufferedImage sourceImage = convertToBufferedImage(source);
     BufferedImage target =
         new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), sourceImage.getType());
     AffineTransform affineTransform;
     AffineTransformOp affineTransformOp;
     if (flipType.equals(FlipType.HORIZONTAL) || flipType.equals(FlipType.BOTH)) {
       affineTransform = AffineTransform.getScaleInstance(1, -1);
       affineTransform.translate(-sourceImage.getWidth(), 0);
       affineTransformOp =
           new AffineTransformOp(affineTransform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
       target = affineTransformOp.filter(sourceImage, target);
     }
     if (flipType.equals(FlipType.VERTICAL) || flipType.equals(FlipType.BOTH)) {
       affineTransform = AffineTransform.getScaleInstance(1, -1);
       affineTransformOp =
           new AffineTransformOp(affineTransform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
       affineTransform.translate(0, -sourceImage.getHeight());
       target = affineTransformOp.filter(sourceImage, target);
     }
     return target;
   }
   throw new NullPointerException(E_OBJECT_WAS_NULL);
 }
 protected void makeSheet() {
   if (false) { // TODO deal with sheets
     // sprite sheet is East 0, clockwise
     // direction sheet is North 0, clockwise
     /*int sheetIndex = (dir.ordinal() - Direction.EAST.ordinal() + 8) % 8;
        int soldierHeight = image.getHeight();
        if (!isAttacking()) {
     sheetIndex += 8;
        }
        image = image.getSubimage(sheetIndex * soldierHeight, 0,
     		      soldierHeight, soldierHeight);
        */
   } else {
     sprites = new BufferedImage[8];
     sprites[0] = image;
     for (int i = 1; i < 8; i++) {
       sprites[i] =
           new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
       double rotationRequired = Math.toRadians(i * 45);
       double locationX = image.getWidth() / 2;
       double locationY = image.getHeight() / 2;
       AffineTransform tx =
           AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
       AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
       // Drawing the rotated image at the required drawing locations
       sprites[i].createGraphics().drawImage(op.filter(image, null), 0, 0, null);
     }
   }
 }