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:Utils.java

public static BufferedImage createGradientImage(int width, int height, Color gradient1, Color gradient2) {

    BufferedImage gradientImage = createCompatibleImage(width, height);
    GradientPaint gradient = new GradientPaint(0, 0, gradient1, 0, height, gradient2, false);
    Graphics2D g2 = (Graphics2D) gradientImage.getGraphics();
    g2.setPaint(gradient);// w w w . ja  v  a  2  s  . c  om
    g2.fillRect(0, 0, width, height);
    g2.dispose();

    return gradientImage;
}

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();
    return img;/*  w  w w . ja va 2  s .  co m*/
}

From source file:Main.java

/**
 * Creates an outline of an image,//ww w  .  ja v a  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;
}

From source file:Main.java

public static void draw(BufferedImage image, Rectangle rectangle, BufferedImage backgroundImg) {
    Graphics2D g = backgroundImg.createGraphics();
    if (rectangle == null) {
        g.drawImage(image, 0, 0, null);//from w ww  .  j  a  v a2  s .  c  om
    } else {
        g.drawImage(image, rectangle.x, rectangle.y, null);
    }
    g.dispose();
}

From source file:net.chris54721.infinitycubed.utils.Utils.java

public static BufferedImage toBufferedImage(Image img) {
    if (img instanceof BufferedImage)
        return (BufferedImage) img;
    BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D bGr = bimage.createGraphics();
    bGr.drawImage(img, 0, 0, null);//from   w ww .  ja  v a 2  s .  com
    bGr.dispose();
    return bimage;
}

From source file:Main.java

/**
 * Snapshots the specified {@link BufferedImage} and stores a copy of
 * its pixels into a JavaFX {@link Image} object, creating a new
 * object if needed.//from ww w  . j  a  v a 2s  . c  o m
 * The returned {@code Image} will be a static snapshot of the state
 * of the pixels in the {@code BufferedImage} at the time the method
 * completes.  Further changes to the {@code BufferedImage} will not
 * be reflected in the {@code Image}.
 * <p>
 * The optional JavaFX {@link WritableImage} parameter may be reused
 * to store the copy of the pixels.
 * A new {@code Image} 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 bimg the {@code BufferedImage} object to be converted
 * @param wimg an optional {@code WritableImage} object that can be
 *        used to store the returned pixel data
 * @return an {@code Image} object representing a snapshot of the
 *         current pixels in the {@code BufferedImage}.
 * @since JavaFX 2.2
 */
public static WritableImage toFXImage(BufferedImage bimg, WritableImage wimg) {
    int bw = bimg.getWidth();
    int bh = bimg.getHeight();
    switch (bimg.getType()) {
    case BufferedImage.TYPE_INT_ARGB:
    case BufferedImage.TYPE_INT_ARGB_PRE:
        break;
    default:
        BufferedImage converted = new BufferedImage(bw, bh, BufferedImage.TYPE_INT_ARGB_PRE);
        Graphics2D g2d = converted.createGraphics();
        g2d.drawImage(bimg, 0, 0, null);
        g2d.dispose();
        bimg = converted;
        break;
    }
    // assert(bimg.getType == TYPE_INT_ARGB[_PRE]);
    if (wimg != null) {
        int iw = (int) wimg.getWidth();
        int ih = (int) wimg.getHeight();
        if (iw < bw || ih < bh) {
            wimg = null;
        } else if (bw < iw || bh < ih) {
            int empty[] = new int[iw];
            PixelWriter pw = wimg.getPixelWriter();
            PixelFormat<IntBuffer> pf = PixelFormat.getIntArgbPreInstance();
            if (bw < iw) {
                pw.setPixels(bw, 0, iw - bw, bh, pf, empty, 0, 0);
            }
            if (bh < ih) {
                pw.setPixels(0, bh, iw, ih - bh, pf, empty, 0, 0);
            }
        }
    }
    if (wimg == null) {
        wimg = new WritableImage(bw, bh);
    }
    PixelWriter pw = wimg.getPixelWriter();
    IntegerComponentRaster icr = (IntegerComponentRaster) bimg.getRaster();
    int data[] = icr.getDataStorage();
    int offset = icr.getDataOffset(0);
    int scan = icr.getScanlineStride();
    PixelFormat<IntBuffer> pf = (bimg.isAlphaPremultiplied() ? PixelFormat.getIntArgbPreInstance()
            : PixelFormat.getIntArgbInstance());
    pw.setPixels(0, 0, bw, bh, pf, data, offset, scan);
    return wimg;
}

