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:fr.msch.wissl.server.Library.java

public static String resizeArtwork(String artPath) throws IOException {
    BufferedImage orig = ImageIO.read(new File(artPath));

    if (orig == null) {
        throw new IOException("Failed to open image");
    }/*from   w  ww  .  j  a va  2s.c o  m*/
    Image sc = orig.getScaledInstance(70, 70, Image.SCALE_SMOOTH);

    BufferedImage scaled = new BufferedImage(70, 70, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = scaled.createGraphics();
    g.drawImage(sc, 0, 0, 70, 70, null);
    g.dispose();

    File ret = new File(artPath + "_SCALED.jpg");
    ImageIO.write(scaled, "JPG", ret);

    return ret.getAbsolutePath();
}

From source file:com.t3.persistence.PersistenceUtil.java

static public void saveCampaignThumbnail(String fileName) {
    BufferedImage screen = TabletopTool.takeMapScreenShot(new PlayerView(TabletopTool.getPlayer().getRole()));
    if (screen == null)
        return;// ww w  .j  av a  2s .  com

    Dimension imgSize = new Dimension(screen.getWidth(null), screen.getHeight(null));
    SwingUtil.constrainTo(imgSize, 200, 200);

    BufferedImage thumb = new BufferedImage(imgSize.width, imgSize.height, BufferedImage.TYPE_INT_BGR);
    Graphics2D g2d = thumb.createGraphics();
    g2d.drawImage(screen, 0, 0, imgSize.width, imgSize.height, null);
    g2d.dispose();

    File thumbFile = getCampaignThumbnailFile(fileName);

    try {
        ImageIO.write(thumb, "jpg", thumbFile);
    } catch (IOException ioe) {
        TabletopTool.showError("msg.error.failedSaveCampaignPreview", ioe);
    }
}

From source file:at.tugraz.sss.serv.SSFileU.java

public static void scalePNGAndWrite(final BufferedImage buffImage, final String pngFilePath,
        final Integer width, final Integer height) throws IOException {

    final BufferedImage scaledThumb = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    final Graphics2D graphics2D = scaledThumb.createGraphics();

    graphics2D.setComposite(AlphaComposite.Src);
    graphics2D.drawImage(buffImage, 0, 0, width, height, null);
    graphics2D.dispose();//from   ww w . j av  a 2 s.c o m

    ImageIO.write(scaledThumb, SSFileExtE.png.toString(), new File(pngFilePath));
}

From source file:com.sun.socialsite.util.ImageUtil.java

public static BufferedImage getScaledImage(BufferedImage origImage, Integer desiredWidth, Integer desiredHeight)
        throws IOException {

    if (origImage == null) {
        return null;
    }//from   w ww.  j  a v a 2s.  c  om

    if (desiredWidth == null) {
        desiredWidth = origImage.getWidth();
    }

    if (desiredHeight == null) {
        desiredHeight = origImage.getHeight();
    }

    int origWidth = origImage.getWidth();
    int origHeight = origImage.getHeight();

    double ratio = Math.min((((double) desiredWidth) / origWidth), (((double) desiredHeight) / origHeight));

    int extraWidth = desiredWidth - ((int) (origWidth * ratio));
    int extraHeight = desiredHeight - ((int) (origHeight * ratio));

    int tmpWidth = (desiredWidth - extraWidth);
    int tmpHeight = (desiredHeight - extraHeight);
    BufferedImage tmpImage = getScaledInstance(origImage, tmpWidth, tmpHeight,
            RenderingHints.VALUE_INTERPOLATION_BICUBIC, true);

    log.debug(String.format("tmpImage[width=%d height=%d", tmpImage.getWidth(), tmpImage.getHeight()));

    if ((tmpImage.getWidth() == desiredWidth) && (tmpImage.getHeight() == desiredHeight)) {
        return tmpImage;
    } else {
        BufferedImage scaledImage = new BufferedImage(desiredWidth, desiredHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = scaledImage.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);

        // We recalculate these in case scaling didn't quite hit its targets
        extraWidth = desiredWidth - tmpImage.getWidth();
        extraHeight = desiredHeight - tmpImage.getHeight();

        int dx1 = extraWidth / 2;
        int dy1 = extraHeight / 2;
        int dx2 = desiredWidth - dx1;
        int dy2 = desiredWidth - dy1;

        // transparent background
        g2d.setColor(new Color(0, 0, 0, 0));
        g2d.fillRect(0, 0, desiredWidth, desiredHeight);

        g2d.drawImage(tmpImage, dx1, dy1, dx2, dy2, null);
        return scaledImage;
    }
}

From source file:com.flexive.shared.media.impl.FxMediaNativeEngine.java

/**
 * Rotate an image using the requested angle
 *
 * @param bufferedImage imeg to rotate/*  ww w.ja  v  a2 s .co  m*/
 * @param angle         angle to rotate
 * @return BufferedImage containing the rotation
 */
@SuppressWarnings({ "UnusedAssignment" })
private static BufferedImage rotate(BufferedImage bufferedImage, int angle) {
    angle = angle % 360;
    if (angle == 0)
        return bufferedImage;
    if (angle < 0)
        angle += 360;
    switch (angle) {
    case 90:
        BufferedImageOp rot90 = new AffineTransformOp(AffineTransform.getRotateInstance(Math.PI / 2.0,
                bufferedImage.getHeight() / 2.0, bufferedImage.getHeight() / 2.0),
                AffineTransformOp.TYPE_BILINEAR);
        BufferedImage img90 = new BufferedImage(bufferedImage.getHeight(), bufferedImage.getWidth(),
                bufferedImage.getType());
        return rot90.filter(bufferedImage, img90);
    case 180:
        BufferedImageOp rot180 = new AffineTransformOp(AffineTransform.getRotateInstance(Math.PI,
                bufferedImage.getWidth() / 2.0, bufferedImage.getHeight() / 2.0),
                AffineTransformOp.TYPE_BILINEAR);
        BufferedImage img180 = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(),
                bufferedImage.getType());
        return rot180.filter(bufferedImage, img180);
    case 270:
        BufferedImageOp rot270 = new AffineTransformOp(AffineTransform.getRotateInstance(-Math.PI / 2.0,
                bufferedImage.getWidth() / 2.0, bufferedImage.getWidth() / 2.0),
                AffineTransformOp.TYPE_BILINEAR);
        BufferedImage img270 = new BufferedImage(bufferedImage.getHeight(), bufferedImage.getWidth(),
                bufferedImage.getType());
        return rot270.filter(bufferedImage, img270);
    default:
        //rotate using a non-standard angle (have to draw a box around the image that can fit it)
        int box = (int) Math.sqrt(bufferedImage.getHeight() * bufferedImage.getHeight()
                + bufferedImage.getWidth() * bufferedImage.getWidth());
        BufferedImage imgFree = new BufferedImage(box, box, bufferedImage.getType());
        BufferedImage imgRet = new BufferedImage(box, box, bufferedImage.getType());
        Graphics2D g = imgFree.createGraphics();
        if (bufferedImage.getTransparency() == Transparency.OPAQUE) {
            //draw a white background on opaque images since they dont support transparency
            g.setBackground(Color.WHITE);
            g.clearRect(0, 0, box, box);
            Graphics2D gr = imgRet.createGraphics();
            gr.setBackground(Color.WHITE);
            gr.clearRect(0, 0, box, box);
            gr = null;
        }
        g.drawImage(bufferedImage, box / 2 - bufferedImage.getWidth() / 2,
                box / 2 - bufferedImage.getHeight() / 2, bufferedImage.getWidth(), bufferedImage.getHeight(),
                null);
        g = null;
        AffineTransform at = new AffineTransform();
        at.rotate(angle * Math.PI / 180.0, box / 2.0, box / 2.0);
        BufferedImageOp bio;
        bio = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
        return bio.filter(imgFree, imgRet);
    }
}

