Ejemplo n.º 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);
     }
   }
 }
Ejemplo n.º 3
0
 @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);
   }
 }
Ejemplo n.º 4
0
  protected void doDrawOnTo(BufferedImageRaster canvas) {
    Sector sector = this.getSector();
    if (null == sector) {
      String message = Logging.getMessage("nullValue.SectorIsNull");
      Logging.logger().severe(message);
      throw new IllegalArgumentException(message);
    }

    if (!sector.intersects(canvas.getSector())) {
      return;
    }

    java.awt.Graphics2D g2d = null;
    java.awt.Shape prevClip = null;
    java.awt.Composite prevComposite = null;
    java.lang.Object prevInterpolation = null, prevAntialiasing = null;

    try {
      int canvasWidth = canvas.getWidth();
      int canvasHeight = canvas.getHeight();

      // Apply the transform that correctly maps the image onto the canvas.
      java.awt.geom.AffineTransform transform =
          this.computeSourceToDestTransform(
              this.getWidth(),
              this.getHeight(),
              this.getSector(),
              canvasWidth,
              canvasHeight,
              canvas.getSector());

      AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
      Rectangle2D rect = op.getBounds2D(this.getBufferedImage());

      int clipWidth =
          (int) Math.ceil((rect.getMaxX() >= canvasWidth) ? canvasWidth : rect.getMaxX());
      int clipHeight =
          (int) Math.ceil((rect.getMaxY() >= canvasHeight) ? canvasHeight : rect.getMaxY());

      if (clipWidth <= 0 || clipHeight <= 0) {
        return;
      }

      g2d = canvas.getGraphics();

      prevClip = g2d.getClip();
      prevComposite = g2d.getComposite();
      prevInterpolation = g2d.getRenderingHint(RenderingHints.KEY_INTERPOLATION);
      prevAntialiasing = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);

      // Set the alpha composite for appropriate alpha blending.
      g2d.setComposite(java.awt.AlphaComposite.SrcOver);
      g2d.setRenderingHint(
          RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

      g2d.drawImage(this.getBufferedImage(), transform, null);
    }
    //        catch (java.awt.image.ImagingOpException ioe)
    //        {
    //            // If we catch a ImagingOpException, then the transformed image has a width or
    // height of 0.
    //            // This indicates that there is no intersection between the source image and the
    // canvas,
    //            // or the intersection is smaller than one pixel.
    //        }
    //        catch (java.awt.image.RasterFormatException rfe)
    //        {
    //            // If we catch a RasterFormatException, then the transformed image has a width or
    // height of 0.
    //            // This indicates that there is no intersection between the source image and the
    // canvas,
    //            // or the intersection is smaller than one pixel.
    //        }
    catch (Throwable t) {
      String reason = WWUtil.extractExceptionReason(t);
      Logging.logger().log(java.util.logging.Level.SEVERE, reason, t);
    } finally {
      // Restore the previous clip, composite, and transform.
      try {
        if (null != g2d) {
          if (null != prevClip) g2d.setClip(prevClip);

          if (null != prevComposite) g2d.setComposite(prevComposite);

          if (null != prevInterpolation)
            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, prevInterpolation);

          if (null != prevAntialiasing)
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, prevAntialiasing);
        }
      } catch (Throwable t) {
        Logging.logger().log(java.util.logging.Level.FINEST, WWUtil.extractExceptionReason(t), t);
      }
    }
  }
Ejemplo n.º 5
0
 /** 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);
 }