From source file:Main.java

public static BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint,
        boolean higherQuality) {
    int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage ret = img;//from   w w w  .j  av  a2  s .com
    int w, h;
    if (higherQuality) {
        // Use multi-step technique: start with original size, then
        // scale down in multiple passes with drawImage()
        // until the target size is reached
        w = img.getWidth();
        h = img.getHeight();
    } else {
        // Use one-step technique: scale directly from original
        // size to target size with a single drawImage() call
        w = targetWidth;
        h = targetHeight;
    }

    do {
        if (higherQuality && w > targetWidth) {
            w /= 2;
            if (w < targetWidth) {
                w = targetWidth;
            }
        }

        if (higherQuality && h > targetHeight) {
            h /= 2;
            if (h < targetHeight) {
                h = targetHeight;
            }
        }

        BufferedImage tmp = new BufferedImage(w, h, type);
        Graphics2D g2 = tmp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
        g2.drawImage(ret, 0, 0, w, h, null);
        g2.dispose();

        ret = tmp;
    } while (w != targetWidth || h != targetHeight);

    return ret;
}

From source file:org.web4thejob.web.util.MediaUtil.java

public static BufferedImage createThumbnail(byte[] bytes) {
    Image image = getImage(bytes);
    BufferedImage bufferedImage = new BufferedImage(image.getWidth(), image.getHeight(),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = bufferedImage.createGraphics();
    g.drawImage(image.toImageIcon().getImage(), 0, 0, null);
    g.dispose();
    return createThumbnail(bufferedImage);
}

From source file:de.dfki.owlsmx.gui.util.Converter.java

public static void convertToPdf(JFreeChart chart, int width, int height, String filename) {
    // step 1       
    Document document = new Document(new Rectangle(width, height));
    try {// ww  w.j a v  a2  s.co m
        // step 2
        PdfWriter writer;
        writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate tp = cb.createTemplate(width, height);
        Graphics2D g2d = tp.createGraphics(width, height, new DefaultFontMapper());
        Rectangle2D r2d = new Rectangle2D.Double(0, 0, width, height);
        chart.draw(g2d, r2d);
        g2d.dispose();
        cb.addTemplate(tp, 0, 0);
    } catch (DocumentException de) {

    } catch (FileNotFoundException e) {

    }
    // step 5
    document.close();
}

From source file:net.pickapack.chart.ChartPdfExporter.java

/**
 * Export the specified chart to the specified PDF file.
 *
 * @param chart the chart/*from  w w  w  .  ja  v  a  2 s. c  o  m*/
 * @param width the width of the PDF file
 * @param height the height of the PDF file
 * @param fileName the PDF file name
 */
public static void exportPdf(JFreeChart chart, int width, int height, String fileName) {
    try {
        Document document = new Document(new Rectangle(width, height));
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate tp = cb.createTemplate(width, height);
        Graphics2D g2d = tp.createGraphics(width, height, new DefaultFontMapper());
        Rectangle2D r2d = new Rectangle2D.Double(0, 0, width, height);
        chart.draw(g2d, r2d);
        g2d.dispose();
        cb.addTemplate(tp, 0, 0);
        document.close();
    } catch (DocumentException e) {
        throw new RuntimeException(e);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    }
}