From source file:com.t3.persistence.PersistenceUtil.java

public static void saveToken(Token token, File file, boolean doWait) throws IOException {
    // Thumbnail/*w w  w.  j  a  v  a 2  s  .  co m*/
    BufferedImage image = null;
    if (doWait)
        image = ImageManager.getImageAndWait(token.getImageAssetId());
    else
        image = ImageManager.getImage(token.getImageAssetId());

    Dimension sz = new Dimension(image.getWidth(), image.getHeight());
    SwingUtil.constrainTo(sz, 50);
    BufferedImage thumb = new BufferedImage(sz.width, sz.height, BufferedImage.TRANSLUCENT);
    Graphics2D g = thumb.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.drawImage(image, 0, 0, sz.width, sz.height, null);
    g.dispose();

    PackedFile pakFile = null;
    try {
        pakFile = new PackedFile(file);
        saveAssets(token.getAllImageAssets(), pakFile);
        pakFile.putFile(Token.FILE_THUMBNAIL, ImageUtil.imageToBytes(thumb, "png"));
        pakFile.setContent(token);
        pakFile.save();
    } finally {
        if (pakFile != null)
            pakFile.close();
    }
}

From source file:com.sketchy.image.ImageProcessingThread.java

public static BufferedImage flipImage(BufferedImage image, FlipOption flipOption) {

    if (flipOption == FlipOption.FLIP_NONE)
        return image;

    int imageWidth = image.getWidth();
    int imageHeight = image.getHeight();

    BufferedImage flippedImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g = flippedImage.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);

    int startX = 0;
    int startY = 0;
    int endX = imageWidth;
    int endY = imageHeight;
    if ((flipOption == FlipOption.FLIP_HORIZONTAL) || (flipOption == FlipOption.FLIP_BOTH)) {
        startX = imageWidth;/*w  w  w.jav a  2  s.c  o  m*/
        endX = -imageWidth;
    }
    if ((flipOption == FlipOption.FLIP_VERTICAL) || (flipOption == FlipOption.FLIP_BOTH)) {
        startY = imageHeight;
        endY = -imageHeight;
    }

    g.drawImage(image, startX, startY, endX, endY, null);
    g.dispose();
    return flippedImage;
}

