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:peakml.graphics.JFreeChartTools.java

/**
 * This method writes the given graph to the output stream in the PNG format.
 * // w w w . j  a v a2 s.  c o  m
 * @param out         The output stream to write to.
 * @param chart         The chart to be written.
 * @param width         The width of the image.
 * @param height      The height of the image.
 * @throws IOException   Thrown when an error occurs with the IO.
 */
public static void writeAsPNG(OutputStream out, JFreeChart chart, int width, int height) throws IOException {
    BufferedImage graph_img = new BufferedImage(800, 500, BufferedImage.TYPE_INT_ARGB);
    chart.draw(graph_img.createGraphics(), new java.awt.Rectangle(0, 0, 800, 500));
    javax.imageio.ImageIO.write(graph_img, "png", out);
}

From source file:jmbench.plots.UtilPlotPdf.java

protected static BufferedImage draw(JFreeChart chart, int width, int height) {
    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = img.createGraphics();

    chart.draw(g2, new Rectangle2D.Double(0, 0, width, height));

    g2.dispose();/*  w ww  .  j  a  va 2s . c om*/
    return img;
}

From source file:Main.java

public static BufferedImage asCompatibleImage(Image img, int transparency) {
    BufferedImage ret = defaultScreenDeviceConfiguration().createCompatibleImage(img.getWidth(null),
            img.getHeight(null), transparency);
    Graphics2D gc = ret.createGraphics();
    gc.setComposite(AlphaComposite.Src);
    gc.drawImage(img, 0, 0, null);/*from w w  w.  j  ava  2  s .com*/
    gc.dispose();
    return ret;
}

From source file:net.dv8tion.jda.utils.AvatarUtil.java

private static BufferedImage resize(BufferedImage originalImage) {
    BufferedImage resizedImage = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = resizedImage.createGraphics();

    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);

    g.drawImage(originalImage, 0, 0, SIZE, SIZE, Color.white, null);

    g.dispose();//from   w  ww .  j a  v a  2s .  c om

    return resizedImage;
}

From source file:com.seleniumtests.util.imaging.ImageProcessor.java

/**
 * cut part of an image/*  ww  w.  ja v a 2s  .c  om*/
 * @param img      source image
 * @param cropX      x coord of the top left point
 * @param cropY      y coord of the top left point
 * @param width      width of picture portion to keep
 * @param height   height of picture portion to keep
 * @return          cut image
 */
public static BufferedImage cropImage(BufferedImage img, Integer cropX, Integer cropY, Integer width,
        Integer height) {

    BufferedImage newImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    newImg.createGraphics().drawImage(img, 0, 0, width, height, cropX, cropY, cropX + width, cropY + height,
            null);

    return newImg;
}

From source file:QRCode.java

public static String createQRCode(String arg) {
    int size = 125;
    String fileType = "png";
    File myFile = null;/*  ww  w. j  a  v a  2s  .  c  o m*/
    UUID uuid = UUID.nameUUIDFromBytes(arg.getBytes());

    try {
        myFile = File.createTempFile("temp-file-name", ".png");

        Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix byteMatrix = qrCodeWriter.encode(uuid.toString(), BarcodeFormat.QR_CODE, size, size, hintMap);
        int CrunchifyWidth = byteMatrix.getWidth();
        BufferedImage image = new BufferedImage(CrunchifyWidth, CrunchifyWidth, BufferedImage.TYPE_INT_RGB);
        image.createGraphics();

        Graphics2D graphics = (Graphics2D) image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, CrunchifyWidth, CrunchifyWidth);
        graphics.setColor(Color.BLACK);

        for (int i = 0; i < CrunchifyWidth; i++) {
            for (int j = 0; j < CrunchifyWidth; j++) {
                if (byteMatrix.get(i, j)) {
                    graphics.fillRect(i, j, 1, 1);
                }
            }
        }
        ImageIO.write(image, fileType, myFile);
        //System.out.println("\n\nYou have successfully created QR Code " + myFile.getCanonicalPath());
        return myFile.getCanonicalPath();
    } catch (WriterException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:de.laures.cewolf.util.ImageHelper.java

public static BufferedImage loadBufferedImage(String fileName) {
    Image image = loadImage(fileName);
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }//ww  w  .j a  va  2s . c om
    /*        final boolean hasAlpha = hasAlpha(image);
    int transparency = Transparency.OPAQUE;
    if (hasAlpha) {
    transparency = Transparency.BITMASK;
    }*/
    int width = (int) Math.max(1.0, image.getWidth(null));
    int height = (int) Math.max(1.0, image.getHeight(null));
    // BufferedImage bimage = GRAPHICS_CONV.createCompatibleImage(width, height, transparency);
    BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    final Graphics g = bimage.createGraphics();
    g.drawImage(image, 0, 0, null);
    g.dispose();
    return bimage;
}

