Example usage for java.awt Graphics2D dispose

List of usage examples for java.awt Graphics2D dispose

Introduction

In this page you can find the example usage for java.awt Graphics2D dispose.

Prototype

public abstract void dispose();

Source Link

Document

Disposes of this graphics context and releases any system resources that it is using.

Usage

From source file:org.jamwiki.utils.ImageUtil.java

/**
 * Convert a Java Image object to a Java BufferedImage object.
 *///from   w w  w  .  j a va2s. c o m
private static BufferedImage imageToBufferedImage(Image image) throws Exception {
    BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
            BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = bufferedImage.createGraphics();
    graphics.drawImage(image, 0, 0, null);
    graphics.dispose();
    return bufferedImage;
}

From source file:org.jfree.chart.demo.PDFChartTransferable.java

public static void writeChartAsPDF(ByteArrayOutputStream bytearrayoutputstream, JFreeChart jfreechart, int i,
        int j, FontMapper fontmapper) throws IOException {
    Rectangle rectangle = new Rectangle(i, j);
    Document document = new Document(rectangle, 50F, 50F, 50F, 50F);
    try {/*from ww w . ja  va2s.c  o m*/
        PdfWriter pdfwriter = PdfWriter.getInstance(document, bytearrayoutputstream);
        document.addAuthor("JFreeChart");
        document.addSubject("Demonstration");
        document.open();
        PdfContentByte pdfcontentbyte = pdfwriter.getDirectContent();
        PdfTemplate pdftemplate = pdfcontentbyte.createTemplate(i, j);
        Graphics2D graphics2d = pdftemplate.createGraphics(i, j, fontmapper);
        java.awt.geom.Rectangle2D.Double double1 = new java.awt.geom.Rectangle2D.Double(0.0D, 0.0D, i, j);
        jfreechart.draw(graphics2d, double1);
        graphics2d.dispose();
        pdfcontentbyte.addTemplate(pdftemplate, 0.0F, 0.0F);
    } catch (DocumentException documentexception) {
        System.err.println(documentexception.getMessage());
    }
    document.close();
}

From source file:Utils.java

public static Shape generateShapeFromText(Font font, String string) {
    BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = img.createGraphics();

    try {/*from  ww w. ja va  2 s.  c o  m*/
        GlyphVector vect = font.createGlyphVector(g2.getFontRenderContext(), string);
        Shape shape = vect.getOutline(0f, (float) -vect.getVisualBounds().getY());

        return shape;
    } finally {
        g2.dispose();
    }
}

From source file:Main.java