From source file:net.rptools.maptool.util.PersistenceUtil.java

static public void saveCampaignThumbnail(String fileName) {
    BufferedImage screen = MapTool.takeMapScreenShot(new PlayerView(MapTool.getPlayer().getRole()));
    if (screen == null)
        return;//from ww  w  .  ja  va  2s  .  com

    Dimension imgSize = new Dimension(screen.getWidth(null), screen.getHeight(null));
    SwingUtil.constrainTo(imgSize, 200, 200);

    BufferedImage thumb = new BufferedImage(imgSize.width, imgSize.height, BufferedImage.TYPE_INT_BGR);
    Graphics2D g2d = thumb.createGraphics();
    g2d.drawImage(screen, 0, 0, imgSize.width, imgSize.height, null);
    g2d.dispose();

    File thumbFile = getCampaignThumbnailFile(fileName);

    try {
        ImageIO.write(thumb, "jpg", thumbFile);
    } catch (IOException ioe) {
        MapTool.showError("msg.error.failedSaveCampaignPreview", ioe);
    }
}

From source file:SplashScreen.java

public SplashScreen(final BufferedImage img) {
    JPanel panel = new JPanel() {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();

            g2d.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), SplashScreen.this);
        }//from ww  w  .ja  v a2 s . c  o m
    };
    panel.setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));

    Container content = getContentPane();
    content.setLayout(new BorderLayout());
    content.add(panel, BorderLayout.NORTH);
    content.add(label = new JLabel(), BorderLayout.CENTER);
    content.add(bar = new JProgressBar(), BorderLayout.SOUTH);
    pack();
    setLocationRelativeTo(null);
}

From source file:eu.novait.imageresizer.helpers.ImageConverter.java

public void process() throws IOException {
    BufferedImage inImage = ImageIO.read(this.file);
    double ratio = (double) inImage.getWidth() / (double) inImage.getHeight();
    int w = this.width;
    int h = this.height;
    if (inImage.getWidth() >= inImage.getHeight()) {
        w = this.width;
        h = (int) Math.round(w / ratio);
    } else {// w ww . j  a va 2  s. c o  m
        h = this.height;
        w = (int) Math.round(ratio * h);
    }
    int type = inImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : inImage.getType();
    BufferedImage outImage = new BufferedImage(w, h, type);
    Graphics2D g = outImage.createGraphics();
    g.drawImage(inImage, 0, 0, w, h, null);
    g.dispose();
    String ext = FilenameUtils.getExtension(this.file.getAbsolutePath());
    String t = "jpg";
    switch (ext) {
    case "png":
        t = "png";
        break;
    }
    ImageIO.write(outImage, t, this.outputfile);
}