Example usage for java.awt Graphics2D drawImage

List of usage examples for java.awt Graphics2D drawImage

Introduction

In this page you can find the example usage for java.awt Graphics2D drawImage.

Prototype

public abstract boolean drawImage(Image img, AffineTransform xform, ImageObserver obs);

Source Link

Document

Renders an image, applying a transform from image space into user space before drawing.

Usage

From source file:com.piaoyou.util.ImageUtil.java

/**
 * @todo: xem lai ham nay, neu kich thuoc nho hon max thi ta luu truc tiep
 *          inputStream xuong thumbnailFile luon
 *
 * This method create a thumbnail and reserve the ratio of the output image
 * NOTE: This method closes the inputStream after it have done its work.
 *
 * @param inputStream     the stream of a jpeg file
 * @param outputStream    the output stream
 * @param maxWidth        the maximum width of the image
 * @param maxHeight       the maximum height of the image
 * @throws IOException/*from   w w  w . ja v a2s .  c  o  m*/
 * @throws BadInputException
 */
public static void createThumbnail(InputStream inputStream, OutputStream outputStream, int maxWidth,
        int maxHeight) throws IOException {

    if (inputStream == null) {
        throw new IllegalArgumentException("inputStream must not be null.");
    }
    if (outputStream == null) {
        throw new IllegalArgumentException("outputStream must not be null.");
    }

    //boolean useSun = false;
    if (maxWidth <= 0) {
        throw new IllegalArgumentException("maxWidth must >= 0");
    }
    if (maxHeight <= 0) {
        throw new IllegalArgumentException("maxHeight must >= 0");
    }

    try {
        //JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(inputStream);
        //BufferedImage srcImage = decoder.decodeAsBufferedImage();
        byte[] srcByte = FileUtil.getBytes(inputStream);
        InputStream is = new ByteArrayInputStream(srcByte);
        //ImageIcon imageIcon = new ImageIcon(srcByte);
        //Image srcImage = imageIcon.getImage();

        BufferedImage srcImage = ImageIO.read(is);
        if (srcImage == null) {
            throw new IOException("Cannot decode image. Please check your file again.");
        }

        int imgWidth = srcImage.getWidth();
        int imgHeight = srcImage.getHeight();
        //          imgWidth or imgHeight could be -1, which is considered as an assertion
        AssertionUtil.doAssert((imgWidth > 0) && (imgHeight > 0),
                "Assertion: ImageUtil: cannot get the image size.");
        // Set the scale.
        AffineTransform tx = new AffineTransform();
        if ((imgWidth > maxWidth) || (imgHeight > maxHeight)) {
            double scaleX = (double) maxWidth / imgWidth;
            double scaleY = (double) maxHeight / imgHeight;
            double scaleRatio = (scaleX < scaleY) ? scaleX : scaleY;
            imgWidth = (int) (imgWidth * scaleX);
            imgWidth = (imgWidth == 0) ? 1 : imgWidth;
            imgHeight = (int) (imgHeight * scaleY);
            imgHeight = (imgHeight == 0) ? 1 : imgHeight;
            // scale as needed
            tx.scale(scaleRatio, scaleRatio);
        } else {// we don't need any transform here, just save it to file and return
            outputStream.write(srcByte);
            return;
        }

        // create thumbnail image
        BufferedImage bufferedImage = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);

        Graphics2D g = bufferedImage.createGraphics();
        boolean useTransform = false;
        if (useTransform) {// use transfrom to draw
            //log.trace("use transform");
            g.drawImage(srcImage, tx, null);
        } else {// use java filter
            //log.trace("use filter");
            Image scaleImage = getScaledInstance(srcImage, imgWidth, imgHeight);
            g.drawImage(scaleImage, 0, 0, null);
        }
        g.dispose();// free resource
        // write it to outputStream 
        // JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream);
        // encoder.encode(bufferedImage);

        ImageIO.write(bufferedImage, "jpeg", outputStream);
    } catch (IOException e) {
        log.error("Error", e);
        throw e;
    } finally {// this finally is very important
        try {
            inputStream.close();
        } catch (IOException e) {
            /* ignore */
            e.printStackTrace();
        }
        try {
            outputStream.close();
        } catch (IOException e) {/* ignore */
            e.printStackTrace();
        }
    }
}

