Example usage for java.awt Graphics dispose

List of usage examples for java.awt Graphics dispose

Introduction

In this page you can find the example usage for java.awt Graphics 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:zz.pseas.ghost.utils.BrowserUtil.java

public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }/*from   w w  w  . j  a v a 2 s. com*/
    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
        int transparency = Transparency.OPAQUE;
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
        // The system does not have a screen
    }
    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }
    // Copy image to buffered image
    Graphics g = bimage.createGraphics();
    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();
    return bimage;
}

From source file:Main.java

/**
 * Takes a snapshot of the target component.
 *
 * @param component the component to draw
 * @param usePrint  whether <tt>print()</tt> or <tt>paint()</tt> is used to grab the snapshot
 * @return a Graphics compatible image of the component
 *//*from w  w w . j av a 2s  .  c o  m*/
public static Image takeSnapshot(Component component, boolean usePrint) {
    BufferedImage image = null;
    GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = genv.getDefaultScreenDevice();
    GraphicsConfiguration gc = gd.getDefaultConfiguration();

    if (gc.getColorModel().hasAlpha()) {
        image = gc.createCompatibleImage((int) component.getSize().getWidth(),
                (int) component.getSize().getHeight());
    } else {
        image = new BufferedImage((int) component.getSize().getWidth(), (int) component.getSize().getHeight(),
                BufferedImage.TYPE_INT_ARGB);
    }

    Graphics g = image.getGraphics();
    if (usePrint) {
        component.print(g);
    } else {
        component.paint(g);
    }
    g.dispose();

    return image;
}

From source file:Main.java

public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }/*from w  ww . j  ava  2s  .c o m*/

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels; for this method's
    // implementation, see Determining If an Image Has Transparent Pixels
    boolean hasAlpha = true;

    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;
        if (hasAlpha) {
            transparency = Transparency.BITMASK;
        }

        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
        // The system does not have a screen
    }

    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;
        if (hasAlpha) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }

    // Copy image to buffered image
    Graphics g = bimage.createGraphics();

    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bimage;
}

From source file:tourma.utils.web.WebStatistics.java

/**
 * /*  w  w  w  .java 2s  .c  o  m*/
 */
public static String getHTML() {
    StringBuffer stats = new StringBuffer("");

    JPNStatistics jpn = new JPNStatistics();
    jpn.setSize(640, 480);
    JTabbedPane jtp = jpn.getTabbedPane();
    for (int i = 0; i < jtp.getTabCount(); i++) {
        Component comp = jtp.getComponent(i);

        if (comp instanceof ChartPanel) {
            ChartPanel panel = (ChartPanel) comp;
            panel.setSize(640, 480);
            BufferedImage buf = new BufferedImage(640, 480, BufferedImage.TYPE_INT_ARGB);
            Graphics g = buf.createGraphics();
            panel.print(g);
            g.dispose();

            //BufferedImage buf = toBufferedImage(img, 640, 480);
            String img_str = WebPicture.getPictureAsHTML(buf, 640, 480, true);
            stats.append(img_str);
        }

        if (comp instanceof JPanel) {
            // Find JList, Select All then Find ChartPanel
            JPanel pane = (JPanel) comp;
            ChartPanel panel = null;
            JList list = null;
            for (int j = 0; j < pane.getComponentCount(); j++) {
                Component c = pane.getComponent(j);
                if (c instanceof JScrollPane) {
                    for (int k = 0; k < ((JScrollPane) c).getViewport().getComponentCount(); k++) {
                        Component c2 = ((JScrollPane) c).getViewport().getComponent(k);
                        if (c2 instanceof JList) {
                            list = (JList) c2;
                        }

                    }
                }
            }

            if (list != null) {

                int start = 0;
                int end = list.getModel().getSize() - 1;
                if (end >= 0) {
                    list.setSelectionInterval(start, end);
                }

                jpn.updatePositions();
            }
            for (int j = 0; j < pane.getComponentCount(); j++) {
                Component c = pane.getComponent(j);
                if (c instanceof ChartPanel) {
                    panel = (ChartPanel) c;
                }
            }

            if (panel != null) {
                panel.setSize(640, 480);
                BufferedImage buf = new BufferedImage(640, 480, BufferedImage.TYPE_INT_ARGB);
                Graphics g = buf.createGraphics();
                panel.print(g);
                g.dispose();

                //BufferedImage buf = toBufferedImage(img, 640, 480);
                String img_str = WebPicture.getPictureAsHTML(buf, 640, 480, true);
                stats.append(img_str);
            }
        }
    }

    return stats.toString();
}

