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, int x, int y, int width, int height, ImageObserver observer);

Source Link

Document

Draws as much of the specified image as has already been scaled to fit inside the specified rectangle.

Usage

From source file:nz.govt.natlib.ndha.manualdeposit.dialogs.About.java

private void paintImage() {
    if (theLogo != null) {
        int width = this.getWidth();
        int height = this.getHeight();
        BufferedImage thumbImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = thumbImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(theLogo, 0, 0, width, height, null);
        imgIndigo.setImage(thumbImage);/*  w w w .j  a  va2 s  .  c  om*/
        imgIndigo.setBounds(0, 0, width, height);
        repaint();
    }
}

From source file:de.inren.service.picture.PictureModificationServiceImpl.java

private BufferedImage scaleImage(File orginal, final int max) throws IOException {
    // Load the image.
    BufferedImage originalImage = ImageIO.read(orginal);

    // Figure out the new dimensions.
    final int w = originalImage.getWidth();
    final int h = originalImage.getHeight();
    final double maxOriginal = Math.max(w, h);
    final double scaling = max / maxOriginal;

    final int newW = (int) Math.round(scaling * w);
    final int newH = (int) Math.round(scaling * h);

    // If we need to scale down by more than 2x, scale to double the
    // eventual size and then scale again. This provides much higher
    // quality results.
    if (scaling < 0.5f) {
        final BufferedImage newImg = new BufferedImage(newW * 2, newH * 2, BufferedImage.TYPE_INT_RGB);
        final Graphics2D gfx = newImg.createGraphics();
        gfx.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        gfx.drawImage(originalImage, 0, 0, newW * 2, newH * 2, null);
        gfx.dispose();/*from   w  ww  .j ava 2  s . com*/
        newImg.flush();
        originalImage = newImg;
    }

    // Scale it.
    BufferedImage newImg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_RGB);
    final Graphics2D gfx = newImg.createGraphics();
    gfx.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    gfx.drawImage(originalImage, 0, 0, newW, newH, null);
    gfx.dispose();
    newImg.flush();
    originalImage.flush();

    return newImg;
}

From source file:org.deegree.test.gui.StressTestController.java

private void renderResizedImage(HttpServletResponse response, String shot, int width, int height)
        throws IOException {
    BufferedImage originalImage = ImageIO.read(new File(shot));

    BufferedImage scaledImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = scaledImage.createGraphics();
    graphics.setComposite(AlphaComposite.Src);
    graphics.drawImage(originalImage, 0, 0, width, height, null);
    graphics.dispose();//from   ww w.jav  a  2s  . c  o m

    response.setContentType("image/jpeg");
    OutputStream out = response.getOutputStream();
    ImageIO.write(scaledImage, "jpg", out);
    out.close();

}

From source file:com.igormaznitsa.mindmap.swing.panel.utils.ScalableIcon.java

public synchronized Image getImage(final double scale) {
    if (Double.compare(this.currentScaleFactor, scale) != 0) {
        this.scaledCachedImage = null;
    }/*  w w w  .j  av  a  2  s . co  m*/

    if (this.scaledCachedImage == null) {
        this.currentScaleFactor = scale;

        final int imgw = this.baseImage.getWidth(null);
        final int imgh = this.baseImage.getHeight(null);
        final int scaledW = (int) Math.round(imgw * this.baseScaleX * scale);
        final int scaledH = (int) Math.round(imgh * this.baseScaleY * scale);

        final BufferedImage img = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_INT_ARGB);
        final Graphics2D g = (Graphics2D) img.getGraphics();

        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
                RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);

        g.drawImage(this.baseImage, 0, 0, scaledW, scaledH, null);
        g.dispose();

        this.scaledCachedImage = img;
    }
    return this.scaledCachedImage;
}

From source file:RenderQualityTest.java

public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHints(hints);//  w w w.j av  a  2 s .c o m

    g2.draw(new Ellipse2D.Double(10, 10, 60, 50));
    g2.setFont(new Font("Serif", Font.ITALIC, 40));
    g2.drawString("Hello", 75, 50);

    g2.draw(new Rectangle2D.Double(200, 10, 40, 40));
    g2.draw(new Line2D.Double(201, 11, 239, 49));

    g2.drawImage(image, 250, 10, 100, 100, null);
}

From source file:de.tuttas.restful.SchuelerManager.java

