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

public void paintToPDF(JTextPane ta) {
    try {/*from ww  w.j  av a2 s .  c o m*/
        ta.setBounds(0, 0, (int) convertToPixels(612 - 58), (int) convertToPixels(792 - 60));

        Document document = new Document();
        FileOutputStream fos = new FileOutputStream("2.pdf");
        PdfWriter writer = PdfWriter.getInstance(document, fos);

        document.setPageSize(new com.lowagie.text.Rectangle(612, 792));
        document.open();
        PdfContentByte cb = writer.getDirectContent();

        cb.saveState();
        cb.concatCTM(1, 0, 0, 1, 0, 0);

        DefaultFontMapper mapper = new DefaultFontMapper();
        mapper.insertDirectory("c:/windows/fonts");

        Graphics2D g2 = cb.createGraphics(612, 792, mapper, true, .95f);

        AffineTransform at = new AffineTransform();
        at.translate(convertToPixels(20), convertToPixels(20));
        at.scale(pixelToPoint, pixelToPoint);

        g2.transform(at);

        g2.setColor(Color.WHITE);
        g2.fill(ta.getBounds());

        Rectangle alloc = getVisibleEditorRect(ta);
        ta.getUI().getRootView(ta).paint(g2, alloc);

        g2.setColor(Color.BLACK);
        g2.draw(ta.getBounds());

        g2.dispose();
        cb.restoreState();
        document.close();
        fos.flush();
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

private void saveImage() {
    LayerViewAwtPanel.logger.debug("record"); //$NON-NLS-1$
    Color bg = this.getBackground();
    BufferedImage image = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = image.createGraphics();
    graphics.setColor(bg);/* ww  w  .  j a v a2 s  . co  m*/
    graphics.fillRect(0, 0, this.getWidth(), this.getHeight());
    this.getRenderingManager().copyTo(graphics);
    this.recording = false;
    // this.paintOverlays(graphics);
    graphics.dispose();
    try {
        NumberFormat format = NumberFormat.getInstance();
        format.setMinimumIntegerDigits(3);
        ImgUtil.saveImage(image, this.recordFileName + format.format(this.recordIndex) + ".png"); //$NON-NLS-1$
        this.recordIndex++;
    } catch (IOException e1) {
        e1.printStackTrace();
    }
}

From source file:eu.udig.style.advanced.utils.Utilities.java

/**
 * Creates an image from a set of {@link RuleWrapper}s.
 * /*from  w  ww.j  a va2 s.c o m*/
 * @param ruleWrapperList the list of rule wrappers.
 * @param width the image width.
 * @param height the image height.
 * @return the new created {@link BufferedImage}.
 */
public static BufferedImage pointRulesWrapperToImage(final List<RuleWrapper> ruleWrapperList, int width,
        int height) {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = image.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    for (RuleWrapper ruleWrapper : ruleWrapperList) {
        BufferedImage tmpImage = Utilities.pointRuleWrapperToImage(ruleWrapper, width, height);
        g2d.drawImage(tmpImage, 0, 0, null);
    }
    g2d.dispose();
    return image;
}

From source file:IconDemoApp.java

public void paintIcon(Component c, Graphics g, int x, int y) {
    Graphics2D g2d = (Graphics2D) g.create();

    g2d.setColor(Color.WHITE);// w  w  w. j ava2 s. com
    g2d.fillRect(x + 1, y + 1, width - 2, height - 2);

    g2d.setColor(Color.BLACK);
    g2d.drawRect(x + 1, y + 1, width - 2, height - 2);

    g2d.setColor(Color.RED);

    g2d.setStroke(stroke);
    g2d.drawLine(x + 10, y + 10, x + width - 10, y + height - 10);
    g2d.drawLine(x + 10, y + height - 10, x + width - 10, y + 10);

    g2d.dispose();
}

From source file:edu.fullerton.viewerplugin.PluginSupport.java

public void saveImageAsPdfFile(JFreeChart chart, String filename) throws WebUtilException {
    try {//from   ww w  .  j a  v  a 2  s .com
        OutputStream out = new BufferedOutputStream(new FileOutputStream(filename));
        Rectangle pagesize = new Rectangle(width, height);
        com.itextpdf.text.Document document;
        document = new com.itextpdf.text.Document(pagesize, 50, 50, 50, 50);
        try {
            PdfWriter writer = PdfWriter.getInstance(document, out);
            FontMapper mapper = new DefaultFontMapper();
            document.addAuthor("JFreeChart");
            document.addSubject("Demonstration");
            document.open();
            PdfContentByte cb = writer.getDirectContent();
            PdfTemplate tp = cb.createTemplate(width, height);
            Graphics2D g2 = tp.createGraphics(width, height, mapper);
            Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height);
            chart.draw(g2, r2D);
            g2.dispose();
            cb.addTemplate(tp, 0, 0);
        } catch (DocumentException de) {
            throw new WebUtilException("Saving as pdf", de);
        }
        document.close();
    } catch (FileNotFoundException ex) {
        throw new WebUtilException("Saving plot as pdf: ", ex);
    }

}

From source file:piramide.interaction.reasoner.FuzzyReasonerWizardFacade.java

private BufferedImage createErrorMessagesImage(String text) {
    final Font font = TextTitle.DEFAULT_FONT;
    final Font bigBold = new Font(font.getName(), Font.BOLD, 24);
    final FontRenderContext frc = new FontRenderContext(null, true, false);
    final TextLayout layout = new TextLayout(text, bigBold, frc);
    final Rectangle2D bounds = layout.getBounds();
    final int w = (int) Math.ceil(bounds.getWidth());
    final int h = (int) Math.ceil(bounds.getHeight());
    final BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    final Graphics2D g = image.createGraphics();
    g.setColor(Color.WHITE);/*from  w  w w. j a  v a 2 s  .c  om*/
    g.fillRect(0, 0, w, h);
    g.setColor(Color.RED);
    g.setFont(bigBold);
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
    g.drawString(text, (float) -bounds.getX(), (float) -bounds.getY());
    g.dispose();
    return image;
}

From source file:common.utils.ImageUtils.java

/**
  * resize input image to new dinesions(only smaller) into rez parameter
  * @param image input image for scaling
 * @param rez resulting image. must have required width and height
 * @throws IOException/*from w  w  w . ja va 2 s . co  m*/
  */
public static void getScaledImageDimmension(BufferedImage image, BufferedImage rez) throws IOException {
    Graphics2D g2 = rez.createGraphics();
    if (rez.getHeight() > image.getHeight() || rez.getWidth() > image.getWidth()) {
        //rez image is bigger no resize
        g2.drawImage(image, 0, 0, null);
        return;
    }
    //1-st getting first side to resize (width or height)
    double scale_factor;
    if (getScaling(image.getHeight(), rez.getHeight()) > getScaling(image.getWidth(), rez.getWidth())) {
        //resize height
        scale_factor = getScaling(image.getHeight(), rez.getHeight());
        int width = (int) (scale_factor * image.getWidth());
        //cut width
        int x = (rez.getWidth() - width) / 2;
        g2.drawImage(image.getScaledInstance(width, rez.getHeight(), Image.SCALE_SMOOTH), x, 0, null);
        //System.out.println("resizing height: h="+image.getHeight()+"/"+rez.getHeight()+"; x="+x);
    } else {
        //resize width
        scale_factor = getScaling(image.getWidth(), rez.getWidth());
        int height = (int) (scale_factor * image.getHeight());
        //cut height
        int y = (rez.getHeight() - height) / 2;
        g2.drawImage(image.getScaledInstance(rez.getWidth(), height, Image.SCALE_SMOOTH), 0, y, null);
        //System.out.println("resizing width: w="+image.getWidth()+"/"+rez.getWidth()+"; y="+y);
    }
    g2.dispose();
}

From source file:Main.java

@Override
protected void paintThumb(Graphics g, JComponent c, Rectangle r) {
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Color color = null;//from   www .j a  v a  2 s.c om
    JScrollBar sb = (JScrollBar) c;
    if (!sb.isEnabled() || r.width > r.height) {
        return;
    } else if (isDragging) {
        color = Color.DARK_GRAY;
    } else if (isThumbRollover()) {
        color = Color.LIGHT_GRAY;
    } else {
        color = Color.GRAY;
    }
    g2.setPaint(color);
    g2.fillRoundRect(r.x, r.y, r.width, r.height, 10, 10);
    g2.setPaint(Color.WHITE);
    g2.drawRoundRect(r.x, r.y, r.width, r.height, 10, 10);
    g2.dispose();
}

From source file:net.daimonin.client3d.editor.main.Editor3D.java

/**
* Builds a single PNG out of all ImageSetImages, considering their calculated
* coordinates.//from   www.  j  av  a 2s  . c  om
* 
* @param fileNameImageSet Name of resulting PNG.
* @param dimension [width, height] of the resulting PNG. where 0 is maximum
*          compression, 1 is no compression at all.
* @throws IOException IOException.
*/
private static void writeImageSet(final String fileNameImageSet, final int[] dimension) throws IOException {

    BufferedImage bigImg = new BufferedImage(dimension[0], dimension[1], BufferedImage.TYPE_INT_ARGB);
    Graphics2D big = bigImg.createGraphics();
    for (int i = 0; i < images.size(); i++) {
        if (images.get(i).getBorderSize() > 0) {
            ParameterBlock params = new ParameterBlock();
            params.addSource(images.get(i).getImage());
            params.add(images.get(i).getBorderSize()); // left pad
            params.add(images.get(i).getBorderSize()); // right pad
            params.add(images.get(i).getBorderSize()); // top pad
            params.add(images.get(i).getBorderSize()); // bottom pad
            params.add(new BorderExtenderConstant(new double[] { images.get(i).getBorderColor().getRed(),
                    images.get(i).getBorderColor().getGreen(), images.get(i).getBorderColor().getBlue(),
                    BORDERCOLORMAX }));

            big.drawImage(JAI.create("border", params).getAsBufferedImage(), images.get(i).getPosX(),
                    images.get(i).getPosY(), null);

        } else {
            big.drawImage(images.get(i).getImage(), images.get(i).getPosX(), images.get(i).getPosY(), null);
        }
    }

    big.dispose();
    ImageIO.write(bigImg, "png", new File(fileNameImageSet));
    printInfo(System.getProperty("user.dir") + "/" + imageset + " created");
}

From source file:RotationAboutCenter.java

protected void paintComponent(Graphics g) {
    Graphics2D g2d;
    g2d = (Graphics2D) g.create();

    // Erase background to white
    g2d.setColor(Color.WHITE);/*from w  w w .j a va 2 s.c o m*/
    g2d.fillRect(0, 0, getWidth(), getHeight());

    // base rectangle
    g2d.setColor(Color.GRAY.brighter());
    g2d.fillRect(50, 50, 50, 50);

    // rotated 45 degrees around origin
    g2d.rotate(Math.toRadians(45));
    g2d.setColor(Color.GRAY.darker());
    g2d.fillRect(50, 50, 50, 50);

    // rotated 45 degrees about center of rect
    g2d = (Graphics2D) g.create();
    g2d.rotate(Math.toRadians(45), 75, 75);
    g2d.setColor(Color.BLACK);
    g2d.fillRect(50, 50, 50, 50);

    // done with g2d, dispose it
    g2d.dispose();
}