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.tsho.dmc2.core.chart.jfree.DmcChartPanel.java

/**
 * Handles a 'mouse released' event./*from  www  .j  av  a 2  s.  c o m*/
 * <P>
 * On Windows, we need to check if this is a popup trigger, but only if we
 * haven't already been tracking a zoom rectangle.
 *
 * @param e  Information about the event.
 */
public void mouseReleased(MouseEvent e) {

    if (zoomRectangle != null) {

        //            if (Math.abs(e.getX() - zoomPoint.getX()) >= MINIMUM_DRAG_ZOOM_SIZE) {
        if (Math.abs(e.getX() - zoomPoint.getX()) >= 7) {
            if (e.getX() < zoomPoint.getX() || e.getY() < zoomPoint.getY()) {
                autoRangeBoth();
            } else {
                double x, y, w, h;
                Rectangle2D scaledDataArea = getScaledDataArea();
                //for a mouseReleased event, (horizontalZoom || verticalZoom)
                //will be true, so we can just test for either being false;
                //otherwise both are true
                if (!verticalZoom) {
                    x = zoomPoint.getX();
                    y = scaledDataArea.getMinY();
                    w = Math.min(zoomRectangle.getWidth(), scaledDataArea.getMaxX() - zoomPoint.getX());
                    h = scaledDataArea.getHeight();
                } else if (!horizontalZoom) {
                    x = scaledDataArea.getMinX();
                    y = zoomPoint.getY();
                    w = scaledDataArea.getWidth();
                    h = Math.min(zoomRectangle.getHeight(), scaledDataArea.getMaxY() - zoomPoint.getY());
                } else {
                    x = zoomPoint.getX();
                    y = zoomPoint.getY();
                    w = Math.min(zoomRectangle.getWidth(), scaledDataArea.getMaxX() - zoomPoint.getX());
                    h = Math.min(zoomRectangle.getHeight(), scaledDataArea.getMaxY() - zoomPoint.getY());
                }
                Rectangle2D zoomArea = new Rectangle2D.Double(x, y, w, h);
                zoom(zoomArea);

            }
            this.zoomPoint = null;
            this.zoomRectangle = null;
        } else {
            Graphics2D g2 = (Graphics2D) getGraphics();
            g2.setXORMode(java.awt.Color.gray);
            if (fillZoomRectangle) {
                g2.fill(zoomRectangle);
            } else {
                g2.draw(zoomRectangle);
            }
            g2.dispose();
            this.zoomRectangle = null;
        }

        // notify a redraw event
        CoreStatusEvent ev = new CoreStatusEvent(this);
        ev.setType(CoreStatusEvent.REDRAW);
        ((AbstractDmcPlot) chart.getPlot()).notifyCoreStatusListeners(ev);
    }

    else if (e.isPopupTrigger()) {
        if (popup != null) {
            displayPopupMenu(e.getX(), e.getY());
        }
    }
}

From source file:PictureScaler.java

/**
 * Convenience method that returns a scaled instance of the
 * provided BufferedImage./*  ww  w. j  a va2s .  c  o m*/
 * 
 * 
 * @param img the original image to be scaled
 * @param targetWidth the desired width of the scaled instance,
 *    in pixels
 * @param targetHeight the desired height of the scaled instance,
 *    in pixels
 * @param hint one of the rendering hints that corresponds to
 *    RenderingHints.KEY_INTERPOLATION (e.g.
 *    RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR,
 *    RenderingHints.VALUE_INTERPOLATION_BILINEAR,
 *    RenderingHints.VALUE_INTERPOLATION_BICUBIC)
 * @param progressiveBilinear if true, this method will use a multi-step
 *    scaling technique that provides higher quality than the usual
 *    one-step technique (only useful in down-scaling cases, where
 *    targetWidth or targetHeight is
 *    smaller than the original dimensions)
 * @return a scaled version of the original BufferedImage
 */
public BufferedImage getFasterScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint,
        boolean progressiveBilinear) {
    int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage ret = img;
    BufferedImage scratchImage = null;
    Graphics2D g2 = null;
    int w, h;
    int prevW = ret.getWidth();
    int prevH = ret.getHeight();
    boolean isTranslucent = img.getTransparency() != Transparency.OPAQUE;

    if (progressiveBilinear) {
        // 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 (progressiveBilinear && w > targetWidth) {
            w /= 2;
            if (w < targetWidth) {
                w = targetWidth;
            }
        }

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

        if (scratchImage == null || isTranslucent) {
            // Use a single scratch buffer for all iterations
            // and then copy to the final, correctly-sized image
            // before returning
            scratchImage = new BufferedImage(w, h, type);
            g2 = scratchImage.createGraphics();
        }
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
        g2.drawImage(ret, 0, 0, w, h, 0, 0, prevW, prevH, null);
        prevW = w;
        prevH = h;

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

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

    // If we used a scratch buffer that is larger than our target size,
    // create an image of the right size and copy the results into it
    if (targetWidth != ret.getWidth() || targetHeight != ret.getHeight()) {
        scratchImage = new BufferedImage(targetWidth, targetHeight, type);
        g2 = scratchImage.createGraphics();
        g2.drawImage(ret, 0, 0, null);
        g2.dispose();
        ret = scratchImage;
    }

    return ret;
}