From source file:ConvolveIt.java

public ConvolveIt() {
    int width = image.getWidth(this);
    int height = image.getHeight(this);
    bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D big = bufferedImage.createGraphics();
    AffineTransform affineTransform = new AffineTransform();
    big.drawImage(image, affineTransform, this);
    Kernel kernel = new Kernel(3, 3, SHARP);
    convolveOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
}

From source file:AttributesApp.java

public AttributesApp() {
    setBackground(Color.lightGray);
    setSize(500, 200);/*w w  w.ja  va2  s.  c  o  m*/

    attribString = new AttributedString(text);

    GeneralPath star = new GeneralPath();
    star.moveTo(0, 0);
    star.lineTo(10, 30);
    star.lineTo(-10, 10);
    star.lineTo(10, 10);
    star.lineTo(-10, 30);
    star.closePath();
    GraphicAttribute starShapeAttr = new ShapeGraphicAttribute(star, GraphicAttribute.TOP_ALIGNMENT, false);
    attribString.addAttribute(TextAttribute.CHAR_REPLACEMENT, starShapeAttr, 0, 1);
    attribString.addAttribute(TextAttribute.FOREGROUND, new Color(255, 255, 0), 0, 1);

    int index = text.indexOf("Java Source");
    attribString.addAttribute(TextAttribute.FOREGROUND, Color.blue, index, index + 7);
    Font font = new Font("sanserif", Font.ITALIC, 40);
    attribString.addAttribute(TextAttribute.FONT, font, index, index + 7);

    loadImage();
    BufferedImage bimage = new BufferedImage(image.getWidth(this), image.getHeight(this),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D big = bimage.createGraphics();
    big.drawImage(image, null, this);
    GraphicAttribute javaImageAttr = new ImageGraphicAttribute(bimage, GraphicAttribute.TOP_ALIGNMENT, 0, 0);

    index = text.indexOf("Java");
    attribString.addAttribute(TextAttribute.CHAR_REPLACEMENT, javaImageAttr, index - 1, index);

    font = new Font("serif", Font.BOLD, 60);
    attribString.addAttribute(TextAttribute.FONT, font, index, index + 4);
    attribString.addAttribute(TextAttribute.FOREGROUND, new Color(243, 63, 163), index, index + 4); // Start and end indexes.

    index = text.indexOf("source");
    attribString.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, index, index + 2);

    index = text.indexOf("and support");
    font = new Font("sanserif", Font.ITALIC, 40);
    attribString.addAttribute(TextAttribute.FONT, font, index, index + 10);

    attribString.addAttribute(TextAttribute.FOREGROUND, Color.white, index, index + 2); // Start and end indexes.
    attribString.addAttribute(TextAttribute.FOREGROUND, Color.blue, index + 3, index + 10); // Start and end indexes.
}

From source file:BasicDraw.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    AffineTransform tx = new AffineTransform();

    double radians = -Math.PI / 4;
    tx.rotate(radians);/*from w ww .j av  a  2 s .  com*/

    g2d.setTransform(tx);
    g2d.drawImage(new ImageIcon("a.png").getImage(), tx, this);
}

From source file:org.esa.nest.dat.layers.maptools.components.CompassComponent.java

