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:com.frostwire.gui.library.LibraryCoverArt.java

private void setPrivateImage(Image image) {
    coverArtImage = image;// ww  w .  j a  v  a2s .  co  m

    if (coverArtImage == null) {
        coverArtImage = defaultCoverArt;
    }

    Graphics2D g2 = background.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);

    g2.setBackground(new Color(255, 255, 255, 0));
    g2.clearRect(0, 0, getWidth(), getHeight());

    g2.drawImage(coverArtImage, 0, 0, getWidth(), getHeight(), null);
    g2.dispose();

    repaint();
    getToolkit().sync();
}

From source file:Main.java

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

    g2d.setColor(Color.RED);//from   ww w . j  av a  2  s  . co m
    g2d.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight());
    g2d.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2);

    Font font = new Font("Arial", Font.BOLD, 48);
    g2d.setFont(font);
    FontMetrics fm = g2d.getFontMetrics();
    int x = ((getWidth() - fm.stringWidth(text)) / 2);
    int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();

    g2d.setColor(Color.BLACK);
    g2d.drawString(text, x, y);

    g2d.dispose();
}

From source file:LineGraphDrawable.java

/**
 * Draws the bar-graph into the given Graphics2D context in the given area.
 * This method will not draw a graph if the data given is null or empty.
 * /*from w w w. j a v  a2  s  . c  o  m*/
 * @param graphics
 *          the graphics context on which the bargraph should be rendered.
 * @param drawArea
 *          the area on which the bargraph should be drawn.
 */
public void draw(final Graphics2D graphics, final Rectangle2D drawArea) {
    if (graphics == null) {
        throw new NullPointerException();
    }
    if (drawArea == null) {
        throw new NullPointerException();
    }

    final int height = (int) drawArea.getHeight();
    if (height <= 0) {
        return;
    }

    final Graphics2D g2 = (Graphics2D) graphics.create();
    if (background != null) {
        g2.setPaint(background);
        g2.draw(drawArea);
    }

    if (data == null || data.length == 0) {
        g2.dispose();
        return;
    }

    g2.translate(drawArea.getX(), drawArea.getY());

    float d = getDivisor(data, height);
    final int spacing = getSpacing();
    final int w = (((int) drawArea.getWidth()) - (spacing * (data.length - 1))) / (data.length - 1);

    float min = Float.MAX_VALUE;
    for (int index = 0; index < data.length; index++) {
        Number i = data[index];
        if (i == null) {
            continue;
        }
        final float value = i.floatValue();
        if (value < min) {
            min = value;
        }
    }

    int x = 0;
    int y = -1;

    if (d == 0.0) {
        // special case -- a horizontal straight line
        d = 1.0f;
        y = -height / 2;
    }

    final Line2D.Double line = new Line2D.Double();
    for (int i = 0; i < data.length - 1; i++) {
        final int px1 = x;
        x += (w + spacing);
        final int px2 = x;

        g2.setPaint(color);

        final Number number = data[i];
        final Number nextNumber = data[i + 1];
        if (number == null && nextNumber == null) {
            final float delta = height - ((0 - min) / d);
            line.setLine(px1, y + delta, px2, y + delta);
        } else if (number == null) {
            line.setLine(px1, y + (height - ((0 - min) / d)), px2,
                    y + (height - ((nextNumber.floatValue() - min) / d)));
        } else if (nextNumber == null) {
            line.setLine(px1, y + (height - ((number.floatValue() - min) / d)), px2,
                    y + (height - ((0 - min) / d)));
        } else {
            line.setLine(px1, y + (height - ((number.floatValue() - min) / d)), px2,
                    y + (height - ((nextNumber.floatValue() - min) / d)));
        }
        g2.draw(line);

    }

    g2.dispose();

}

From source file:com.igormaznitsa.jhexed.values.HexSVGImageValue.java