private BufferedImage createResizedCopy(Image originalImage, int scaledWidth, int scaledHeight,
        boolean preserveAlpha) {
    int imageType = preserveAlpha ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
    BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, imageType);
    Graphics2D g = scaledBI.createGraphics();
    if (preserveAlpha) {
        g.setComposite(AlphaComposite.Src);
    }// w  w w .j  a  va  2s.co m
    g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null);
    g.dispose();
    return scaledBI;
}

From source file:de.dakror.villagedefense.game.entity.Entity.java

public void drawEntity(Graphics2D g) {
    if (alpha == 0)
        return;// ww  w.ja v a 2s  .  co  m

    drawBump(g, false);
    Composite c = g.getComposite();
    if (this instanceof Struct)
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
    draw(g);
    g.setComposite(c);

    if (isHungry()) {
        g.drawImage(Game.getImage("icon/hunger.png"), (int) x,
                (int) (y - Tile.SIZE - Math.cos(tick / 10f) * Tile.SIZE / 4), 32, 32, Game.w);
    } else if (this instanceof Struct && !((Struct) this).isWorking()) {
        g.drawImage(Game.getImage("icon/sleep.png"), (int) (x + width * 0.75f),
                (int) (y - Tile.SIZE - Math.cos(tick / 10f) * Tile.SIZE / 4), 32, 32, Game.w);
    }

    drawBump(g, true);
}

From source file:com.salesmanager.core.util.ProductImageUtil.java

public BufferedImage resize(BufferedImage image, int width, int height) {
    int type = image.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : image.getType();
    BufferedImage resizedImage = new BufferedImage(width, height, type);
    Graphics2D g = resizedImage.createGraphics();
    g.setComposite(AlphaComposite.Src);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.drawImage(image, 0, 0, width, height, null);
    g.dispose();/*  w w w.  j av a 2  s . c  o  m*/
    return resizedImage;
}

From source file:dcstore.web.ImagesWeb.java

private void resizeImage(String inPath, int w, int h, String outPath) {
    try {//from  ww w.  ja v  a2s  .c  o m
        BufferedImage originalImage = ImageIO.read(new File(inPath));
        int ow, oh;
        ow = originalImage.getWidth();
        oh = originalImage.getHeight();
        double ratio = (double) ow / (double) oh;
        int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
        int ch = (int) Math.round(w / ratio);

        BufferedImage resizedImage = new BufferedImage(w, h, type);
        Graphics2D g = resizedImage.createGraphics();
        g.setComposite(AlphaComposite.Src);
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(Color.white);
        g.fillRect(0, 0, w, h);
        g.drawImage(originalImage, 0, (int) (((float) h - (float) ch) / 2), w, ch, null);
        g.dispose();
        ImageIO.write(resizedImage, "jpg", new File(outPath));
    } catch (Exception e) {
        FacesContext.getCurrentInstance().addMessage("",
                new FacesMessage("Error while resizeing image: " + e.getMessage()));
    }
}

From source file:org.bigbluebuttonproject.fileupload.document.impl.FileSystemSlideManager.java

/**
 * This method create thumbImage of the image file given and save it in outFile.
 * Compression quality is also given. thumbBounds is used for calculating the size of the thumb.
 * // ww w . j  a  va  2 s  .  c  o  m
 * @param infile slide image to create thumb
 * @param outfile output thumb file
 * @param compressionQuality the compression quality
 * @param thumbBounds the thumb bounds
 * 
 * @throws IOException Signals that an I/O exception has occurred.
 */
public void resizeImage(File infile, File outfile, float compressionQuality, int thumbBounds)
        throws IOException {
    // Retrieve jpg image to be resized
    Image image = ImageIO.read(infile);

    // get original image size for thumb size calculation
    int imageWidth = image.getWidth(null);
    int imageHeight = image.getHeight(null);

    float thumbRatio = (float) thumbBounds / Math.max(imageWidth, imageHeight);

    int thumbWidth = (int) (imageWidth * thumbRatio);
    int thumbHeight = (int) (imageHeight * thumbRatio);

    // draw original image to thumbnail image object and
    // scale it to the new size on-the-fly
    BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics2D = thumbImage.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);

    // Find a jpeg writer
    ImageWriter writer = null;
    Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpg");
    if (iter.hasNext()) {
        writer = (ImageWriter) iter.next();
    }

    ImageWriteParam iwp = writer.getDefaultWriteParam();
    iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    iwp.setCompressionQuality(compressionQuality);

    // Prepare output file
    ImageOutputStream ios = ImageIO.createImageOutputStream(outfile);
    writer.setOutput(ios);
    // write to the thumb image 
    writer.write(thumbImage);

    // Cleanup
    ios.flush();
    writer.dispose();
    ios.close();
}