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.geomajas.plugin.rasterizing.layer.RasterDirectLayer.java

private BufferedImage makeOpaque(BufferedImage image) {
    if (image.getType() == BufferedImage.TYPE_CUSTOM) {
        log.warn("makeOpaque {} Unknown Image Type 0: ", getTitle());
        return image;
    }/*w  w w. java  2  s.  c o  m*/
    BufferedImage opaqueCopy = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
    Graphics2D g1 = opaqueCopy.createGraphics();
    g1.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getOpacity()));
    g1.drawImage(image, null, 0, 0);
    g1.dispose();
    return opaqueCopy;
}

From source file:org.shredzone.commons.captcha.impl.DefaultCaptchaGenerator.java

/**
 * Draws a single character./*from w  w w .j a  va 2s  . c  om*/
 *
 * @param g2d
 *            {@link Graphics2D} context
 * @param ch
 *            character to draw
 * @param x
 *            left x position of the character
 * @param boxWidth
 *            width of the box
 */
private void drawCharacter(Graphics2D g2d, char ch, int x, int boxWidth) {
    double degree = (rnd.nextDouble() * rotationAmplitude * 2) - rotationAmplitude;
    double scale = 1 - (rnd.nextDouble() * scaleAmplitude / 100);

    Graphics2D cg2d = (Graphics2D) g2d.create();
    cg2d.setFont(font.deriveFont(fontSize));

    cg2d.translate(x + (boxWidth / 2), height / 2);
    cg2d.rotate(degree * PI / 90);
    cg2d.scale(scale, scale);

    FontMetrics fm = cg2d.getFontMetrics();
    int charWidth = fm.charWidth(ch);
    int charHeight = fm.getAscent() + fm.getDescent();

    cg2d.drawString(String.valueOf(ch), -(charWidth / 2), fm.getAscent() - (charHeight / 2));

    cg2d.dispose();
}

From source file:org.openscience.cdk.applications.taverna.basicutilities.ChartTool.java

/**
 * Adds a chart to the pdf file.//from   w w w.j  a v  a 2  s .  c  om
 * 
 * @param chart
 * @param cb
 */
private void addChartPageToPDF(JFreeChart chart, PdfContentByte cb) {
    PdfTemplate tp = cb.createTemplate(this.width, this.height);
    Graphics2D g2 = tp.createGraphics(this.width, this.height, new DefaultFontMapper());
    Rectangle2D r2D = new Rectangle2D.Double(0, 0, this.width, this.height);
    chart.draw(g2, r2D);
    g2.dispose();
    cb.addTemplate(tp, 0, 0);
}

From source file:gui.GraphsPanel.java

/** Save Graph as JPEG.
 * // w  w  w .j  a  va2s  .com
 */
