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

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    String text = "I don't see the problem";
    FontMetrics fm = g2d.getFontMetrics();
    int x = (getWidth() - fm.stringWidth(text)) / 2;
    int y = ((getHeight() - fm.getHeight()) / 2) + fm.getDescent();
    g2d.setTransform(AffineTransform.getRotateInstance(Math.toRadians(45), getWidth() / 2, getHeight() / 2));
    g2d.drawString(text, x, y);/* w  ww . j ava  2 s  .c  o m*/
    g2d.dispose();
}

From source file:main.MapKit.java

private BufferedImage convert(BufferedImage loadImg, Color newColor) {
    int w = loadImg.getWidth();
    int h = loadImg.getHeight();
    BufferedImage imgOut = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    BufferedImage imgColor = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g = imgColor.createGraphics();
    g.setColor(newColor);/* w ww  .  j a v  a2  s.c o m*/
    g.fillRect(0, 0, w + 1, h + 1);
    g.dispose();

    Graphics2D graphics = imgOut.createGraphics();
    graphics.drawImage(loadImg, 0, 0, null);
    graphics.setComposite(MultiplyComposite.Default);
    graphics.drawImage(imgColor, 0, 0, null);
    graphics.dispose();

    return imgOut;
}

From source file:edu.csun.ecs.cs.multitouchj.application.whiteboard.ui.InteractiveCanvas.java

public void clearCanvas(Color color) {
    synchronized (resizableBufferedImage) {
        BufferedImage bufferedImage = resizableBufferedImage.getBufferedImage();

        Graphics2D graphics = bufferedImage.createGraphics();
        graphics.setPaint(color);/*from   w w w .j  a v  a 2 s . c o m*/
        graphics.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());
        graphics.dispose();

        setDirty(true);
    }
}

From source file:net.atomique.ksar.export.FilePDF.java

private void addchart(PdfWriter writer, Graph graph) {
    JFreeChart chart = graph.getgraph(mysar.myparser.getStartOfGraph(), mysar.myparser.getEndOfGraph());
    PdfTemplate pdftpl = pdfcb.createTemplate(pagewidth, pageheight);
    Graphics2D g2d = new PdfGraphics2D(pdftpl, pagewidth, pageheight);
    Double r2d = new Rectangle2D.Double(0, 0, pagewidth, pageheight);
    chart.draw(g2d, r2d);/*from   w  w  w  .  j  av a  2s.c  o m*/
    g2d.dispose();
    pdfcb.addTemplate(pdftpl, pdfmargins, pdfmargins);
    try {
        writer.releaseTemplate(pdftpl);
    } catch (IOException ioe) {
        log.error("Unable to write to : {}", pdffilename);
    }
}

From source file:com.salesmanager.core.util.ProductImageUtil.java

private BufferedImage createCompatibleImage(BufferedImage image) {
    GraphicsConfiguration gc = BufferedImageGraphicsConfig.getConfig(image);
    int w = image.getWidth();
    int h = image.getHeight();
    BufferedImage result = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
    Graphics2D g2 = result.createGraphics();
    g2.drawRenderedImage(image, null);/*  w ww . j  av a  2 s  .c o m*/
    g2.dispose();
    return result;
}

From source file:ancat.visualizers.UndirectedModelVisualizer.java

