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

public void paint(Graphics g) {
    w = getWidth();/*ww w .  j a v a  2 s.  c  o m*/
    h = getHeight();

    if (w <= 0 || h <= 0)
        return;

    Graphics2D g2 = createDemoGraphics2D(g);
    drawDemo(g2);
    g2.dispose();

    if (offImg != null && isShowing()) {
        g.drawImage(offImg, 0, 0, this);
    }

    newBufferedImage = false;
}

From source file:Main.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    String text = getWidth() + "x" + getHeight();
    FontMetrics fm = g2d.getFontMetrics();
    int x = (getWidth() - fm.stringWidth(text)) / 2;
    int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
    g2d.drawString(text, x, y);//from   w w w . ja  v a 2 s.  co m
    g2d.dispose();
}

From source file:com.alibaba.webx.tutorial.app1.module.screen.simple.SayHiImage.java

private void writeImage(OutputStream out) throws IOException {
    BufferedImage img = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = img.createGraphics();

    g2d.setPaint(Color.red);//from  ww w  .  j  av  a2  s . c  om
    g2d.setFont(new Font("Serif", Font.BOLD, 36));
    g2d.drawString("Hi there, how are you doing today?", 5, g2d.getFontMetrics().getHeight());
    g2d.dispose();

    ImageIO.write(img, "jpg", out);
}

From source file:Utils.java

/** 
 * Renders a paragraph of text (line breaks ignored) to an image (created and returned). 
 *
 * @param font The font to use//from   w  ww  .  jav  a 2 s  . c  o m
 * @param textColor The color of the text
 * @param text The message
 * @param width The width the text should be limited to
 * @return An image with the text rendered into it
 */
public static BufferedImage renderTextToImage(Font font, Color textColor, String text, int width) {
    Hashtable map = new Hashtable();
    map.put(TextAttribute.FONT, font);
    AttributedString attributedString = new AttributedString(text, map);
    AttributedCharacterIterator paragraph = attributedString.getIterator();

    FontRenderContext frc = new FontRenderContext(null, false, false);
    int paragraphStart = paragraph.getBeginIndex();
    int paragraphEnd = paragraph.getEndIndex();
    LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(paragraph, frc);

    float drawPosY = 0;

    //First time around, just determine the height
    while (lineMeasurer.getPosition() < paragraphEnd) {
        TextLayout layout = lineMeasurer.nextLayout(width);

        // Move it down
        drawPosY += layout.getAscent() + layout.getDescent() + layout.getLeading();
    }

    BufferedImage image = createCompatibleImage(width, (int) drawPosY);
    Graphics2D graphics = (Graphics2D) image.getGraphics();
    graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

    drawPosY = 0;
    lineMeasurer.setPosition(paragraphStart);
    while (lineMeasurer.getPosition() < paragraphEnd) {
        TextLayout layout = lineMeasurer.nextLayout(width);

        // Move y-coordinate by the ascent of the layout.
        drawPosY += layout.getAscent();

        /* Compute pen x position.  If the paragraph is
           right-to-left, we want to align the TextLayouts
           to the right edge of the panel.
         */
        float drawPosX;
        if (layout.isLeftToRight()) {
            drawPosX = 0;
        } else {
            drawPosX = width - layout.getAdvance();
        }

        // Draw the TextLayout at (drawPosX, drawPosY).
        layout.draw(graphics, drawPosX, drawPosY);

        // Move y-coordinate in preparation for next layout.
        drawPosY += layout.getDescent() + layout.getLeading();
    }

    graphics.dispose();
    return image;
}

From source file:org.geotools.renderer.chart.ChartGraphicFactory.java

BufferedImage drawChart(JFreeChart chart, int w, int h) {
    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);
    Graphics2D gr = bi.createGraphics();
    try {/*from w  ww  .  j av  a  2 s .  c  o m*/
        chart.draw(gr, new Rectangle2D.Double(0, 0, w, h));
    } finally {
        gr.dispose();
    }

    return bi;
}

From source file:Main.java

