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:Main.java

/**
 * Resizes an image using trilinear filtering.
 * @param source   the source image.//w  w w  .  ja  v a2s.c  o  m
 * @param newWidth   the new image width.
 * @param newHeight   the new image height.
 */
public static BufferedImage performResizeTrilinear(BufferedImage source, int newWidth, int newHeight) {
    BufferedImage out = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = out.createGraphics();
    g2d.setComposite(AlphaComposite.Src);
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.drawImage(source, 0, 0, newWidth, newHeight, null);
    g2d.dispose();
    return out;
}

From source file:Main.java

/**
 * Resizes an image using bilinear filtering.
 * @param source   the source image.// ww  w.  j a  v a2 s  . c o m
 * @param newWidth   the new image width.
 * @param newHeight   the new image height.
 */
public static BufferedImage performResizeBilinear(BufferedImage source, int newWidth, int newHeight) {
    BufferedImage out = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = out.createGraphics();
    g2d.setComposite(AlphaComposite.Src);
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.drawImage(source, 0, 0, newWidth, newHeight, null);
    g2d.dispose();
    return out;
}

From source file:com.kabone.research.common.utils.FileUtil.java

public static boolean thumbnail(int imageWidth, int imageHeight, File originFileName, File thumbFileName) {
    boolean result = false;
    try {//from  ww w . java  2  s .  c  o  m
        BufferedImage bufferOriginImg = ImageIO.read(originFileName);
        BufferedImage bufferThumbImg = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D graphic = bufferThumbImg.createGraphics();
        graphic.drawImage(bufferOriginImg, 0, 0, imageWidth, imageHeight, null);
        ImageIO.write(bufferThumbImg, "jpg", thumbFileName);
        result = true;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}

From source file:GUI.Main.java

public static BufferedImage resize(BufferedImage image, int width, int height) {
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TRANSLUCENT);
    Graphics2D g2d = bi.createGraphics();
    g2d.addRenderingHints(// w w w .  j  a v a2  s  .  c o m
            new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
    g2d.drawImage(image, 0, 0, width, height, null);
    g2d.dispose();
    return bi;
}

From source file:com.t3.image.ImageUtil.java

/**
 * Flip the image and return a new image
 * @param direction 0-nothing, 1-horizontal, 2-vertical, 3-both
 * @return//from  w w w .j  a  v  a2s  .co m
 */
public static BufferedImage flip(BufferedImage image, int direction) {
    BufferedImage workImage = new BufferedImage(image.getWidth(), image.getHeight(), image.getTransparency());

    boolean flipHorizontal = (direction & 1) == 1;
    boolean flipVertical = (direction & 2) == 2;

    int workW = image.getWidth() * (flipHorizontal ? -1 : 1);
    int workH = image.getHeight() * (flipVertical ? -1 : 1);
    int workX = flipHorizontal ? image.getWidth() : 0;
    int workY = flipVertical ? image.getHeight() : 0;

    Graphics2D wig = workImage.createGraphics();
    wig.drawImage(image, workX, workY, workW, workH, null);
    wig.dispose();

    return workImage;
}

From source file:imageLines.ImageHelpers.java

public static BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint,
        boolean higherQuality) {
    int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage ret = (BufferedImage) img;
    int w, h;//from w ww. j ava 2s  .c o  m
    if (higherQuality) {
        // Use multi-step technique: start with original size, then
        // scale down in multiple passes with drawImage()
        // until the target size is reached
        w = img.getWidth();
        h = img.getHeight();
    } else {
        // Use one-step technique: scale directly from original
        // size to target size with a single drawImage() call
        w = targetWidth;
        h = targetHeight;
    }

    do {
        if (higherQuality && w > targetWidth) {
            w /= 2;
            if (w < targetWidth) {
                w = targetWidth;
            }
        }

        if (higherQuality && h > targetHeight) {
            h /= 2;
            if (h < targetHeight) {
                h = targetHeight;
            }
        }

        BufferedImage tmp = new BufferedImage(w, h, type);
        Graphics2D g2 = tmp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
        g2.drawImage(ret, 0, 0, w, h, null);
        g2.dispose();

        ret = tmp;
    } while (w != targetWidth || h != targetHeight);

    return ret;
}

From source file:com.t3.macro.api.functions.input.InputFunctions.java

/**
 * Gets icon from the asset manager. Code copied and modified from
 * EditTokestaticnDialog.java/*from ww  w.j a  v a 2  s  .  c  o  m*/
 */
static ImageIcon getIcon(String id, int size, ImageObserver io) {
    // Extract the MD5Key from the URL
    if (id == null)
        return null;
    MD5Key assetID = new MD5Key(id);

    // Get the base image && find the new size for the icon
    BufferedImage assetImage = ImageManager.getImage(assetID, io);

    // Resize
    if (assetImage.getWidth() > size || assetImage.getHeight() > size) {
        Dimension dim = new Dimension(assetImage.getWidth(), assetImage.getWidth());
        if (dim.height < dim.width) {
            dim.height = (int) ((dim.height / (double) dim.width) * size);
            dim.width = size;
        } else {
            dim.width = (int) ((dim.width / (double) dim.height) * size);
            dim.height = size;
        }
        BufferedImage image = new BufferedImage(dim.width, dim.height, Transparency.BITMASK);
        Graphics2D g = image.createGraphics();
        g.drawImage(assetImage, 0, 0, dim.width, dim.height, null);
        assetImage = image;
    }
    return new ImageIcon(assetImage);
}