private void produce(String fileName) {
    _currentLayout.setSize(new Dimension(600, 600));

    VisualizationImageServer<V, E> vv = new VisualizationImageServer<V, E>(_currentLayout,
            new Dimension(600, 600));

    vv.setPreferredSize(new Dimension(600, 600));

    // Setup up a new vertex to paint transformer...
    Transformer<V, Paint> vertexPaint = new Transformer<V, Paint>() {
        public Paint transform(V v) {
            //return Color.GREEN;
            ancat.common.Vertex vertex = (Vertex) v;

            if (vertex instanceof ancat.common.ExecutableFile)
                return Color.BLUE;

            if (vertex instanceof ancat.common.DynamicLibrary)
                return Color.YELLOW;

            if (vertex instanceof ancat.common.ConfigurationFile)
                return Color.ORANGE;

            if (vertex instanceof ancat.common.Unspecified)
                return Color.RED;

            return Color.darkGray;
        }/*from ww  w  .  j a  va  2 s . co  m*/
    };
    // Set up a new stroke Transformer for the edges
    float dash[] = { 10.0f };

    final Stroke edgeStroke = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash,
            0.0f);

    Transformer<E, Stroke> edgeStrokeTransformer = new Transformer<E, Stroke>() {
        public Stroke transform(E s) {
            return edgeStroke;
        }
    };

    Transformer<E, Font> edgeFontTransformer = new Transformer<E, Font>() {
        public Font transform(E e) {
            return new Font("Dialog", Font.PLAIN, 10);
        }
    };

    Transformer<E, String> lblTransformer = new Transformer<E, String>() {
        public String transform(E e) {
            Edge edge = (Edge) e;
            //return edge.getXMLId();
            return edge.getXMLId() != null ? edge.getXMLId() : "";
        }
    };

    Transformer<V, Font> vertexFontTransformer = new Transformer<V, Font>() {
        public Font transform(V v) {
            return new Font("Dialog", Font.PLAIN, 10);
        }
    };

    Transformer<V, String> vLblTransformer = new Transformer<V, String>() {
        public String transform(V v) {
            Vertex vertex = (Vertex) v;
            return vertex.getElementID();
        }
    };

    vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
    vv.getRenderContext().setEdgeStrokeTransformer(edgeStrokeTransformer);
    vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<V>());
    vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller<E>());
    // ---->
    vv.getRenderContext().setEdgeFontTransformer(edgeFontTransformer);
    vv.getRenderContext().setLabelOffset(20);
    vv.getRenderContext().setEdgeLabelTransformer(lblTransformer);

    vv.getRenderContext().setVertexFontTransformer(vertexFontTransformer);
    vv.getRenderContext().setVertexLabelTransformer(vLblTransformer);

    vv.getRenderer().getVertexLabelRenderer().setPosition(Position.N);

    BufferedImage img = new BufferedImage((int) _currentLayout.getSize().getWidth(),
            (int) _currentLayout.getSize().getHeight(), BufferedImage.TYPE_INT_ARGB);

    vv.getImage(new Point(0, 0), new Dimension(700, 700));

    Graphics2D g2d = (Graphics2D) img.getGraphics();

    vv.paint(g2d);

    setDisclaimer(g2d);

    g2d.dispose();

    try {
        File file = new File(fileName);

        ImageIO.write(img, "png", file);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.igormaznitsa.jhexed.renders.svg.SVGImage.java

public BufferedImage rasterize(final int width, final int height, final int imageType) throws IOException {
    final BufferedImage result = new BufferedImage(width, height, imageType);
    final float xfactor = (float) width / getSVGWidth();
    final float yfactor = (float) height / getSVGHeight();

    final Graphics2D g = result.createGraphics();

    processAntialias(this.quality, g);
    g.setTransform(AffineTransform.getScaleInstance(xfactor, yfactor));
    this.svgGraphicsNode.primitivePaint(g);

    g.dispose();
    return result;
}

From source file:br.prof.salesfilho.oci.image.ImageProcessor.java

public BufferedImage resize(int newWidth, int newHeigth) {
    Image tmp = image.getScaledInstance(newWidth, newHeigth, Image.SCALE_SMOOTH);
    BufferedImage dimg = new BufferedImage(newWidth, newHeigth, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = dimg.createGraphics();
    g2d.drawImage(tmp, 0, 0, null);//from  w  ww.  ja  v a2s . com
    g2d.dispose();
    this.image = dimg;
    return this.image;
}

From source file:com.datamyne.charts.AlmostThereDemo.java

/**
 * Creates PDf file./*w w w . j  av  a2s  .  c o  m*/
 * @param outputStream {@link OutputStream}.
 * @throws DocumentException
 * @throws IOException
 */
public void create(OutputStream outputStream) throws DocumentException, IOException {
    Document document = null;
    PdfWriter writer = null;

    try {
        //instantiate document and writer
        document = new Document();
        writer = PdfWriter.getInstance(document, outputStream);

        //open document
        document.open();

        //add image
        int width = 300;
        int height = 300;
        JFreeChart chart = getChart();

        //create PdfContentByte
        //if you work with this object, you write to
        //the top most layer, meaning anything behind
        //will be clipped
        PdfContentByte contentByte = writer.getDirectContent();
        //create PdfTemplate from PdfContentByte
        PdfTemplate template = contentByte.createTemplate(width, height);
        //create Graphics2D from PdfTemplate
        Graphics2D g2 = template.createGraphics(width, height, new DefaultFontMapper());
        //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, null);
        g2.dispose(); //always dispose this
        //add the PdfTemplate to the PdfContentByte
        contentByte.addTemplate(template, 0, 300);

        //release resources
        document.close();
        document = null;

        writer.close();
        writer = null;
    } catch (DocumentException de) {
        throw de;
    } finally {
        //release resources
        if (null != document) {
            try {
                document.close();
            } catch (Exception ex) {
            }
        }

        if (null != writer) {
            try {
                writer.close();
            } catch (Exception ex) {
            }
        }
    }
}

From source file:bachelorthesis.ocr.domain.DomainFacade.java

private Captcha createCaptcha(String buildString, int width, int height) throws RuntimeException {
    try {// w  w w.  j a va 2s  .c o m
        CaptchaBuilder captchaBuilder = new CaptchaBuilder(40, 50, buildString);
        Captcha c = captchaBuilder.buildCaptcha();
        BufferedImage img = c.getImage();

        // check if size == the default size (40*50) if not scale
        if (width != 40 || height != 50) {
            BufferedImage resized = new BufferedImage(width, height, img.getType());
            Graphics2D g = resized.createGraphics();
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g.drawImage(img, 0, 0, width, height, 0, 0, img.getWidth(), img.getHeight(), null);
            g.dispose();

            //build new CAPTCHA WITH THE NEW IMAGE SIZE
            c = new Captcha(c.getBuildSequence(), c.getAnswer(), c.isCaseSensative(), img, new Date());
        }

        return c;

    } catch (ParseException ex) {
        Logger.getLogger(DomainFacade.class.getName()).log(Level.SEVERE, null, ex);
        throw new RuntimeException("error creating CAPTCHA");
    }
}