public CompassComponent(final RasterDataNode raster) {
    image = new BufferedImage(roseIcon.getIconWidth(), roseIcon.getIconHeight(), BufferedImage.TYPE_4BYTE_ABGR);
    final Graphics2D g = image.createGraphics();
    g.drawImage(roseIcon.getImage(), null, null);

    rasterWidth = raster.getRasterWidth();
    rasterHeight = raster.getRasterHeight();
    margin = (int) (Math.min(rasterWidth, rasterHeight) * marginPct);
    point1 = new PixelPos(margin, margin);

    final GeoCoding geoCoding = raster.getGeoCoding();
    if (geoCoding == null) {
        tail = head = point1;//from   w  w w  . j a v  a 2  s  .c  o m
        angle = Double.NaN;
        return;
    }

    final GeoPos point1Geo = geoCoding.getGeoPos(point1, null);
    final GeoPos point2Geo = geoCoding.getGeoPos(new PixelPos(rasterWidth / 2, rasterHeight / 2), null);
    final PixelPos point2 = geoCoding.getPixelPos(new GeoPos(point2Geo.getLat(), point1Geo.getLon()), null);

    final double op = point1.x - point2.x;
    final double hyp = FastMath.hypot(op, point1.y - point2.y);
    angle = FastMath.asin(op / hyp);

    if (point1Geo.getLat() < point2Geo.getLat()) {
        tail = point1;
        head = point2;
        angle += Math.PI;
    } else {
        tail = point2;
        head = point1;
    }
}

From source file:org.esa.s1tbx.dat.layers.maptools.components.CompassComponent.java

public CompassComponent(final RasterDataNode raster) {
    image = new BufferedImage(roseIcon.getIconWidth(), roseIcon.getIconHeight(), BufferedImage.TYPE_4BYTE_ABGR);
    final Graphics2D g = image.createGraphics();
    g.drawImage(roseIcon.getImage(), null, null);

    rasterWidth = raster.getRasterWidth();
    rasterHeight = raster.getRasterHeight();
    margin = (int) (Math.min(rasterWidth, rasterHeight) * marginPct);
    point1 = new PixelPos(margin, margin);

    final GeoCoding geoCoding = raster.getGeoCoding();
    if (geoCoding == null) {
        tail = head = point1;/*from  w  w  w  .j  ava2 s.c  om*/
        angle = Double.NaN;
        return;
    }

    final GeoPos point1Geo = geoCoding.getGeoPos(point1, null);
    final GeoPos point2Geo = geoCoding.getGeoPos(new PixelPos(rasterWidth / 2, rasterHeight / 2), null);
    final PixelPos point2 = geoCoding.getPixelPos(new GeoPos(point2Geo.getLat(), point1Geo.getLon()), null);

    final double op = point1.x - point2.x;
    final double hyp = FastMath.hypot(op, point1.y - point2.y);
    angle = FastMath.asin(op / hyp);

    if (point1Geo.getLat() < point2Geo.getLat()) {
        tail = point1;
        head = point2;
        angle += Constants.PI;
    } else {
        tail = point2;
        head = point1;
    }
}

From source file:BasicDraw.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    AffineTransform tx = new AffineTransform();

    double x = 50;
    double y = 50;
    tx.translate(x, y);//from   w ww  .j av a 2 s.c o m

    g2d.setTransform(tx);
    g2d.drawImage(new ImageIcon("a.png").getImage(), tx, this);
}

From source file:Java2DExample.java

public void displayOriginalImage() {
    displayImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);

    Graphics2D graphics = displayImage.createGraphics();
    graphics.drawImage(originalImage, null, null);
    repaint();/*  w  ww  . j a v a2s. c  o m*/
}

From source file:BasicDraw.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    AffineTransform tx = new AffineTransform();

    double shiftx = .1;
    double shifty = .3;
    tx.shear(shiftx, shifty);//from   ww  w .j  a  v  a  2 s.c o m

    g2d.setTransform(tx);
    g2d.drawImage(new ImageIcon("a.png").getImage(), tx, this);
}

From source file:BasicDraw.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    AffineTransform tx = new AffineTransform();

    double scalex = .5;
    double scaley = 1;
    tx.scale(scalex, scaley);/*ww w  .j  a  v  a 2  s.  c  o m*/

    g2d.setTransform(tx);

    g2d.drawImage(new ImageIcon("a.png").getImage(), tx, this);
}