Example usage for java.awt.image BufferedImage createGraphics

List of usage examples for java.awt.image BufferedImage createGraphics

Introduction

In this page you can find the example usage for java.awt.image BufferedImage createGraphics.

Prototype

public Graphics2D createGraphics() 

Source Link

Document

Creates a Graphics2D , which can be used to draw into this BufferedImage .

Usage

From source file:Main.java

public static BufferedImage getTransparentImage(BufferedImage image, Color transparent) {
    BufferedImage img = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = img.createGraphics();
    for (int x = 0; x < img.getWidth(); x++) {
        for (int y = 0; y < img.getHeight(); y++) {
            if (image.getRGB(x, y) != transparent.getRGB()) {
                img.setRGB(x, y, image.getRGB(x, y));
            }//w  w w  .j  a  v  a2s.  c om
        }
    }
    g.dispose();
    return img;
}

From source file:BufferedImages.java

/**
 * Returns a {@link BufferedImage} with the specified image type, where the
 * graphical content is a copy of the specified image.
 * //from   ww w .  j  av a  2  s.com
 * @param img      The image to copy.
 * @param imageType   The image type for the image to return.
 * @return         A copy of the specified image.
 */
public static BufferedImage copy(BufferedImage img, int imageType) {
    int width = img.getWidth();
    int height = img.getHeight();

    BufferedImage newImage = new BufferedImage(width, height, imageType);
    Graphics g = newImage.createGraphics();

    g.drawImage(img, 0, 0, null);

    g.dispose();

    return newImage;
}

From source file:Main.java

public static BufferedImage createImage(Component comp) {
    if (comp == null)
        return null;

    BufferedImage image = (BufferedImage) comp.createImage(comp.getWidth(), comp.getHeight());
    Graphics g = image.createGraphics();
    comp.paintAll(g);//from w  w  w . j av a  2s  .  c  o  m
    return image;
}

From source file:Main.java

public static TexturePaint createCheckerTexture(int cs, Color color) {
    int size = cs * cs;
    BufferedImage img = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = img.createGraphics();
    g2.setPaint(color);/*  w  w  w.j a  va  2 s  .  co m*/
    g2.fillRect(0, 0, size, size);
    for (int i = 0; i * cs < size; i++) {
        for (int j = 0; j * cs < size; j++) {
            if ((i + j) % 2 == 0) {
                g2.fillRect(i * cs, j * cs, cs, cs);
            }
        }
    }
    g2.dispose();
    return new TexturePaint(img, new Rectangle(0, 0, size, size));
}

From source file:Utils.java

/**
 * Produces a resized image that is of the given dimensions
 *
 * @param image The original image//from  w ww.j a  v  a 2 s . c  om
 * @param width The desired width
 * @param height The desired height
 * @return The new BufferedImage
 */
public static BufferedImage scaledImage(BufferedImage image, int width, int height) {
    BufferedImage newImage = createCompatibleImage(width, height);
    Graphics graphics = newImage.createGraphics();

    graphics.drawImage(image, 0, 0, width, height, null);

    graphics.dispose();
    return newImage;
}

From source file:Main.java

public static BufferedImage scaleImage(BufferedImage img, int width, int height, Color background) {
    int imgWidth = img.getWidth();
    int imgHeight = img.getHeight();
    if (imgWidth * height < imgHeight * width) {
        width = imgWidth * height / imgHeight;
    } else {//from   w w  w.  j  ava 2  s.  c o  m
        height = imgHeight * width / imgWidth;
    }
    BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = newImage.createGraphics();
    try {
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g.setBackground(background);
        g.clearRect(0, 0, width, height);
        g.drawImage(img, 0, 0, width, height, null);
    } finally {
        g.dispose();
    }
    return newImage;
}

From source file:Main.java

/**
 * Resizes an image using trilinear filtering.
 * @param source   the source image./*w w w  .j a  va  2 s.  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./*  w ww . j a  va  2s .co  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:Main.java

/**
 * Resize image to specified dimensions//from   www.ja  v a  2s  .  com
 * @param image image to resize
 * @param width new image width
 * @param height new image height
 * @return resized image
 */
public static BufferedImage resizeImage(BufferedImage image, int width, int height) {
    BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(image, 0, 0, width, height, null);
    g.dispose();

    return resizedImage;
}

From source file:Main.java

/**
 * Smoothly scales the given {@link BufferedImage} to the given width and height using the
 * {@link Image#SCALE_SMOOTH} algorithm (generally bicubic resampling or bilinear filtering).
 *
 * @param source The source image./*from  www.j  a  va2  s  . c o  m*/
 * @param width  The destination width to scale to.
 * @param height The destination height to scale to.
 * @return A new, scaled image.
 */
public static BufferedImage scaledImage(BufferedImage source, int width, int height) {
    Image scaledImage = source.getScaledInstance(width, height, Image.SCALE_SMOOTH);
    BufferedImage scaledBufImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics g = scaledBufImage.createGraphics();
    g.drawImage(scaledImage, 0, 0, null);
    g.dispose();
    return scaledBufImage;
}