Example usage for java.awt RenderingHints KEY_INTERPOLATION

List of usage examples for java.awt RenderingHints KEY_INTERPOLATION

Introduction

In this page you can find the example usage for java.awt RenderingHints KEY_INTERPOLATION.

Prototype

Key KEY_INTERPOLATION

To view the source code for java.awt RenderingHints KEY_INTERPOLATION.

Click Source Link

Document

Interpolation hint key.

Usage

From source file:sernet.verinice.service.commands.LoadAttachmentFile.java

private void drawThumbnail(BufferedImage image, BufferedImage resizedImage) {
    Graphics2D g = resizedImage.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
    g.drawImage(image, 0, 0, resizedImage.getWidth(), resizedImage.getHeight(), null);
    g.dispose();//w  ww  .  java 2  s . co  m
}

From source file:util.ui.UiUtilities.java

/**
 * Scales Icons to a specific size//from   ww w.j  a va2s .co m
 *
 * @param icon
 *          Icon that should be scaled
 * @param width
 *          scaled width
 * @param height
 *          scaled height
 * @return Scaled Icon
 */
public static Icon scaleIcon(Icon icon, int width, int height) {
    if (icon == null) {
        return null;
    }
    int currentWidth = icon.getIconWidth();
    int currentHeight = icon.getIconHeight();
    if ((currentWidth == width) && (currentHeight == height)) {
        return icon;
    }
    try {
        // Create Image with Icon
        BufferedImage iconImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = iconImage.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        AffineTransform z = g2.getTransform();
        g2.setTransform(z);
        icon.paintIcon(null, g2, 0, 0);
        g2.dispose();
        BufferedImage scaled = scaleDown(iconImage, width, height);
        // Return new Icon
        return new ImageIcon(scaled);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return icon;
}

From source file:util.ui.UiUtilities.java

/**
 * Convenience method that returns a scaled instance of the
 * provided {@code BufferedImage}./* w w w.j ava 2  s.c o  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
 * @return a scaled version of the original {@code BufferedImage}
 */
public static BufferedImage scaleDown(final BufferedImage img, final int targetWidth, final int targetHeight) {
    if (targetWidth > img.getWidth() || targetHeight > img.getHeight()) {
        return scaleIconToBufferedImage(img, targetWidth, targetHeight);
    }

    int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage result = img;
    int w = img.getWidth();
    int h = img.getHeight();

    do {
        w /= 2;
        if (w < targetWidth) {
            w = targetWidth;
        }
        h /= 2;
        if (h < targetHeight) {
            h = targetHeight;
        }

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

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

    return result;
}