From source file:ImageUtilities.java

private static BufferedImage convertToARGB(BufferedImage srcImage) {
    BufferedImage newImage = new BufferedImage(srcImage.getWidth(null), srcImage.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics bg = newImage.getGraphics();
    bg.drawImage(srcImage, 0, 0, null);//from  w w w .ja va  2s .  co  m
    bg.dispose();
    return newImage;
}

From source file:net.duckling.ddl.util.ImageUtils.java

/**
 * ???//  www .j  av a  2  s  .c  om
 * @param tmpFilePath ?
 * @return
 */
public static boolean scare(String tmpFilePath) {
    try {
        BufferedImage src = ImageIO.read(new File(tmpFilePath)); // 
        int width = src.getWidth();
        int height = src.getHeight();
        if (width > DEFAULT_WIDTH) {
            height = (DEFAULT_WIDTH * height) / width;
            width = DEFAULT_WIDTH;
        }
        Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);
        BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = tag.getGraphics();
        g.drawImage(image, 0, 0, null); // ??
        g.dispose();
        File resultFile = new File(tmpFilePath);
        ImageIO.write(tag, "JPEG", resultFile);// ?
        return true;
    } catch (IOException e) {
        LOG.error(e);
    }
    return false;
}

From source file:ImageUtil.java

/**
 * Posted by alpha02 at http://www.dreamincode.net/code/snippet1076.htm
 *///from   w  ww  .  j  a  va2s.  c om
public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage)
        return (BufferedImage) image;

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels
    boolean hasAlpha = hasAlpha(image);

    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;

        if (hasAlpha == true)
            transparency = Transparency.BITMASK;

        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();

        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
    } //No screen

    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;

        if (hasAlpha == true) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }

    // Copy image to buffered image
    Graphics g = bimage.createGraphics();

    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bimage;
}

From source file:Main.java

/**
 * This method returns a buffered image with the contents of an image
 * This snippet was taken from: http://www.exampledepot.com/egs/java.awt.image/Image2Buf.html
 * @param image//from   ww  w.j  av  a2  s  . c  o  m
 * @return The buffered image
 */
public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels; for this method's
    // implementation, see e661 Determining If an Image Has Transparent Pixels
    boolean hasAlpha = hasAlpha(image);

    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;
        if (hasAlpha) {
            transparency = Transparency.BITMASK;
        }

        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
        // The system does not have a screen
    }

    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;
        if (hasAlpha) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }

    // Copy image to buffered image
    Graphics g = bimage.createGraphics();

    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bimage;
}

From source file:SWTUtils.java

/**
 * Converts an AWT image to SWT.//from   w w w.  ja  v a 2  s.  com
 *
 * @param image  the image (<code>null</code> not permitted).
 *
 * @return Image data.
 */
public static ImageData convertAWTImageToSWT(Image image) {
    if (image == null) {
        throw new IllegalArgumentException("Null 'image' argument.");
    }
    int w = image.getWidth(null);
    int h = image.getHeight(null);
    if (w == -1 || h == -1) {
        return null;
    }
    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();
    g.drawImage(image, 0, 0, null);
    g.dispose();
    return convertToSWT(bi);
}

From source file:MainClass.java

static void printDimensions(Graphics g, Dimension size) {
    int width = size.width;
    int height = size.height;
    int x1 = (int) (width * 0.1);
    int x2 = (int) (width * 0.9);
    int y1 = (int) (height * 0.1);
    int y2 = (int) (height * 0.9);
    g.drawRect(x1, y1, x2 - x1, y2 - y1);
    g.dispose();
}