From source file:fr.ign.cogit.geoxygene.appli.layer.LayerViewAwtPanel.java

public void saveAsImage(String fileName, int width, int height, boolean doSaveWorldFile, boolean drawOverlay) {
    Color bg = this.getBackground();
    BufferedImage outImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = outImage.createGraphics();
    // TEMP//ww w .jav a2s . c  o  m
    graphics.setColor(bg);
    graphics.fillRect(0, 0, width, height);
    int tmpw = this.getWidth();
    int tmph = this.getHeight();
    // We save the old extent to force the Viewport to keep the old
    // window in world coordinates.
    IEnvelope env = this.getViewport().getEnvelopeInModelCoordinates();
    // Artificially resize the canvas to the image dimensions.
    this.setSize(width, height);
    try {
        // We zoom to the old extent in the resized canvas.
        this.getViewport().zoom(env);
    } catch (NoninvertibleTransformException e2) {
        logger.error("In Image Export : failed to zoom in the correct extent.");
        e2.printStackTrace();
    }
    this.renderingManager.renderAll();
    long time = System.currentTimeMillis();
    long twaited = 0;
    while (this.renderingManager.isRendering() && twaited < 15000) {
        // Wait for the rendering to end for a maximum of 15s. If the
        // rendering is not finished after this delay,
        // we give up.
        twaited = System.currentTimeMillis() - time;
    }
    if (this.renderingManager.isRendering()) {
        logger.error("Export to image : waited 15s but the rendering is still not finished. Abort.");
        return;
    }

    // We have to impose a bbox !!!
    this.getRenderingManager().copyTo(graphics);

    if (drawOverlay)
        this.paintOverlays(graphics);
    graphics.dispose();
    try {
        ImgUtil.saveImage(outImage, fileName);
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    if (doSaveWorldFile) {
        String wld = FilenameUtils.removeExtension(fileName) + ".wld";
        try {
            AffineTransform t = this.getViewport().getModelToViewTransform();
            fr.ign.cogit.geoxygene.util.conversion.WorldFileWriter.write(new File(wld), t.getScaleX(),
                    t.getScaleY(), this.getViewport().getViewOrigin().getX(),
                    this.getViewport().getViewOrigin().getY(), this.getHeight());
        } catch (NoninvertibleTransformException e) {
            logger.error("Failed to save the world file associated with the image file " + fileName);
            e.printStackTrace();
        }
    }

    // Finally, rollback the canvas to its original size.
    this.setSize(tmpw, tmph);
    try {
        // Zoom back to the "normal" extent
        this.getViewport().zoom(env);
    } catch (NoninvertibleTransformException e2) {
        logger.error("In Image Export : failed to zoom back to the original LayerViewPanel extent.");
        e2.printStackTrace();
        return;
    }
}

From source file:org.eurocarbdb.application.glycoworkbench.plugin.SpectraPanel.java

public void mouseDragged(MouseEvent e) {
    if (mouse_start_point != null && theDocument.getNoScans() > 0) {
        if (is_moving) {
            // moving
            double mz_delta = screenToDataX(mouse_start_point.getX() - e.getPoint().getX());
            if (mz_delta > 0.) {
                double old_upper_bound = thePlot.getDomainAxis().getUpperBound();
                double old_lower_bound = thePlot.getDomainAxis().getLowerBound();
                double new_upper_bound = Math.min(old_upper_bound + mz_delta,
                        theDocument.getPeakDataAt(current_ind).getMaxMZ());
                double new_lower_bound = old_lower_bound + new_upper_bound - old_upper_bound;

                thePlot.getDomainAxis().setRange(new Range(new_lower_bound, new_upper_bound));
            } else {
                double old_upper_bound = thePlot.getDomainAxis().getUpperBound();
                double old_lower_bound = thePlot.getDomainAxis().getLowerBound();
                double new_lower_bound = Math.max(old_lower_bound + mz_delta,
                        theDocument.getPeakDataAt(current_ind).getMinMZ());
                double new_upper_bound = old_upper_bound + new_lower_bound - old_lower_bound;

                thePlot.getDomainAxis().setRange(new Range(new_lower_bound, new_upper_bound));
            }// w w w.j  a v  a2 s. c om

            mouse_start_point = e.getPoint();
        } else {
            // zooming                
            Graphics2D g2 = (Graphics2D) theChartPanel.getGraphics();
            g2.setXORMode(java.awt.Color.gray);

            // delete old rectangle
            if (zoom_rectangle != null)
                g2.draw(zoom_rectangle);

            // create new rectangle
            double start_x = Math.min(e.getX(), mouse_start_point.getX());
            double end_x = Math.max(e.getX(), mouse_start_point.getX());

            Rectangle2D data_area = theChartPanel.getScreenDataArea((int) start_x,
                    (int) mouse_start_point.getY());
            double xmax = Math.min(end_x, data_area.getMaxX());
            zoom_rectangle = new Rectangle2D.Double(start_x, data_area.getMinY(), xmax - start_x,
                    data_area.getHeight());

            // draw new rectangle
            g2.draw(zoom_rectangle);
            g2.dispose();
        }
    }
}

From source file:org.eurocarbdb.application.glycoworkbench.plugin.reporting.AnnotationReportCanvas.java

protected void paintComponent(Graphics g) {

    // prepare graphic object
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    // set clipping area
    if (is_printing) {
        g2d.translate(-draw_area.x, -draw_area.y);
        g2d.setClip(draw_area);//from  www. j a v  a  2 s . c  o  m
    }

    //paint canvas background
    if (!is_printing) {
        g2d.setColor(getBackground());
        g2d.fillRect(0, 0, getWidth(), getHeight());
    }

    // paint white background on drawing area    
    g2d.setColor(Color.white);
    g2d.fillRect(draw_area.x, draw_area.y, draw_area.width, draw_area.height);
    if (!is_printing) {
        g2d.setColor(Color.black);
        g2d.draw(draw_area);
    }

    // paint
    paintChart(g2d);
    paintAnnotations(g2d);

    // dispose graphic object
    g2d.dispose();

    if (!is_printing) {
        if (first_time) {
            if (first_time_init_pos)
                placeStructures(true);
            else
                theDocument.fireDocumentInit();
            first_time = false;
        } else
            revalidate();
    }

}

From source file:lucee.runtime.img.Image.java

public void grayscale() throws ExpressionException {
    BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_BYTE_GRAY);
    Graphics2D graphics = img.createGraphics();
    graphics.drawImage(image(), new AffineTransformOp(AffineTransform.getTranslateInstance(0.0, 0.0), 1), 0, 0);
    graphics.dispose();
    image(img);/*from w w  w . j  a  v  a  2 s.  c  om*/
}