From source file:com.reydentx.core.common.PhotoUtils.java

public static byte[] scaleBufferedImage(boolean isPNG, BufferedImage originalImage, int x, int y, int width,
        int height) {
    byte[] imageFinal = null;
    try {/*from  w ww  . j  av a  2s.  c  om*/
        if (originalImage != null) {
            int type = (originalImage.getType() == 0) ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
            BufferedImage resizedImage = new BufferedImage(width, height, type);
            Graphics2D g = resizedImage.createGraphics();
            ByteArrayOutputStream outstream = new ByteArrayOutputStream();
            g.drawImage(originalImage, x, y, width, height, null);
            g.dispose();

            if (isPNG) {
                ImageIO.write(resizedImage, "png", outstream);
            } else {
                ImageIO.write(resizedImage, "jpg", outstream);
            }
            imageFinal = outstream.toByteArray();
        }
    } catch (IOException ex) {
    }
    return imageFinal;
}

From source file:it.reexon.lib.files.FileUtils.java

/**
 * Convenience method that returns a scaled instance of the provided {@code BufferedImage}. 
 *  //from ww  w  . j a va  2  s.  co  m
 * @param img the original image to be scaled 
 * @param targetWidth the desired width of the scaled instance, in pixels 
 * @param targetHeight the desired height of the scaled instance, in pixels 
 * @param hint one of the rendering hints that corresponds to {@code RenderingHints.KEY_INTERPOLATION} (e.g. 
 *        {@code RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR}, {@code RenderingHints.VALUE_INTERPOLATION_BILINEAR}, 
 *        {@code RenderingHints.VALUE_INTERPOLATION_BICUBIC}) 
 * @param higherQuality if true, this method will use a multi-step scaling technique that provides higher quality than the 
 *        usual one-step technique (only useful in downscaling cases, where {@code targetWidth} or {@code targetHeight} is 
 *        smaller than the original dimensions, and generally only when the {@code BILINEAR} hint is specified) 
 * @return a scaled version of the original {@code BufferedImage} 
 */
public static BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint,
        boolean higherQuality) {
    final int type = img.getTransparency() == Transparency.OPAQUE ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage ret = img;
    int w;
    int h;
    if (higherQuality) {
        // Use multi-step technique: start with original size, then 
        // scale down in multiple passes with drawImage() 
        // until the target size is reached 
        w = img.getWidth();
        h = img.getHeight();
    } else {
        // Use one-step technique: scale directly from original 
        // size to target size with a single drawImage() call 
        w = targetWidth;
        h = targetHeight;
    }

    do {
        if (higherQuality && w > targetWidth) {
            w /= 2;
            if (w < targetWidth) {
                w = targetWidth;
            }
        }

        if (higherQuality && h > targetHeight) {
            h /= 2;
            if (h < targetHeight) {
                h = targetHeight;
            }
        }

        final BufferedImage tmp = new BufferedImage(w, h, type);
        final Graphics2D g2 = tmp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
        g2.drawImage(ret, 0, 0, w, h, null);
        g2.dispose();

        ret = tmp;
    } while (w != targetWidth || h != targetHeight);

    return ret;
}

From source file:juicebox.tools.utils.juicer.apa.APAPlotter.java

/**
 * Method for plotting apa data/*w ww  . j a  va2  s.co  m*/
 *
 * @param data       for heat map
 * @param axesRange  initial values and increments to annotate axes [x0, dx, y0, dy]
 * @param outputFile where image will saved
 */
public static void plot(RealMatrix data, int[] axesRange, File outputFile, String title) {

    APARegionStatistics apaStats = new APARegionStatistics(data);
    DecimalFormat df = new DecimalFormat("0.000");
    title += ", P2LL = " + df.format(apaStats.getPeak2LL());

    // initialize heat map
    HeatChart map = new HeatChart(data.getData());
    map.setLowValueColour(Color.WHITE);
    map.setHighValueColour(Color.RED);
    map.setXValues(axesRange[0], axesRange[1]);
    map.setYValues(axesRange[2], axesRange[3]);
    map.setTitle(title);

    try {
        // calculate dimensions for plot wrapper
        initializeSizes(map);

        // create blank white image
        BufferedImage apaImage = new BufferedImage(fullWidth, fullHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = apaImage.createGraphics();
        g2.setBackground(Color.WHITE);
        g2.fillRect(0, 0, fullWidth, fullHeight);

        // plot in heat map, color bar, etc
        g2.drawImage(map.getChartImage(), 0, 0, heatmapWidth, fullHeight, null);
        drawHeatMapBorder(g2, map);
        plotColorScaleBar(g2);
        plotColorScaleValues(g2, map);

        // top left, top right, bottom left, bottom right values (from apa)

        drawCornerRegions(g2, map, new Dimension(APA.regionWidth, APA.regionWidth),
                apaStats.getRegionCornerValues());

        // save data
        ImageIO.write(apaImage, "png", outputFile);

    } catch (IOException e) {
        e.printStackTrace();
    }
}