@Override
public BufferedImage makeIcon(final int width, final int height, final Path2D shape, final boolean allowAlpha) {
    try {//w  w w  .  j  ava 2  s  .c om
        final BufferedImage img = this.image.rasterize(width, height, BufferedImage.TYPE_INT_ARGB);
        if (shape == null) {
            return img;
        } else {
            final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            final Graphics2D g = result.createGraphics();
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
                    RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
            g.setClip(makeTransformedPathForSize(width, height, shape));
            g.drawImage(img, 0, 0, null);
            g.dispose();
            return result;
        }

    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:de.inren.service.picture.PictureModificationServiceImpl.java

private BufferedImage scaleImage(File orginal, final int max) throws IOException {
    // Load the image.
    BufferedImage originalImage = ImageIO.read(orginal);

    // Figure out the new dimensions.
    final int w = originalImage.getWidth();
    final int h = originalImage.getHeight();
    final double maxOriginal = Math.max(w, h);
    final double scaling = max / maxOriginal;

    final int newW = (int) Math.round(scaling * w);
    final int newH = (int) Math.round(scaling * h);

    // If we need to scale down by more than 2x, scale to double the
    // eventual size and then scale again. This provides much higher
    // quality results.
    if (scaling < 0.5f) {
        final BufferedImage newImg = new BufferedImage(newW * 2, newH * 2, BufferedImage.TYPE_INT_RGB);
        final Graphics2D gfx = newImg.createGraphics();
        gfx.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        gfx.drawImage(originalImage, 0, 0, newW * 2, newH * 2, null);
        gfx.dispose();
        newImg.flush();/*from w  ww  .  j  av  a 2 s .  c o m*/
        originalImage = newImg;
    }

    // Scale it.
    BufferedImage newImg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_RGB);
    final Graphics2D gfx = newImg.createGraphics();
    gfx.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    gfx.drawImage(originalImage, 0, 0, newW, newH, null);
    gfx.dispose();
    newImg.flush();
    originalImage.flush();

    return newImg;
}

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

/**
 * Creates an {@link Image} for the given rule.
 * //from  w w w .  ja  v  a 2  s.c  o  m
 * @param rule the rule for which to create the image.
 * @param width the image width.
 * @param height the image height.
 * @return the generated image.
 */
public static BufferedImage polygonRuleToImage(final Rule rule, int width, int height) {
    DuplicatingStyleVisitor copyStyle = new DuplicatingStyleVisitor();
    rule.accept(copyStyle);
    Rule newRule = (Rule) copyStyle.getCopy();

    Stroke stroke = null;
    Symbolizer[] symbolizers = newRule.getSymbolizers();
    if (symbolizers.length > 0) {
        Symbolizer symbolizer = symbolizers[0];
        if (symbolizer instanceof PolygonSymbolizer) {
            PolygonSymbolizer polygonSymbolizer = (PolygonSymbolizer) symbolizer;
            stroke = SLDs.stroke(polygonSymbolizer);
        }
    }
    int strokeSize = 0;
    if (stroke != null) {
        strokeSize = SLDs.width(stroke);
        if (strokeSize < 0) {
            strokeSize = 0;
            stroke.setWidth(ff.literal(strokeSize));
        }
    }

    // pointSize = width;
    BufferedImage finalImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    // Polygon polygon = d.polygon(new int[]{40,30, 60,70, 30,130, 130,130, 130,30});

    int[] xy = new int[] { (int) (height * 0.15), (int) (width * 0.20), (int) (height * 0.4),
            (int) (width * 0.3), (int) (height * 0.85), (int) (width * 0.15), (int) (height * 0.85),
            (int) (width * 0.85), (int) (height * 0.15), (int) (width * 0.85) };
    Polygon polygon = d.polygon(xy);
    d.drawDirect(finalImage, d.feature(polygon), newRule);
    Graphics2D g2d = finalImage.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.drawImage(finalImage, 0, 0, null);
    g2d.dispose();

    return finalImage;
}

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

/**
 * Creates an {@link Image} for the given rule.
 * //from w w w.jav  a2s. c om
 * @param rule the rule for which to create the image.
 * @param width the image width.
 * @param height the image height.
 * @return the generated image.
 */
public static BufferedImage lineRuleToImage(final Rule rule, int width, int height) {
    DuplicatingStyleVisitor copyStyle = new DuplicatingStyleVisitor();
    rule.accept(copyStyle);
    Rule newRule = (Rule) copyStyle.getCopy();

    Stroke stroke = null;
    Symbolizer[] symbolizers = newRule.getSymbolizers();
    if (symbolizers.length > 0) {
        Symbolizer symbolizer = newRule.getSymbolizers()[0];
        if (symbolizer instanceof LineSymbolizer) {
            LineSymbolizer lineSymbolizer = (LineSymbolizer) symbolizer;
            stroke = SLDs.stroke(lineSymbolizer);
        }
    }

    int strokeSize = 0;
    if (stroke != null) {
        strokeSize = SLDs.width(stroke);
        if (strokeSize < 0) {
            strokeSize = 0;
            stroke.setWidth(ff.literal(strokeSize));
        }
    }

    // pointSize = width;
    BufferedImage finalImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    // Polygon polygon = d.polygon(new int[]{40,30, 60,70, 30,130, 130,130, 130,30});

    int[] xy = new int[] { (int) (height * 0.15), (int) (width * 0.85), (int) (height * 0.35),
            (int) (width * 0.15), (int) (height * 0.75), (int) (width * 0.85), (int) (height * 0.85),
            (int) (width * 0.15) };
    LineString line = d.line(xy);
    d.drawDirect(finalImage, d.feature(line), newRule);
    Graphics2D g2d = finalImage.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.drawImage(finalImage, 0, 0, null);
    g2d.dispose();

    return finalImage;
}

From source file:editeurpanovisu.ReadWriteImage.java

/**
 *
 * @param img/*ww  w .ja  v a 2s.  c  o  m*/
 * @param destFile
 * @param sharpen
 * @param sharpenLevel
 * @throws IOException
 */
public static void writePng(Image img, String destFile, boolean sharpen, float sharpenLevel)
        throws IOException {
    sharpenMatrix = calculeSharpenMatrix(sharpenLevel);
    BufferedImage imageRGBSharpen = null;
    IIOImage iioImage = null;
    BufferedImage image = SwingFXUtils.fromFXImage(img, null); // Get buffered image.
    BufferedImage imageRGB = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.BITMASK);
    Graphics2D graphics = imageRGB.createGraphics();
    graphics.drawImage(image, 0, 0, null);
    if (sharpen) {
        imageRGBSharpen = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
        Kernel kernel = new Kernel(3, 3, sharpenMatrix);
        ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
        cop.filter(imageRGB, imageRGBSharpen);
    }
    ImageWriter writer = null;
    FileImageOutputStream output = null;
    try {
        writer = ImageIO.getImageWritersByFormatName("png").next();
        ImageWriteParam param = writer.getDefaultWriteParam();
        output = new FileImageOutputStream(new File(destFile));
        writer.setOutput(output);
        if (sharpen) {
            iioImage = new IIOImage(imageRGBSharpen, null, null);
        } else {
            iioImage = new IIOImage(imageRGB, null, null);
        }
        writer.write(null, iioImage, param);
    } catch (IOException ex) {
        throw ex;
    } finally {
        if (writer != null) {
            writer.dispose();
        }
        if (output != null) {
            output.close();
        }
    }
    graphics.dispose();
}

