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:ImageUtil.java

/**
 * Creates and returns a buffered version of the specified image.
 *
 * @param image the image to create a buffered image for
 * @return a buffered image based on the specified image
 *///from   w w  w  .  j a  v  a  2  s .c  om
public static BufferedImage getBufferedImage(Image image) {
    BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);

    Graphics g = bufferedImage.getGraphics();

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

    return bufferedImage;
}

From source file:Main.java

private static int[] makeGradientPallet() {
    BufferedImage image = new BufferedImage(100, 1, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();
    Point2D start = new Point2D.Float(0f, 0f);
    Point2D end = new Point2D.Float(99f, 0f);
    float[] dist = { 0.0f, 0.5f, 1.0f };
    Color[] colors = { Color.RED, Color.YELLOW, Color.GREEN };
    g2.setPaint(new LinearGradientPaint(start, end, dist, colors));
    g2.fillRect(0, 0, 100, 1);/*from  w w  w .  j  av  a 2  s .  c o  m*/
    g2.dispose();

    int width = image.getWidth(null);
    int[] pallet = new int[width];
    PixelGrabber pg = new PixelGrabber(image, 0, 0, width, 1, pallet, 0, width);
    try {
        pg.grabPixels();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return pallet;
}

From source file:Main.java

/**
 * Creates a shadow mask//from w  ww  .  jav a  2 s  .c  om
 * @param image
 * @param shadowColor
 * @param shadowOpacity
 * @return
 */
private static BufferedImage createShadowMask(BufferedImage image, Color shadowColor, float shadowOpacity) {
    BufferedImage mask = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2d = mask.createGraphics();
    g2d.drawImage(image, 0, 0, null);
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, shadowOpacity));
    g2d.setColor(shadowColor);
    g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
    g2d.dispose();

    return mask;
}

From source file:Main.java

public static BufferedImage createImage(JTable table) {
    JTableHeader tableHeaderComp = table.getTableHeader();
    int totalWidth = tableHeaderComp.getWidth() + table.getWidth();
    int totalHeight = tableHeaderComp.getHeight() + table.getHeight();
    BufferedImage tableImage = new BufferedImage(totalWidth, totalHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2D = (Graphics2D) tableImage.getGraphics();
    tableHeaderComp.paint(g2D);// w  w  w .jav a  2  s .c o  m
    g2D.translate(0, tableHeaderComp.getHeight());
    table.paint(g2D);
    return tableImage;
}

From source file:Main.java

public static BufferedImage getFlippedImage(BufferedImage bi) {
    BufferedImage flipped = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
    AffineTransform tran = AffineTransform.getTranslateInstance(0, bi.getHeight());
    AffineTransform flip = AffineTransform.getScaleInstance(1d, -1d);
    tran.concatenate(flip);/* w w w. j  av a2  s. c  o m*/

    Graphics2D g = flipped.createGraphics();
    g.setTransform(tran);
    g.drawImage(bi, 0, 0, null);
    g.dispose();

    return flipped;
}

From source file:ImageUtils.java

/**
 * Creates a transparent image.  These can be used for aligning menu items.
 *
 * @param width  the width./*from   ww w .  jav a2s  .  com*/
 * @param height the height.
 * @return the created transparent image.
 */
public static BufferedImage createTransparentImage(final int width, final int height) {
    return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
}

From source file:Main.java

public static BufferedImage resize(int targetWidth, int targetHeight, BufferedImage src) {
    double scaleW = (double) targetWidth / (double) src.getWidth();
    double scaleH = (double) targetHeight / (double) src.getHeight();

    double scale = scaleW < scaleH ? scaleW : scaleH;

    BufferedImage result = new BufferedImage((int) (src.getWidth() * scale), (int) (src.getHeight() * scale),
            BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2d = result.createGraphics();
    g2d.drawImage(src, 0, 0, result.getWidth(), result.getHeight(), null);
    g2d.dispose();/*ww  w  .  ja va2s  . c  o  m*/

    return result;
}

From source file:Main.java

/**
 * Creates a nifty looking inner shadow for the window.
 * @param width The width of the shadow.
 * @param height The height of the shadow.
 * @return The translucent shadow that was created.
 *//*from   w ww . jav  a 2 s .  c  o m*/
public static BufferedImage genInnerShadow(int width, int height) {
    BufferedImage shadowOuter = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics g = shadowOuter.getGraphics();
    Graphics2D g2 = (Graphics2D) g;
    Point2D center = new Point2D.Float(width / 2, height / 2);
    float radius = width;
    Point2D focus = new Point2D.Float(width / 2, height / 2);
    float[] dist = { 0.0f, 1.0f };
    Color[] colors = { new Color(0, 0, 0, 0), new Color(0, 0, 0, 220) };
    RadialGradientPaint p = new RadialGradientPaint(center, radius, focus, dist, colors, CycleMethod.NO_CYCLE);
    g2.setPaint(p);
    g2.fillRect(0, 0, width, height);
    return shadowOuter;
}

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 {//www  .  j a  v a2s  .  c  om
        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

public static BufferedImage getStrokedImage(BufferedImage bi, Shape shape, int strokeWidth) {
    int w = bi.getWidth();
    int h = bi.getHeight();
    BufferedImage bib = new BufferedImage(w, h, bi.getType());
    Graphics2D g = bib.createGraphics();

    BasicStroke bs = new BasicStroke(strokeWidth);
    g.setStroke(bs);/* ww  w  .  ja  v  a  2s.  c om*/
    Rectangle rect = new Rectangle(0, 0, w, h);
    TexturePaint tp = new TexturePaint(bi, rect);
    g.setPaint(tp);
    g.draw(shape);

    g.dispose();
    return bib;
}