From source file:lucee.runtime.img.Image.java

public void threeBBger() throws ExpressionException {
    BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_3BYTE_BGR);
    Graphics2D graphics = img.createGraphics();
    graphics.drawImage(image(), new AffineTransformOp(AffineTransform.getTranslateInstance(0.0, 0.0), 1), 0, 0);
    graphics.dispose();
    image(img);/*from   w ww .java  2  s  .  c o m*/
}

From source file:lucee.runtime.img.Image.java

public void rgb() throws ExpressionException {
    BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
    Graphics2D graphics = img.createGraphics();
    graphics.drawImage(image(), new AffineTransformOp(AffineTransform.getTranslateInstance(0.0, 0.0), 1), 0, 0);
    graphics.dispose();
    image(img);/* w w w.j a va  2s .c  o  m*/

}

From source file:com.centurylink.mdw.designer.pages.ExportHelper.java

public byte[] printImage(float scale, CanvasCommon canvas, Dimension graphsize, String format)
        throws IOException {
    int h_margin = 72, v_margin = 72;
    BufferedImage image = new BufferedImage(graphsize.width + h_margin, graphsize.height + v_margin,
            BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();
    if (scale > 0)
        g2.scale(scale, scale);//from  w w w  .  ja v  a  2 s.co m
    g2.setBackground(Color.WHITE);
    g2.clearRect(0, 0, image.getWidth(), image.getHeight());
    // canvas.paint(g2);
    Color bgsave = canvas.getBackground();
    boolean edsave = canvas.editable;
    canvas.editable = false;
    canvas.setBackground(Color.white);
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    canvas.paintComponent(g2);
    canvas.setBackground(bgsave);
    canvas.editable = edsave;
    g2.dispose();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, format, baos);
    image = null;
    Runtime r = Runtime.getRuntime();
    r.gc();
    return baos.toByteArray();
}

From source file:lucee.runtime.img.Image.java

public void resizeImage(int width, int height, int interpolation) throws ExpressionException {
    Object ip;//from  ww  w  . j a va  2  s  . co m
    if (interpolation == IPC_NEAREST)
        ip = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
    else if (interpolation == IPC_BICUBIC)
        ip = RenderingHints.VALUE_INTERPOLATION_BICUBIC;
    else if (interpolation == IPC_BILINEAR)
        ip = RenderingHints.VALUE_INTERPOLATION_BILINEAR;
    else
        throw new ExpressionException("invalid interpoltion definition");

    BufferedImage dst = new BufferedImage(width, height, image().getType());
    Graphics2D graphics = dst.createGraphics();
    graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, ip);
    graphics.drawImage(image(), 0, 0, width, height, null);
    graphics.dispose();
    image(dst);

}