From source file:script.imglib.analysis.ChartUtils.java

public static final Image<RGBALegacyType> asImage(final JFreeChart chart, int width, int height) {
    ChartPanel panel = new ChartPanel(chart);
    Dimension d = panel.getPreferredSize();
    if (-1 == width && -1 == height) {
        width = d.width;/*from  w ww. java2 s . c o  m*/
        height = d.height;
        panel.setSize(d);
    } else {
        panel.setSize(width, height);
    }
    layoutComponent(panel);
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = bi.createGraphics();
    if (!panel.isOpaque()) {
        g.setColor(panel.getBackground());
        g.fillRect(0, 0, width, height);
    }
    panel.paint(g);
    int[] pixels = new int[width * height];
    PixelGrabber pg = new PixelGrabber(bi, 0, 0, width, height, pixels, 0, width);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
    }
    g.dispose();

    Array<RGBALegacyType, IntAccess> a = new Array<RGBALegacyType, IntAccess>(new ArrayContainerFactory(),
            new IntArray(pixels), new int[] { width, height }, 1);
    // create a Type that is linked to the container
    final RGBALegacyType linkedType = new RGBALegacyType(a);
    // pass it to the DirectAccessContainer
    a.setLinkedType(linkedType);

    return new Image<RGBALegacyType>(a, new RGBALegacyType());
}

From source file:com.seleniumtests.util.imaging.ImageProcessor.java

/**
 * Agregate 2 pictures//w  ww .j  a  v a 2  s .  c o m
 * @param imgf1         first picture to agregate
 * @param imgf2         seconde picture to aggregate
 * @param img2PosX      X coord where second picture will be but, relative to first one 
 * @param img2PosY      Y coord where second picture will be but, relative to first one 
 * @return            the complete picture
 */
public static BufferedImage concat(BufferedImage img1, BufferedImage img2, Integer img2PosX, Integer img2PosY) {

    if (img2PosX < 0 || img2PosY < 0) {
        throw new IllegalArgumentException("relative position must be > 0");
    }

    Integer finalWidth = Math.max(img2.getWidth() + img2PosX, img1.getWidth());
    Integer finalHeight = Math.max(img2.getHeight() + img2PosY, img1.getHeight());

    BufferedImage img = new BufferedImage(finalWidth, finalHeight, BufferedImage.TYPE_INT_RGB);

    img.createGraphics().drawImage(img1, 0, 0, null);
    img.createGraphics().drawImage(img2, img2PosX, img2PosY, null);

    return img;
}

From source file:Utils.java

/** 
 * Renders a component into an image, which is useful for playing with the component's 
 * resultant image in special effects or transitions
 *
 * @param component The component to render
 * @return A buffered image with the rendered component. 
 *///from w  w w  .  ja v  a 2  s  .c  o  m
public static BufferedImage renderComponentToImage(JComponent component) {
    //Create the image
    BufferedImage image = createCompatibleImage(component.getWidth(), component.getHeight());

    //Render the component onto the image
    Graphics graphics = image.createGraphics();
    //      component.update(graphics);
    component.paint(graphics);
    graphics.dispose();
    return image;
}