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:com.actelion.research.orbit.imageAnalysis.utils.ImageUtils.java

/**
 * Rescales using arbitrary intensities and converts to 8bit. Works for gray- and rgb color images.
 *
 * @param bi16//from w  w  w.  j a  va  2s.  co m
 * @return
 */
public static BufferedImage convertTo8bit(BufferedImage bi16, double intScalingMin, double intScalignMax) {
    // TODO: use min,max from plate meta data, not image specific
    int[][] minMax = ImageUtils.getMinMaxIntensitiesOfBI(bi16);
    BufferedImage bi = null;
    if (bi16.getSampleModel().getNumBands() == 1) {
        bi16 = ImageUtils.scaleIntensities(bi16, getPercentileIntensity(bi16, intScalingMin),
                getPercentileIntensity(bi16, intScalignMax));
        bi = new BufferedImage(bi16.getWidth(), bi16.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
    } else if (bi16.getSampleModel().getNumBands() == 3) {
        bi16 = ImageUtils.scaleIntensities(bi16, getPercentileIntensity(bi16, intScalingMin),
                getPercentileIntensity(bi16, intScalignMax));
        bi = new BufferedImage(bi16.getWidth(), bi16.getHeight(), BufferedImage.TYPE_INT_RGB);
    } else {
        throw new IllegalArgumentException(
                "Only images with 1 band (gray-color) or 3 bands (rgb) supported. This image has "
                        + bi16.getSampleModel().getNumBands() + " bands.");
    }
    Graphics2D g2d = bi.createGraphics();
    g2d.drawImage(bi16, 0, 0, null);
    g2d.dispose();
    return bi;
}

From source file:Main.java

public static void draw(BufferedImage imageBG, BufferedImage imageFG) {
    Ellipse2D.Double ellipse1 = new Ellipse2D.Double(20, 20, 30, 30);
    Ellipse2D.Double ellipse2 = new Ellipse2D.Double(25, 25, 30, 30);
    Area circle = new Area(ellipse1);
    circle.subtract(new Area(ellipse2));

    Graphics2D g = imageBG.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
    g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g.setClip(circle);//from   w  w w .jav  a2 s  .c o  m
    g.drawImage(imageFG, 0, 0, null);
    g.setClip(null);
    Stroke s = new BasicStroke(2);
    g.setStroke(s);
    g.setColor(Color.BLACK);
    g.draw(circle);
    g.dispose();

    JLabel l = new JLabel(new ImageIcon(imageBG));
    JOptionPane.showMessageDialog(null, l);
}

From source file:com.actelion.research.orbit.imageAnalysis.utils.ImageUtils.java

/**
 * Rescales using plate intensities and converts to 8bit. Works for gray- and rgb color images.
 *
 * @param bi16//from   w w w.  j  ava 2  s.c om
 * @return
 */
public static BufferedImage convertTo8bit(int rdfId, BufferedImage bi16, PlateScalingMin plateScalingMin,
        PlateScalingMax plateScalingMax) throws Exception {
    RawDataFile rdf = DALConfig.getImageProvider().LoadRawDataFile(rdfId);
    /*
    if (plateScalingMin==defaultPlateScalingMin && plateScalingMax==defaultPlateScalingMax) {
        // /orbitvol1/2014-11/4309324.1002.jpg
        String url = RawUtils.STR_SERVER+"/rawFile?rawFile="+rdf.getDataPath()+"/"+rdfId+"."+RawUtils.LEVEL_8BITPREVIEW+".jpg";
        System.out.println("url: " + url);
        return ImageIO.read(new URL(url));
    }
    */

    RawData rd = DALConfig.getImageProvider().LoadRawData(rdf.getRawDataId());
    List<RawMeta> rmList = DALConfig.getImageProvider().LoadRawMetasByRawDataFile(rdfId);
    List<RawMeta> rmDataList = DALConfig.getImageProvider().LoadRawMetasByRawData(rd.getRawDataId());
    rmList.addAll(rmDataList);
    HashMap<String, RawMeta> rmHash = RawUtilsCommon.getHashFromMetaList(rmList);

    if (!rmHash.containsKey(RawUtilsCommon.STR_META_CHANNEL))
        throw new Exception("Error: Meta data 'Channel' not available for RawDataFile " + rdfId);
    //int channel = Integer.parseInt(rmHash.get(RawUtils.STR_META_CHANNEL).getValue()) - 1;
    int channel = Integer.parseInt(rmHash.get(RawUtilsCommon.STR_META_CHANNEL).getValue());
    String metaKey = "Percentiles_wvlength_" + channel + "_channel_0";
    if (!rmHash.containsKey(metaKey))
        throw new Exception("Error: Meta data '" + metaKey + "' not available for RawDataFile " + rdfId);
    String val = rmHash.get(metaKey).getValue();
    int[] minmax = parseMinMax(val, plateScalingMin, plateScalingMax);

    BufferedImage bi = null;
    if (bi16.getSampleModel().getNumBands() == 1) {
        bi16 = ImageUtils.scaleIntensities(bi16, new int[] { minmax[0] }, new int[] { minmax[1] });
        bi = new BufferedImage(bi16.getWidth(), bi16.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
    } else {
        throw new IllegalArgumentException("Only images with 1 band (gray-color) supported. This image has "
                + bi16.getSampleModel().getNumBands() + " bands.");
    }
    Graphics2D g2d = bi.createGraphics();
    g2d.drawImage(bi16, 0, 0, null);
    g2d.dispose();
    return bi;
}

From source file:Main.java

public static BufferedImage createRotatedTextImage(String text, int angle, Font ft) {
    Graphics2D g2d = null;
    try {/*from  w  ww. ja v  a  2  s  . c o m*/
        if (text == null || text.trim().length() == 0) {
            return null;
        }

        BufferedImage stringImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);

        g2d = (Graphics2D) stringImage.getGraphics();
        g2d.setFont(ft);

        FontMetrics fm = g2d.getFontMetrics();
        Rectangle2D bounds = fm.getStringBounds(text, g2d);

        TextLayout tl = new TextLayout(text, ft, g2d.getFontRenderContext());

        g2d.dispose();
        g2d = null;

        return createRotatedImage(tl, (int) bounds.getWidth(), (int) bounds.getHeight(), angle);
    } catch (Exception e) {
        e.printStackTrace();

        if (g2d != null) {
            g2d.dispose();
        }
    }

    return null;
}

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);/*from   w w w . jav a2 s .  co 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:be.fedict.eid.applet.maven.DocbookMojo.java

private static void graphToFile(BasicVisualizationServer<String, String> visualization, File file)
        throws IOException {
    Dimension size = visualization.getSize();
    int width = (int) (size.getWidth() + 1);
    int height = (int) (size.getHeight() + 1);
    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = bufferedImage.createGraphics();
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics.setColor(Color.WHITE);
    graphics.fillRect(0, 0, 900, 650);//  w w  w  . ja  v  a2 s. co m
    visualization.setBounds(0, 0, 900, 650);
    visualization.paint(graphics);
    graphics.dispose();
    ImageIO.write(bufferedImage, "png", file);
}

From source file:Main.java

private static BufferedImage renderRotatedObject(Object src, double angle, int width, int height, double tx,
        double ty) {
    BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2d = (Graphics2D) dest.getGraphics();
    g2d.setColor(Color.black);// ww  w. j  a  va  2  s .c o m
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    AffineTransform at = AffineTransform.getRotateInstance(angle);
    at.translate(tx, ty);
    g2d.setTransform(at);

    if (src instanceof TextLayout) {
        TextLayout tl = (TextLayout) src;
        tl.draw(g2d, 0, tl.getAscent());
    } else if (src instanceof Image) {
        g2d.drawImage((Image) src, 0, 0, null);
    }
    g2d.dispose();

    return dest;
}

From source file:net.rptools.lib.image.ImageUtil.java

/**
 * Create a copy of the image that is compatible with the current graphics context and scaled to the supplied size
 *//*  ww w  .  j  ava  2s.  c  o m*/