public void repaint(BufferedImage orig, BufferedImage copy) {
    Graphics2D g = copy.createGraphics();
    g.drawImage(orig, 0, 0, null);//from   ww w.  j  av a 2  s.  c om
    g.setColor(Color.RED);
    if (captureRect == null) {
        return;
    }
    g.draw(captureRect);
    g.setColor(new Color(25, 25, 23, 10));
    g.fill(captureRect);
    g.dispose();
}

From source file:com.tur0kk.facebook.FacebookClient.java

public String publishPicture(String msg, Image image, String placeId) throws IOException {
    OAuthRequest request = new OAuthRequest(Verb.POST, "https://graph.facebook.com/v2.2/me/photos"); // request node
    request.addHeader("Authorization", "Bearer " + accesTokenString); // authentificate

    // check input to avoid error responses
    if (msg != null && image != null) {
        // facebook requires multipart post structure
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("message", msg); // description

        if (placeId != null && !"".equals(placeId)) {
            builder.addTextBody("place", placeId); // add link to FabLab site if property is set in preferences
        }/*w  ww .j a v  a  2  s . co  m*/

        // convert image to bytearray and append to multipart
        BufferedImage bimage = new BufferedImage(image.getWidth(null), image.getHeight(null),
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D bGr = bimage.createGraphics();
        bGr.drawImage(image, 0, 0, null);
        bGr.dispose();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(bimage, "png", baos);
        builder.addBinaryBody(msg, baos.toByteArray(), ContentType.MULTIPART_FORM_DATA, "test.png");

        // generate multipart byte stream and add to payload of post package
        HttpEntity multipart = builder.build();
        ByteArrayOutputStream multipartOutStream = new ByteArrayOutputStream(
                (int) multipart.getContentLength());
        multipart.writeTo(multipartOutStream);
        request.addPayload(multipartOutStream.toByteArray());

        // set header of post package
        Header contentType = multipart.getContentType();
        request.addHeader(contentType.getName(), contentType.getValue());

        // send and response answer
        Response response = request.send();
        return response.getBody();
    } else {
        throw new RuntimeException("message and image needed");
    }
}

From source file:hudson.jbpm.PluginImpl.java

/**
 * Draws a JPEG for a process definition
 * //from  w  w w.  j a v  a 2s .c o  m
 * @param req
 * @param rsp
 * @param processDefinition
 * @throws IOException
 */
public void doProcessDefinitionImage(StaplerRequest req, StaplerResponse rsp,
        @QueryParameter("processDefinition") long processDefinition) throws IOException {

    JbpmContext context = getCurrentJbpmContext();
    ProcessDefinition definition = context.getGraphSession().getProcessDefinition(processDefinition);
    FileDefinition fd = definition.getFileDefinition();
    byte[] bytes = fd.getBytes("processimage.jpg");
    rsp.setContentType("image/jpeg");
    ServletOutputStream output = rsp.getOutputStream();

    BufferedImage loaded = ImageIO.read(new ByteArrayInputStream(bytes));
    BufferedImage aimg = new BufferedImage(loaded.getWidth(), loaded.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D g = aimg.createGraphics();
    g.drawImage(loaded, null, 0, 0);
    g.dispose();
    ImageIO.write(aimg, "jpg", output);
    output.flush();
    output.close();
}

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

public static void writeChartAsPDF(OutputStream outputstream, 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   w  w w .  j  av a2  s  .c  om
        PdfWriter pdfwriter = PdfWriter.getInstance(document, outputstream);
        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:WeeklyReport.Sections.Bookings.java

public Image bookingsByPODImage(PdfWriter writer) throws BadElementException {
    JFreeChart chart = new Bookings().bookingsByPODChart();
    PdfContentByte contentByte = writer.getDirectContent();
    PdfTemplate template = contentByte.createTemplate(600f, 400f);
    Graphics2D graphics2d = template.createGraphics(600f, 400f, new DefaultFontMapper());
    Rectangle2D rectangle2d = new Rectangle2D.Float(10f, 0, 500f, 400f);
    chart.draw(graphics2d, rectangle2d);
    graphics2d.dispose();

    Image chartImage = Image.getInstance(template);
    return chartImage;
}