Example usage for java.awt.image BufferedImage getGraphics

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

Introduction

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

Prototype

public java.awt.Graphics getGraphics() 

Source Link

Document

This method returns a Graphics2D , but is here for backwards compatibility.

Usage

From source file:Main.java

/**
 * Shrinks an image to fit into memory more
 * Effectively./*from  w  w  w .j a v a 2  s  .c o  m*/
 * @param src The source image.
 * @return
 */
public static BufferedImage imgUtilMinimizeNoAlpha(BufferedImage src) {
    if (src == null)
        return null;
    BufferedImage b = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
    Graphics2D g = (Graphics2D) b.getGraphics();
    g.drawImage(src, 0, 0, null);
    g.dispose();
    return b;
}

From source file:Main.java

/**
 * Fills the given {@link BufferedImage} with a {@link Paint}, preserving its alpha channel.
 *
 * @param source The source image.//from   ww w.j  ava  2s .com
 * @param paint  The paint to fill with.
 * @return A new, painted/filled image.
 */
public static BufferedImage filledImage(BufferedImage source, Paint paint) {
    BufferedImage newImage = newArgbBufferedImage(source.getWidth(), source.getHeight());
    Graphics2D g = (Graphics2D) newImage.getGraphics();
    g.drawImage(source, 0, 0, null);
    g.setComposite(AlphaComposite.SrcAtop);
    g.setPaint(paint);
    g.fillRect(0, 0, source.getWidth(), source.getHeight());
    return newImage;
}

From source file:Main.java

/**
 * Changes the opacity of a given image.
 * @param src The source image.//from  ww  w  .  j  a  v a  2s .  c  o  m
 * @param opacity The opacity to change to.
 * @return
 */
public static BufferedImage imgUtilAdjustImageTransparency(BufferedImage src, float opacity) {
    if (src == null)
        return null;
    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity);
    BufferedImage cpy = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = (Graphics2D) cpy.getGraphics();
    g.setComposite(ac);
    g.drawImage(src, 0, 0, null);
    g.dispose();
    return cpy;
}

From source file:Main.java

private static BufferedImage renderRotatedObject(Object src, double angle, int width, int height, double tx,
        double ty) {
    BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2d = (Graphics2D) dest.getGraphics();
    g2d.setColor(Color.black);//from   w w  w.j  av  a 2  s  . c om
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    AffineTransform at = AffineTransform.getRotateInstance(angle);
    at.translate(tx, ty);
    g2d.setTransform(at);

    if (src instanceof TextLayout) {
        TextLayout tl = (TextLayout) src;
        tl.draw(g2d, 0, tl.getAscent());
    } else if (src instanceof Image) {
        g2d.drawImage((Image) src, 0, 0, null);
    }
    g2d.dispose();

    return dest;
}

From source file:ImageClip.java

/**
 * Clips the input image to the specified shape
 * /*from   w  w  w  .  jav  a2 s.c om*/
 * @param image
 *           the input image
 * @param clipVerts
 *           list of x, y pairs defining the clip shape, normalised
 *           to image dimensions (think texture coordinates)
 * @return The smallest image containing those pixels that fall
 *         inside the clip shape
 */
public static BufferedImage clip(BufferedImage image, float... clipVerts) {
    assert clipVerts.length >= 6;
    assert clipVerts.length % 2 == 0;

    int[] xp = new int[clipVerts.length / 2];
    int[] yp = new int[xp.length];

    int minX = image.getWidth(), minY = image.getHeight(), maxX = 0, maxY = 0;

    for (int j = 0; j < xp.length; j++) {
        xp[j] = Math.round(clipVerts[2 * j] * image.getWidth());
        yp[j] = Math.round(clipVerts[2 * j + 1] * image.getHeight());

        minX = Math.min(minX, xp[j]);
        minY = Math.min(minY, yp[j]);
        maxX = Math.max(maxX, xp[j]);
        maxY = Math.max(maxY, yp[j]);
    }

    for (int i = 0; i < xp.length; i++) {
        xp[i] -= minX;
        yp[i] -= minY;
    }

    Polygon clip = new Polygon(xp, yp, xp.length);
    BufferedImage out = new BufferedImage(maxX - minX, maxY - minY, image.getType());
    Graphics g = out.getGraphics();
    g.setClip(clip);

    g.drawImage(image, -minX, -minY, null);
    g.dispose();

    return out;
}

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  ww .  ja  v a  2 s  .c o m*/
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:BufferedImageConverter.java

static public BufferedImage createBufferedImage(Image imageIn, int imageType, Component comp) {
    MediaTracker mt = new MediaTracker(comp);
    mt.addImage(imageIn, 0);// w  ww .  jav a2 s  .com
    try {
        mt.waitForID(0);
    } catch (InterruptedException ie) {
    }
    BufferedImage bufferedImageOut = new BufferedImage(imageIn.getWidth(null), imageIn.getHeight(null),
            imageType);
    Graphics g = bufferedImageOut.getGraphics();
    g.drawImage(imageIn, 0, 0, null);

    return bufferedImageOut;
}

From source file:com.liusoft.dlog4j.servlet.DLOG_RandomImageServlet.java

/**
 * ???,,?16,/*w ww .j a  v a2s .  c o  m*/
 * @param num   ??
 * @param out   ?
 * @throws IOException
 */
protected static void render(String num, boolean gif, OutputStream out) throws IOException {
    if (num.getBytes().length > 4)
        throw new IllegalArgumentException("The length of param num cannot exceed 4.");
    int width = 40;
    int height = 15;
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = (Graphics2D) bi.getGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, width, height);
    Font mFont = new Font("Tahoma", Font.PLAIN, 14);
    g.setFont(mFont);
    g.setColor(Color.BLACK);
    g.drawString(num, 2, 13);
    if (gif) {
        AnimatedGifEncoder e = new AnimatedGifEncoder();
        e.setTransparent(Color.WHITE);
        e.start(out);
        e.setDelay(0);
        e.addFrame(bi);
        e.finish();
    } else {
        ImageIO.write(bi, "png", out);
    }
}

From source file:Main.java

public static Image merge(Image left, Image right) {
    BufferedImage merged = new BufferedImage(left.getWidth(null) + right.getWidth(null), left.getHeight(null),
            BufferedImage.TYPE_INT_RGB);
    Graphics2D g = (Graphics2D) merged.getGraphics();
    g.drawImage(left, 0, 0, null);/*from   www .j a v a 2s  . c  o  m*/
    g.drawImage(right, left.getWidth(null), 0, null);
    return merged;
}

From source file:org.b3log.symphony.model.Character.java

/**
 * Creates an image with the specified content (a character).
 *
 * @param content the specified content/* w ww  .  j  a va 2 s . c  om*/
 * @return image
 */
public static BufferedImage createImage(final String content) {
    final BufferedImage ret = new BufferedImage(500, 500, Transparency.TRANSLUCENT);
    final Graphics g = ret.getGraphics();
    g.setClip(0, 0, 50, 50);
    g.fillRect(0, 0, 50, 50);
    g.setFont(new Font(null, Font.PLAIN, 40));
    g.setColor(Color.BLACK);
    g.drawString(content, 5, 40);
    g.dispose();

    return ret;
}