public static BufferedImage createCompatibleImage(Image img, int width, int height, Map<String, Object> hints) {
    width = Math.max(width, 1);
    height = Math.max(height, 1);

    int transparency;
    if (hints != null && hints.containsKey(HINT_TRANSPARENCY)) {
        transparency = (Integer) hints.get(HINT_TRANSPARENCY);
    } else {
        transparency = pickBestTransparency(img);
    }
    BufferedImage compImg = new BufferedImage(width, height, transparency);

    Graphics2D g = null;
    try {
        g = compImg.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

        g.drawImage(img, 0, 0, width, height, null);
    } finally {
        if (g != null) {
            g.dispose();
        }
    }
    return compImg;
}

From source file:de.xirp.chart.ChartUtil.java

/**
 * Exports the given chart as PDF in the specified size. The
 * export is written to the given path./*from  ww w  . j  av a2s  .  c  om*/
 * 
 * @param chart
 *            The chart to export.
 * @param width
 *            The desired width of the PDF.
 * @param height
 *            The desired height of the PDF.
 * @param path
 *            The path to write the PDF to.
 * @see org.jfree.chart.JFreeChart
 */
private static void exportPDF(JFreeChart chart, int width, int height, String path) {
    Document document = new Document(new Rectangle(width, height));
    try {
        PdfWriter writer;
        writer = PdfWriter.getInstance(document, new FileOutputStream(path));
        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);
    } catch (Exception e) {
        logClass.error("Error: " + e.getMessage() //$NON-NLS-1$
                + Constants.LINE_SEPARATOR, e);
    }
    document.close();
}

From source file:com.t3.image.ImageUtil.java

/**
 * Create a copy of the image that is compatible with the current graphics context
 * and scaled to the supplied size/*from w  ww .j  a va  2  s  .  c  o  m*/
 */
public static BufferedImage createCompatibleImage(Image img, int width, int height, Map<String, Object> hints) {

    width = Math.max(width, 1);
    height = Math.max(height, 1);

    int transparency;
    if (hints != null && hints.containsKey(HINT_TRANSPARENCY)) {
        transparency = (Integer) hints.get(HINT_TRANSPARENCY);
    } else {
        transparency = pickBestTransparency(img);
    }
    BufferedImage compImg = new BufferedImage(width, height, transparency);

    Graphics2D g = null;
    try {
        g = compImg.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

        g.drawImage(img, 0, 0, width, height, null);
    } finally {
        if (g != null) {
            g.dispose();
        }
    }

    return compImg;
}