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

public static Cursor buildCursorByTrimming(Toolkit toolkit, Image image, int x, int y, String name,
        Cursor defaultCursor) {//from   w w w. j a  va2 s  .co  m
    Dimension d = toolkit.getBestCursorSize(image.getWidth(null), image.getHeight(null));
    if (d == null || d.getWidth() <= 0 || d.getHeight() <= 0)
        return defaultCursor;
    BufferedImage out = new BufferedImage((int) d.getWidth(), (int) d.getHeight(), BufferedImage.TYPE_INT_ARGB);
    out.getGraphics().drawImage(image, 0, 0, null);
    return toolkit.createCustomCursor(out, new Point(x, y), name);
}

From source file:Util.java

/**
 * Converts an Image to a BufferedImage//w w w .  j a  va2 s. com
 */
public static BufferedImage convertToBufferedImg(Image im) {
    BufferedImage bi = new BufferedImage(im.getWidth(null), im.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    Graphics bg = bi.getGraphics();
    bg.drawImage(im, 0, 0, null);
    bg.dispose();
    return bi;
}

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

/**
 * Creates (in a rather clumsy way) the font metrics for a given {@link Font}.
 * /*www .  java  2 s  .  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 convert2grey(BufferedImage image) {
    BufferedImage grey = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
    Graphics g = grey.getGraphics();
    g.drawImage(image, 0, 0, null);/*from ww  w .j  a v a  2  s  . com*/
    g.dispose();
    BufferedImage rgb = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
    g = rgb.getGraphics();
    g.drawImage(grey, 0, 0, null);
    g.dispose();
    return rgb;
}

From source file:Main.java

/**
 * Quickly crops an image from its source.
 * @param src The source image./*from www .  ja va 2s.  c o  m*/
 * @param x The x coordinate to start at.
 * @param y The y coordinate to start at.
 * @param width The width to clip.
 * @param height The height to clip.
 * @return
 */
public static BufferedImage imgUtilFastCrop(BufferedImage src, int x, int y, int width, int height) {
    if (src == null)
        return null;
    if (x == 0 && y == 0 && width == src.getWidth() && height == src.getHeight())
        return imgUtilFastCopy(src);
    else {
        BufferedImage b = new BufferedImage(width, height, src.getType());
        Graphics g = b.getGraphics();
        g.drawImage(src, -x, -y, null);
        g.dispose();
        return b;
    }
}

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);/*from w w  w .ja v  a2s . co m*/
    g2D.translate(0, tableHeaderComp.getHeight());
    table.paint(g2D);
    return tableImage;
}

From source file:WaterMark.java

public static String execute(String src, String dest, String text, Color color, Font font) throws Exception {
    BufferedImage srcImage = ImageIO.read(new File(src));

    int width = srcImage.getWidth(null);
    int height = srcImage.getHeight(null);
    BufferedImage destImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics g = destImage.getGraphics();

    g.drawImage(srcImage, 0, 0, width, height, null);
    g.setColor(color);/*from www .  ja  va2  s  .  com*/
    g.setFont(font);
    g.fillRect(0, 0, 50, 50);
    g.drawString(text, width / 5, height - 10);
    g.dispose();

    ImageIO.write(destImage, DEFAULT_FORMAT, new File("dest.jpg"));
    return dest;
}

From source file:Main.java

/**
 * Applies a {@link BufferedImageOp} on the given {@link BufferedImage}.
 *
 * @param source The source image.//from  ww w.j  av a2 s.c o m
 * @param op     The operation to perform.
 * @return A new image with the operation performed.
 */
public static BufferedImage operatedImage(BufferedImage source, BufferedImageOp op) {
    BufferedImage newImage = newArgbBufferedImage(source.getWidth(), source.getHeight());
    Graphics2D g = (Graphics2D) newImage.getGraphics();
    g.drawImage(source, op, 0, 0);
    return newImage;
}

From source file:Main.java

/**
 * Creates an outline of an image,//from ww  w.  j ava  2 s.c  o m
 * with the default clipping rectangle.
 * @param src The source image.
 * @param c The color to outline the image
 * in.
 * @return
 */
public static BufferedImage imgUtilOutline(BufferedImage src, Color c) {
    if (src == null)
        return null;
    BufferedImage b = new BufferedImage(src.getWidth(), src.getHeight(), src.getType());
    Graphics2D g = (Graphics2D) b.getGraphics();
    g.setColor(c);
    g.drawRect(1, 1, src.getWidth() - 1, src.getHeight() - 1);
    g.drawImage(src, 0, 0, null);
    g.dispose();
    return b;
}