Example usage for java.awt Graphics2D drawRenderedImage

List of usage examples for java.awt Graphics2D drawRenderedImage

Introduction

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

Prototype

public abstract void drawRenderedImage(RenderedImage img, AffineTransform xform);

Source Link

Document

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

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    BufferedImage bsrc = ImageIO.read(new File("a.jpg"));
    BufferedImage bdest = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bdest.createGraphics();
    AffineTransform at = AffineTransform.getScaleInstance(2, 2);
    g.drawRenderedImage(bsrc, at);
    ImageIO.write(bdest, "JPG", new File("b.jpg"));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    BufferedImage bufferedImage = ImageIO.read(new File("a.jpg"));
    BufferedImage destinationBufferedImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = destinationBufferedImage.createGraphics();
    AffineTransform at = AffineTransform.getScaleInstance(2, 2);
    g.drawRenderedImage(bufferedImage, at);
    ImageIO.write(destinationBufferedImage, "JPG", new File("b.jpg"));
}

From source file:Main.java

public static BufferedImage create(BufferedImage image, double angle, GraphicsConfiguration gc) {
    double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
    int w = image.getWidth(), h = image.getHeight();
    int neww = (int) Math.floor(w * cos + h * sin), newh = (int) Math.floor(h * cos + w * sin);
    int transparency = image.getColorModel().getTransparency();
    BufferedImage result = gc.createCompatibleImage(neww, newh, transparency);
    Graphics2D g = result.createGraphics();
    g.translate((neww - w) / 2, (newh - h) / 2);
    g.rotate(angle, w / 2, h / 2);//ww w  .j a  v a2  s . c o  m
    g.drawRenderedImage(image, null);
    return result;
}

From source file:ImageUtil.java

public static BufferedImage scale(BufferedImage src, int width, int height) throws IOException {
    BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = dest.createGraphics();
    AffineTransform at = AffineTransform.getScaleInstance((double) width / src.getWidth(),
            (double) height / src.getHeight());
    g.drawRenderedImage(src, at);
    return dest;//from  w w w. jav a 2 s.c  o m
}

From source file:com.ables.pix.utility.PictureOps.java

public static BufferedImage tilt(BufferedImage image, double rotation) {
    double sin = Math.abs(Math.sin(getAngle()));
    double cos = Math.abs(Math.cos(getAngle()));
    int w = image.getWidth(), h = image.getHeight();

    int neww = (int) Math.floor(w * cos + sin * h), newh = (int) Math.floor(h * cos + sin * w);
    GraphicsConfiguration gc = getDefaultConfiguration();
    BufferedImage rotated = gc.createCompatibleImage(neww, newh);
    Graphics2D g = rotated.createGraphics();
    g.translate((neww - w) / 2, (newh - h / 2));
    g.rotate(getAngle(), w / 2, h / 2);/*  w  ww .  j  a  v a  2s .co  m*/
    g.drawRenderedImage(image, null);
    g.dispose();
    return rotated;
}

From source file:de.bamamoto.mactools.png2icns.Scaler.java

public static BufferedImage resize(BufferedImage source, int width, int height) {

    double xScale = ((double) width) / (double) source.getWidth();
    double yScale = ((double) height) / (double) source.getHeight();
    BufferedImage result = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
            .getDefaultConfiguration()/*from w w w .jav a  2s  .  c om*/
            .createCompatibleImage(width, height, source.getColorModel().getTransparency());
    Graphics2D newImage = null;
    try {
        newImage = result.createGraphics();
        newImage.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        newImage.drawRenderedImage(source, AffineTransform.getScaleInstance(xScale, yScale));
    } finally {
        if (newImage != null) {
            newImage.dispose();
        }
    }
    return result;
}

From source file:imageLines.ImageHelpers.java

/**Scale an image to the specified width and height*/
public static BufferedImage scale(BufferedImage img, int height, int width) {

    BufferedImage bdest = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bdest.createGraphics();
    AffineTransform at = AffineTransform.getScaleInstance((double) width / img.getWidth(),
            (double) height / img.getHeight());
    g.drawRenderedImage(img, at);
    return bdest;

}

From source file:com.fengduo.bee.service.impl.file.FileServiceImpl.java

/**
 * ?//from www  . j av  a2  s .co  m
 * 
 * @param source
 * @param targetW
 * @param targetH
 * @param ifScaling ?
 * @return
 */
public static BufferedImage resize(BufferedImage source, int targetW, int targetH, boolean ifScaling) {
    // targetWtargetH
    int type = source.getType();
    BufferedImage target = null;
    double sx = (double) targetW / source.getWidth();
    double sy = (double) targetH / source.getHeight();
    // targetWtargetH??,?if else???
    if (ifScaling) {
        if (sx > sy) {
            sx = sy;
            targetW = (int) (sx * source.getWidth());
        } else {
            sy = sx;
            targetH = (int) (sy * source.getHeight());
        }
    }
    if (type == BufferedImage.TYPE_CUSTOM) { // handmade
        ColorModel cm = source.getColorModel();
        WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
        boolean alphaPremultiplied = cm.isAlphaPremultiplied();
        target = new BufferedImage(cm, raster, alphaPremultiplied, null);
    } else
        target = new BufferedImage(targetW, targetH, type);
    Graphics2D g = target.createGraphics();
    // smoother than exlax:
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
    g.dispose();
    return target;
}

From source file:de.darkblue.bongloader2.utils.ToolBox.java

public static BufferedImage resizeImage(BufferedImage image, int width, int height) {
    BufferedImage returnValue = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2D = returnValue.createGraphics();
    g2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    AffineTransform at = AffineTransform.getScaleInstance((double) width / image.getWidth(),
            (double) height / image.getHeight());
    g2D.drawRenderedImage(image, at);
    return returnValue;
}

From source file:TestImage2.java

public void paintComponent(Graphics g) {
    // For background
    super.paintComponent(g);

    // Meant for visual check
    if (image != null) {

        Graphics2D g2 = (Graphics2D) g;

        g2.drawRenderedImage(image, scaleXform);
        //g2.drawImage (image, 1, 1, this);
    }//  w ww. j a  va  2 s . c  om
}