/** * 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); } } }
@Override public void paintComponent(Graphics g) { g.setColor(getBackground()); g.fillRect(0, 0, getWidth(), getHeight()); int w = bufferedImage.getWidth(this); int h = bufferedImage.getHeight(this); if (mode == Flip.NONE) { g.drawImage(bufferedImage, 0, 0, w, h, this); } else if (mode == Flip.VERTICAL) { AffineTransform at = AffineTransform.getScaleInstance(1d, -1d); at.translate(0, -h); Graphics2D g2 = (Graphics2D) g.create(); g2.drawImage(bufferedImage, at, this); g2.dispose(); } else if (mode == Flip.HORIZONTAL) { AffineTransform at = AffineTransform.getScaleInstance(-1d, 1d); at.translate(-w, 0); AffineTransformOp atOp = new AffineTransformOp(at, null); g.drawImage(atOp.filter(bufferedImage, null), 0, 0, w, h, this); } }
/** Flip the given BufferedImage vertically. Return the new flipped BufferedImage. */ public static BufferedImage flipY(BufferedImage bsrc) { AffineTransform tx = AffineTransform.getScaleInstance(1, -1); tx.translate(0, -bsrc.getHeight(null)); AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); return op.filter(bsrc, null); }