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

/**
 * Creates and returns image from the given text.
 * @param text input text/*from   ww w  . ja v  a2  s .  c o  m*/
 * @param font text font
 * @return image with input text
 */
public static BufferedImage createImageFromText(String text, Font font) {
    //You may want to change these setting, or make them parameters
    boolean isAntiAliased = true;
    boolean usesFractionalMetrics = false;
    FontRenderContext frc = new FontRenderContext(null, isAntiAliased, usesFractionalMetrics);
    TextLayout layout = new TextLayout(text, font, frc);
    Rectangle2D bounds = layout.getBounds();
    int w = (int) Math.ceil(bounds.getWidth());
    int h = (int) Math.ceil(bounds.getHeight()) + 2;
    BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); //for example;
    Graphics2D g = image.createGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, w, h);
    g.setColor(Color.BLACK);
    g.setFont(font);
    Object antiAliased = isAntiAliased ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON
            : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF;
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, antiAliased);
    Object fractionalMetrics = usesFractionalMetrics ? RenderingHints.VALUE_FRACTIONALMETRICS_ON
            : RenderingHints.VALUE_FRACTIONALMETRICS_OFF;
    g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, fractionalMetrics);
    g.drawString(text, (float) -bounds.getX(), (float) -bounds.getY());
    g.dispose();

    return image;
}

From source file:com.pureinfo.srm.common.ImageHelper.java

public static void drawImage(String _sString, OutputStream _os) throws PureException {
    int nWidth = 200;
    int nHeight = 50;
    String sText = _sString;/*from w  w w.  ja  v a 2 s. c om*/

    BufferedImage image = new BufferedImage(nWidth, nHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();

    for (int i = 0; i < sText.length(); i++) {
        draw(String.valueOf(sText.charAt(i)), g2, i * nWidth / sText.length(), 0,
                (i + 1) * nWidth / sText.length(), nHeight);
    }

    g2.dispose();
    try {
        EncoderUtil.writeBufferedImage(image, ImageFormat.PNG, _os);
    } catch (Exception ex) {
        throw new PureException(PureException.UNKNOWN, "", ex);
    }
}

From source file:Main.java

/**
 * Converts a {@link Image} into a {@link BufferedImage}. If the image 
 * is already a {@link BufferedImage} then the same instance is returned.
 *
 * @param image the image to convert to a buffered image.
 * @param imageType the image type to use.
 * @return the converted image or the same instance if already a
 * {@link BufferedImage}.//w w w  . jav  a  2s  .co  m
 */
public static BufferedImage toBufferedImage(Image image, int imageType) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }
    final BufferedImage buffImage = new BufferedImage(image.getWidth(null), image.getHeight(null), imageType);
    final Graphics2D gfx = buffImage.createGraphics();
    gfx.drawImage(image, 0, 0, null);
    return buffImage;
}

From source file:Main.java

