Example usage for java.awt.image BufferedImage BufferedImage

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

Introduction

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

Prototype

public BufferedImage(int width, int height, int imageType) 

Source Link

Document

Constructs a BufferedImage of one of the predefined image types.

Usage

From source file:Main.java

private static BufferedImage resizeImageWithHint(BufferedImage originalImage, int type) {
    int IMG_WIDTH = 512;
    int IMG_CLAHEIGHT = 512;
    BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_CLAHEIGHT, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_CLAHEIGHT, null);
    g.dispose();/*from w w  w.j  av a2  s.co  m*/
    g.setComposite(AlphaComposite.Src);

    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    return resizedImage;
}

From source file:Main.java

public static BufferedImage int2image(int[][] scene) {
    int maxValue = -1;
    int minValue = Integer.MAX_VALUE;
    for (int y = 0; y < scene.length; y++) {
        for (int x = 0; x < scene[y].length; x++) {
            maxValue = Math.max(maxValue, scene[y][x]);
            minValue = Math.min(minValue, scene[y][x]);
        }//from  w  ww. ja  v a2s  . c  o m
    }

    if (maxValue == minValue)
        maxValue = minValue + 1;
    final double scale = 255.0 / (maxValue - minValue);

    BufferedImage image = new BufferedImage(scene[0].length, scene.length, BufferedImage.TYPE_INT_RGB);
    for (int y = 0; y < scene.length; y++) {
        for (int x = 0; x < scene[y].length; x++) {
            final int c = (int) (scale * (scene[y][x] - minValue));
            image.setRGB(x, y, c << 16 | c << 8 | c);
        }
    }

    return image;
}

From source file:Transparency.java

public static BufferedImage makeImageTranslucent(BufferedImage source, double alpha) {
    BufferedImage target = new BufferedImage(source.getWidth(), source.getHeight(),
            java.awt.Transparency.TRANSLUCENT);
    // Get the images graphics
    Graphics2D g = target.createGraphics();
    // Set the Graphics composite to Alpha
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) alpha));
    // Draw the image into the prepared reciver image
    g.drawImage(source, null, 0, 0);/*from w  w  w  .jav a  2 s .  co  m*/
    // let go of all system resources in this Graphics
    g.dispose();
    // Return the image
    return target;
}

From source file:Main.java

private Image createImage() {
    BufferedImage bufferedImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
    Graphics g = bufferedImage.getGraphics();
    g.drawString("www.java2s.com", 20, 20);

    return bufferedImage;
}

From source file:Main.java

public static Image createImage(int size, Color color) {
    BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = image.createGraphics();
    g.setColor(color);/*from   www  .  j a va 2 s .co m*/
    g.fillRect(0, 0, size, size);
    g.dispose();
    return image;
}

From source file:GrayImage.java

public void paint(Graphics g) {
    Image myImage = new ImageIcon("yourImage.png").getImage();
    BufferedImage bufferedImage = new BufferedImage(myImage.getHeight(this), myImage.getWidth(this),
            BufferedImage.TYPE_BYTE_GRAY);

    Graphics gi = bufferedImage.getGraphics();
    gi.drawImage(myImage, 0, 0, null);/*from  ww  w  .ja va 2  s .  c o m*/
    gi.dispose();

    Graphics2D g2d = (Graphics2D) g;
    g2d.drawImage(bufferedImage, null, 0, 0);
}

From source file:Main.java

/**
 * Creates (in a rather clumsy way) the font metrics for a given {@link Font}.
 * //from   w ww  . j av a2s.  co m
 * @param aFont
 *          the font instance to create the font metrics for, cannot be
 *          <code>null</code>.
 * @return a font metrics, never <code>null</code>.
 */
private static FontMetrics createFontMetrics(final Font aFont) {
    BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
    Graphics canvas = img.getGraphics();

    try {
        return canvas.getFontMetrics(aFont);
    } finally {
        canvas.dispose();
        canvas = null;
        img = null;
    }
}

From source file:Main.java

public static BufferedImage generateOutline(BufferedImage source, Color color1, Color color2, boolean alpha) {
    int w = source.getWidth();
    int h = source.getHeight();
    BufferedImage result = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);

    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            if (!testNeighbour(source, x, y, alpha)) {
                if (testNeighbours(source, x, y, alpha)) {
                    boolean a = ((x + y) % 10) <= 5;
                    result.setRGB(x, y, a ? color1.getRGB() : color2.getRGB());
                }//from  ww w . j av a  2s. c  om
            }
        }
    }
    return result;
}

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  w  w w .j  av  a 2 s. c om*/
 * @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;
}

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;

    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f);

    BufferedImage buffImg = new BufferedImage(60, 60, BufferedImage.TYPE_INT_ARGB);
    Graphics2D gbi = buffImg.createGraphics();

    gbi.setPaint(Color.red);//from  www. j  a v  a  2  s . com
    gbi.fillRect(0, 0, 40, 40);
    gbi.setComposite(ac);

    gbi.setPaint(Color.green);
    gbi.fillRect(5, 5, 40, 40);

    g2d.drawImage(buffImg, 20, 20, null);
}