/**
 * Snapshots the specified JavaFX {@link Image} object and stores a
 * copy of its pixels into a {@link BufferedImage} object, creating
 * a new object if needed./*from  w  w w. j  ava  2 s  .co m*/
 * The method will only convert a JavaFX {@code Image} that is readable
 * as per the conditions on the
 * {@link Image#getPixelReader() Image.getPixelReader()}
 * method.
 * If the {@code Image} is not readable, as determined by its
 * {@code getPixelReader()} method, then this method will return null.
 * If the {@code Image} is a writable, or other dynamic image, then
 * the {@code BufferedImage} will only be set to the current state of
 * the pixels in the image as determined by its {@link PixelReader}.
 * Further changes to the pixels of the {@code Image} will not be
 * reflected in the returned {@code BufferedImage}.
 * <p>
 * The optional {@code BufferedImage} parameter may be reused to store
 * the copy of the pixels.
 * A new {@code BufferedImage} will be created if the supplied object
 * is null, is too small or of a type which the image pixels cannot
 * be easily converted into.
 * 
 * @param img the JavaFX {@code Image} to be converted
 * @param bimg an optional {@code BufferedImage} object that may be
 *        used to store the returned pixel data
 * @return a {@code BufferedImage} containing a snapshot of the JavaFX
 *         {@code Image}, or null if the {@code Image} is not readable.
 * @since JavaFX 2.2
 */
public static BufferedImage fromFXImage(Image img, BufferedImage bimg) {
    PixelReader pr = img.getPixelReader();
    if (pr == null) {
        return null;
    }
    int iw = (int) img.getWidth();
    int ih = (int) img.getHeight();
    int prefBimgType = getBestBufferedImageType(pr.getPixelFormat(), bimg);
    if (bimg != null) {
        int bw = bimg.getWidth();
        int bh = bimg.getHeight();
        if (bw < iw || bh < ih || bimg.getType() != prefBimgType) {
            bimg = null;
        } else if (iw < bw || ih < bh) {
            Graphics2D g2d = bimg.createGraphics();
            g2d.setComposite(AlphaComposite.Clear);
            g2d.fillRect(0, 0, bw, bh);
            g2d.dispose();
        }
    }
    if (bimg == null) {
        bimg = new BufferedImage(iw, ih, prefBimgType);
    }
    IntegerComponentRaster icr = (IntegerComponentRaster) bimg.getRaster();
    int offset = icr.getDataOffset(0);
    int scan = icr.getScanlineStride();
    int data[] = icr.getDataStorage();
    WritablePixelFormat<IntBuffer> pf = getAssociatedPixelFormat(bimg);
    pr.getPixels(0, 0, iw, ih, pf, data, offset, scan);
    return bimg;
}

From source file:com.smash.revolance.ui.model.helper.ImageHelper.java

public static BufferedImage eraseRect(BufferedImage image, int x, int y, int w, int h, double xScale,
        double yScale) {
    x = (int) (x * xScale);
    w = (int) (w * xScale);

    y = (int) (y * yScale);
    h = (int) (h * yScale);

    Graphics2D g = image.createGraphics();
    g.setColor(new Color(0, 0, 0, 0));
    g.fillRect(x, y, w, h);//from ww  w  .java2s .  c  o m
    g.dispose();
    return image;
}

From source file:es.logongas.util.seguridad.CodigoVerificacionSeguro.java

/**
 * Esta funcin se aplica pq para imagenes muy grandes no reconoce el cdigo
 * QR//from  w  w w  . j  av  a2  s.  co  m
 *
 * @param originalImage La imagen original
 * @param f El factor de escalado
 * @return La iamgen reescalada.
 */
private static BufferedImage resizeImage(BufferedImage originalImage, double f) {
    int ancho = (int) (((double) originalImage.getWidth()) * f);
    int alto = (int) (((double) originalImage.getHeight()) * f);
    int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
    BufferedImage resizedImage = new BufferedImage(ancho, alto, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, ancho, alto, null);
    g.dispose();

    return resizedImage;
}

From source file:com.vectorprint.report.jfree.ItextChartHelper.java

/**
 *
 * @param chart the chart tp plot//from www . j a  va  2s  .co  m
 * @param contentByte the canvas to plot to
 * @param width the width of the chart will be fit to
 * @param height the height of the chart will be fit to
 * @return
 * @throws BadElementException
 */
public static Image getChartImage(JFreeChart chart, PdfContentByte contentByte, float width, float height,
        float opacity) throws BadElementException {

    // create PdfTemplate from PdfContentByte
    PdfTemplate template = contentByte.createTemplate(width, height);
    template.saveState();
    PdfGState pgs = new PdfGState();
    pgs.setFillOpacity(opacity);
    pgs.setStrokeOpacity(opacity);
    template.setGState(pgs);

    // create Graphics2D from PdfTemplate
    Graphics2D g2 = new PdfGraphics2D(template, width, height);

    // setup the drawing area
    Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height);

    // pass the Graphics2D and drawing area to JFreeChart
    chart.draw(g2, r2D);
    g2.dispose(); // always dispose this

    template.restoreState();

    // create Image from PdfTemplate
    return Image.getInstance(template);
}

From source file:com.fun.util.TesseractUtil.java

/**
 * //from w w  w.  j  ava2s  .co  m
 *
 * @param imageFile
 * @param times
 * @param targetFile
 * @throws IOException
 */
private static void scaled(File imageFile, int times, File targetFile) throws IOException {
    BufferedImage image = ImageIO.read(imageFile);
    int targetWidth = image.getWidth() * times;
    int targetHeight = image.getHeight() * times;
    int type = (image.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage tmp = new BufferedImage(targetWidth, targetHeight, type);
    Graphics2D g2 = tmp.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g2.drawImage(image, 0, 0, targetWidth, targetHeight, null);
    g2.dispose();
    ImageIO.write(tmp, "png", targetFile);
}

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  ww .  j a v a2 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

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 {//from   w  w  w . j  a v  a2 s .c  o  m
        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;
}