public static BufferedImage highlightRegions(Image img, int[][] regions, int regionId, Color fgColour) {
    BufferedImage canvas = new BufferedImage(img.getWidth(null), img.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = canvas.createGraphics();
    g2d.drawImage(img, 0, 0, null);/*from w  ww . ja  v  a  2 s. c  o m*/
    g2d.setColor(fgColour);
    for (int y = 0; y < regions.length; y++) {
        for (int x = 0; x < regions[y].length; x++) {
            if (regions[y][x] == regionId) {
                g2d.drawRect(x, y, 1, 1);
            }
        }
    }

    return canvas;
}

From source file:Main.java

public static BufferedImage takeScreenShot(Component component, String... watermarks) {

    Dimension size = component.getSize();

    BufferedImage screenshot = new BufferedImage(size.width, size.height, Transparency.OPAQUE);
    Graphics2D g = screenshot.createGraphics();
    g.setClip(0, 0, size.width - 1, size.height - 1);

    component.update(g);/*  w w  w.j  a  v a2s . c o  m*/

    FontMetrics fm = g.getFontMetrics();
    int y = fm.getDescent();
    for (String watermark : watermarks)
        if (watermark != null) {
            int x = size.width - SwingUtilities.computeStringWidth(fm, watermark);

            g.setColor(Color.black);
            g.drawString(watermark, x, y);

            g.setColor(Color.white);
            g.drawString(watermark, x - 1, y - 1);

            y -= fm.getHeight();
        }

    g.dispose();

    return screenshot;
}

From source file:edu.jhuapl.graphs.jfreechart.utils.HighResChartUtil.java

/**
 * Returns a high resolution BufferedImage of the chart. Uses the default DPI_FILE_RESOLUTION.
 *
 * @param resolution The resolution, in dots per inch, of the image to generate.
 * @return the buffered image./*ww  w  . j  a va2s  .c  o m*/
 */
public static BufferedImage getHighResChartImage(ChartPanel chartPanel, int resolution) {
    int screenResolution = Toolkit.getDefaultToolkit().getScreenResolution();
    double scaleRatio = resolution / screenResolution;
    int width = chartPanel.getWidth();
    int height = chartPanel.getHeight();
    int rasterWidth = (int) (width * scaleRatio);
    int rasterHeight = (int) (height * scaleRatio);

    BufferedImage image = new BufferedImage(rasterWidth, rasterHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();

    g2.transform(AffineTransform.getScaleInstance(scaleRatio, scaleRatio));
    chartPanel.getChart().draw(g2, new Rectangle2D.Double(0, 0, width, height), null);
    g2.dispose();

    return image;
}

From source file:Main.java

private static BufferedImage prepareImage(BufferedImage image, int shadowSize) {
    BufferedImage subject = new BufferedImage(image.getWidth() + shadowSize * 2,
            image.getHeight() + shadowSize * 2, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2 = subject.createGraphics();
    g2.drawImage(image, null, shadowSize, shadowSize);
    g2.dispose();// ww  w.  j a  va 2  s  .  co  m

    return subject;
}

From source file:Main.java

public static BufferedImage asCompatibleImage(Image img) {
    BufferedImage ret = defaultScreenDeviceConfiguration().createCompatibleImage(img.getWidth(null),
            img.getHeight(null));/*from ww  w .j av  a  2 s . co  m*/
    Graphics2D gc = ret.createGraphics();
    gc.drawImage(img, 0, 0, null);
    gc.dispose();
    return ret;
}

From source file:CursorUtil.java

public static Cursor createCursor(BufferedImage img, Point hotspot, String name)
        throws IndexOutOfBoundsException, Exception {
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension d = tk.getBestCursorSize(img.getWidth(), img.getHeight());
    if ((d.width == img.getWidth()) && (d.height == img.getHeight()))
        return tk.createCustomCursor(img, hotspot, name);

    if ((d.width + d.height) < 2)
        throw new Exception("Invalid Size");

    BufferedImage newImg = GraphicsUtil.createImage(d.width, d.height);
    Graphics2D g2 = newImg.createGraphics();
    g2.drawImage(img, // what to draw
            0, // dest left
            0, // dest top
            newImg.getWidth(), // dest right
            newImg.getHeight(), // dest bottom
            0, // src left
            0, // src top
            img.getWidth(), // src right
            img.getHeight(), // src bottom
            null // to notify of image updates
    );/*from w w  w .j a  v  a2 s .  c om*/

    return tk.createCustomCursor(newImg, hotspot, name);
}

From source file:ImageUtil.java

public static BufferedImage scale(BufferedImage src, int width, int height) throws IOException {
    BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = dest.createGraphics();
    AffineTransform at = AffineTransform.getScaleInstance((double) width / src.getWidth(),
            (double) height / src.getHeight());
    g.drawRenderedImage(src, at);/*w  w w  . j av  a 2 s.  c om*/
    return dest;
}