From source file:editeurpanovisu.ReadWriteImage.java

/**
 *
 * @param img/*  www .  jav  a  2 s .c om*/
 * @param destFile
 * @param sharpen
 * @param sharpenLevel
 * @throws IOException
 */
public static void writeBMP(Image img, String destFile, boolean sharpen, float sharpenLevel)
        throws IOException {
    sharpenMatrix = calculeSharpenMatrix(sharpenLevel);
    BufferedImage imageRGBSharpen = null;
    IIOImage iioImage = null;

    BufferedImage image = SwingFXUtils.fromFXImage(img, null); // Get buffered image.
    BufferedImage imageRGB = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.OPAQUE); // Remove alpha-channel from buffered image.

    Graphics2D graphics = imageRGB.createGraphics();
    graphics.drawImage(image, 0, 0, null);
    if (sharpen) {

        imageRGBSharpen = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
        Kernel kernel = new Kernel(3, 3, sharpenMatrix);
        ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
        cop.filter(imageRGB, imageRGBSharpen);
    }
    ImageWriter writer = null;
    FileImageOutputStream output = null;
    try {
        writer = ImageIO.getImageWritersByFormatName("bmp").next();
        ImageWriteParam param = writer.getDefaultWriteParam();
        output = new FileImageOutputStream(new File(destFile));
        writer.setOutput(output);
        if (sharpen) {
            iioImage = new IIOImage(imageRGBSharpen, null, null);
        } else {
            iioImage = new IIOImage(imageRGB, null, null);
        }
        writer.write(null, iioImage, param);
    } catch (IOException ex) {
        throw ex;
    } finally {
        if (writer != null) {
            writer.dispose();
        }
        if (output != null) {
            output.close();
        }
    }
    graphics.dispose();
}

From source file:TexturedPanel.java

/**
 * Creates a new TexturePaint using the provided colors.
 *///  www .  j ava  2s.  c  om
private void setupDefaultPainter(Color foreground, Color background) {
    if (foreground == null || background == null) {
        ourPainter = null;
        return;
    }

    BufferedImage buff = new BufferedImage(6, 6, BufferedImage.TYPE_INT_ARGB_PRE);

    Graphics2D g2 = buff.createGraphics();

    g2.setColor(background);
    g2.fillRect(0, 0, 6, 6);

    g2.setColor(foreground);
    g2.drawLine(0, 2, 6, 2);
    g2.drawLine(0, 5, 6, 5);

    ourPainter = new TexturePaint(buff, new Rectangle(0, 0, 6, 6));

    g2.dispose();
}