public void writeJpegImage(File file) {
    int width = vv.getWidth();
    int height = vv.getHeight();

    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = bi.createGraphics();
    vv.paint(graphics);
    graphics.dispose();

    try {
        ImageIO.write(bi, "jpeg", file);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:de.tor.tribes.ui.algo.TimeFrameVisualizer.java

/** Creates new form TimeFrameVisualizer */
public TimeFrameVisualizer() {
    initComponents();/*from  ww  w. j a  v  a 2s  .c  om*/
    try {
        STROKED = new BufferedImage(3, 3, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = STROKED.createGraphics();
        g2d.setColor(Constants.DS_BACK_LIGHT);
        g2d.fillRect(0, 0, 3, 3);
        g2d.setColor(Constants.DS_BACK);
        g2d.drawLine(0, 2, 2, 0);
        g2d.dispose();
        DAILY_START_FRAME_FILL = new BufferedImage(3, 3, BufferedImage.TYPE_INT_RGB);
        g2d = DAILY_START_FRAME_FILL.createGraphics();
        g2d.setColor(Color.CYAN);
        g2d.fillRect(0, 0, 3, 3);
        g2d.setColor(Color.BLUE);
        g2d.drawLine(0, 2, 2, 0);
        g2d.dispose();
        EXACT_START_FRAME_FILL = new BufferedImage(3, 3, BufferedImage.TYPE_INT_RGB);
        g2d = EXACT_START_FRAME_FILL.createGraphics();
        g2d.setColor(Color.CYAN);
        g2d.fillRect(0, 0, 3, 3);
        g2d.dispose();
        ONE_DAY_START_FRAME_FILL = new BufferedImage(3, 3, BufferedImage.TYPE_INT_RGB);
        g2d = ONE_DAY_START_FRAME_FILL.createGraphics();
        g2d.setColor(Color.CYAN);
        g2d.fillRect(0, 0, 3, 3);
        g2d.setColor(Color.BLUE);
        g2d.drawLine(1, 0, 1, 2);
        g2d.dispose();
        ARRIVE_FRAME_FILL = new BufferedImage(3, 3, BufferedImage.TYPE_INT_RGB);
        g2d = ARRIVE_FRAME_FILL.createGraphics();
        g2d.setColor(Color.RED);
        g2d.fillRect(0, 0, 3, 3);
        g2d.setColor(Color.BLACK);
        g2d.drawLine(0, 2, 2, 0);
        g2d.dispose();
    } catch (Exception e) {
    }

    popupInfo = new HashMap<String, Object>();
    addMouseMotionListener(new MouseMotionListener() {

        @Override
        public void mouseDragged(MouseEvent e) {
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            repaint();
        }
    });

    addMouseListener(new MouseListener() {

        @Override
        public void mouseClicked(MouseEvent e) {
            fireClickEvent(e);
        }

        @Override
        public void mousePressed(MouseEvent e) {
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }
    });
}

From source file:edu.ku.brc.specify.utilapps.ERDTable.java

/**
 * Returns the BufferedImage of a background shadow. I creates a large rectangle than the orignal image.
 * @return Returns the BufferedImage of a background shadow. I creates a large rectangle than the orignal image.
 *///www. j av a  2s  .c  om
private BufferedImage getBackgroundImageBuffer() {
    if (shadowBuffer == null) {
        ShadowFactory factory = new ShadowFactory(SHADOW_SIZE, 0.17f, Color.BLACK);

        Dimension size = inner.getSize();
        BufferedImage image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB);

        Graphics2D g2 = image.createGraphics();
        g2.setColor(Color.WHITE);
        g2.fillRect(0, 0, image.getWidth(), image.getHeight());
        g2.dispose();

        shadowBuffer = factory.createShadow(image);
    }
    return shadowBuffer;
}

From source file:ca.sqlpower.wabit.swingui.chart.WabitJFreeChartPanel.java

@Override
public void paintComponent(Graphics g) {

    if (getChart() == null) {
        return;/*from  w  w w  .  ja v a  2s . co m*/
    }

    Graphics2D g2 = (Graphics2D) g.create();

    float baseline = getXaxisBaseline();

    ChartGradientPainter.paintChartGradient(g2, getBounds(), (int) baseline);

    g2.setColor(getForeground());
    super.paintComponent(g2);

    // this rendering has a different layout than last time;
    // have to paint again to update gradient position
    if (Math.abs(baseline - getXaxisBaseline()) > 1f) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                repaint();
            }
        });
    }
    g2.dispose();
}

From source file:FieldValidator.java

@Override
public void paint(Graphics g, JComponent c) {
    super.paint(g, c);

    JLayer jlayer = (JLayer) c;
    JFormattedTextField ftf = (JFormattedTextField) jlayer.getView();
    if (!ftf.isEditValid()) {
        Graphics2D g2 = (Graphics2D) g.create();

        // Paint the red X.
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        int w = c.getWidth();
        int h = c.getHeight();
        int s = 8;
        int pad = 4;
        int x = w - pad - s;
        int y = (h - s) / 2;
        g2.setPaint(Color.red);/*w w  w . j a v  a  2  s .  co m*/
        g2.fillRect(x, y, s + 1, s + 1);
        g2.setPaint(Color.white);
        g2.drawLine(x, y, x + s, y + s);
        g2.drawLine(x, y + s, x + s, y);

        g2.dispose();
    }
}

From source file:edu.stanford.epad.epadws.handlers.dicom.DSOUtil.java

private static BufferedImage imageToBufferedImage(Image image) {
    BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bufferedImage.createGraphics();
    g2.drawImage(image, 0, 0, null);//from   www  .j  a  v  a 2s .  com
    g2.dispose();
    return bufferedImage;
}

From source file:com.igormaznitsa.mindmap.swing.panel.utils.ScalableIcon.java

public synchronized Image getImage(final double scale) {
    if (Double.compare(this.currentScaleFactor, scale) != 0) {
        this.scaledCachedImage = null;
    }//from  w  w w  .  ja va2 s  .  c  o  m

    if (this.scaledCachedImage == null) {
        this.currentScaleFactor = scale;

        final int imgw = this.baseImage.getWidth(null);
        final int imgh = this.baseImage.getHeight(null);
        final int scaledW = (int) Math.round(imgw * this.baseScaleX * scale);
        final int scaledH = (int) Math.round(imgh * this.baseScaleY * scale);

        final BufferedImage img = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_INT_ARGB);
        final Graphics2D g = (Graphics2D) img.getGraphics();

        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
                RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);

        g.drawImage(this.baseImage, 0, 0, scaledW, scaledH, null);
        g.dispose();

        this.scaledCachedImage = img;
    }
    return this.scaledCachedImage;
}