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:org.polymap.core.data.image.ImageGrayscaleProcessor.java

protected Image grayscale(Image image) {
    long start = System.currentTimeMillis();

    // load image data
    new javax.swing.ImageIcon(image).getImage();

    if (!(image instanceof BufferedImage)) {
        BufferedImage bimage = new BufferedImage(image.getHeight(null), image.getWidth(null),
                BufferedImage.TYPE_4BYTE_ABGR);
        Graphics g = bimage.getGraphics();
        g.drawImage(image, 0, 0, null);//from   ww  w. j av  a  2  s. c  o m
        g.dispose();

        image = bimage;
    }

    // grayscale
    ColorConvertOp filter = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);

    BufferedImage grayImage = new BufferedImage(image.getHeight(null), image.getWidth(null),
            BufferedImage.TYPE_4BYTE_ABGR);

    Graphics g = grayImage.getGraphics();
    filter.filter((BufferedImage) image, grayImage);
    g.dispose();

    log.info("Gray scaling took: " + (System.currentTimeMillis() - start) + "ms");
    return grayImage;
}

From source file:ClipImage.java

public void run() {
    Thread me = Thread.currentThread();
    while (thread == me && isShowing()) {
        Graphics g = getGraphics();
        paint(g);//w  w  w  . jav a 2s .c  o m
        g.dispose();
        thread.yield();
    }
    thread = null;
}

From source file:com.codenvy.corp.MainPage.java

private void addText(File file, String burnDownInfo) throws IOException {
    BufferedImage image = ImageIO.read(file);
    Graphics g = image.getGraphics();
    g.setColor(Color.RED);//from www.j  av  a 2 s  .  c  o  m
    g.setFont(g.getFont().deriveFont(30f));
    g.drawString(burnDownInfo, 550, 45);
    g.dispose();
    ImageIO.write(image, "png", file);
}

From source file:ClipImage.java

public void run() {

    Thread me = Thread.currentThread();

    while (thread == me && isShowing()) {
        Graphics g = getGraphics();
        paint(g);//from   w  w  w.j  a  v a2  s  . c o m
        g.dispose();
        thread.yield();
    }
    thread = null;
}

From source file:org.sejda.sambox.pdmodel.graphics.image.JPEGFactoryTest.java

@Test
public void testCreateFromImageINT_ARGB() throws IOException {
    // workaround Open JDK bug
    // http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7044758
    if (System.getProperty("java.runtime.name").equals("OpenJDK Runtime Environment")
            && (System.getProperty("java.specification.version").equals("1.6")
                    || System.getProperty("java.specification.version").equals("1.7")
                    || System.getProperty("java.specification.version").equals("1.8"))) {
        return;/*from ww  w.  j a v  a  2s  . c o  m*/
    }

    PDDocument document = new PDDocument();
    BufferedImage image = ImageIO.read(JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg"));

    // create an ARGB image
    int width = image.getWidth();
    int height = image.getHeight();
    BufferedImage argbImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics ag = argbImage.getGraphics();
    ag.drawImage(image, 0, 0, null);
    ag.dispose();

    for (int x = 0; x < argbImage.getWidth(); ++x) {
        for (int y = 0; y < argbImage.getHeight(); ++y) {
            argbImage.setRGB(x, y, (argbImage.getRGB(x, y) & 0xFFFFFF) | ((y / 10 * 10) << 24));
        }
    }

    PDImageXObject ximage = JPEGFactory.createFromImage(argbImage);
    validate(ximage, 8, width, height, "jpg", PDDeviceRGB.INSTANCE.getName());
    assertNotNull(ximage.getSoftMask());
    validate(ximage.getSoftMask(), 8, width, height, "jpg", PDDeviceGray.INSTANCE.getName());
    assertTrue(colorCount(ximage.getSoftMask().getImage()) > image.getHeight() / 10);

    doWritePDF(document, ximage, testResultsDir, "jpeg-intargb.pdf");
}

From source file:org.sejda.sambox.pdmodel.graphics.image.JPEGFactoryTest.java

@Test
public void testCreateFromImage4BYTE_ABGR() throws IOException {
    // workaround Open JDK bug
    // http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7044758
    if (System.getProperty("java.runtime.name").equals("OpenJDK Runtime Environment")
            && (System.getProperty("java.specification.version").equals("1.6")
                    || System.getProperty("java.specification.version").equals("1.7")
                    || System.getProperty("java.specification.version").equals("1.8"))) {
        return;//from  w w w. j ava  2 s. co m
    }

    PDDocument document = new PDDocument();
    BufferedImage image = ImageIO.read(JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg"));

    // create an ARGB image
    int width = image.getWidth();
    int height = image.getHeight();
    BufferedImage argbImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
    Graphics ag = argbImage.getGraphics();
    ag.drawImage(image, 0, 0, null);
    ag.dispose();

    for (int x = 0; x < argbImage.getWidth(); ++x) {
        for (int y = 0; y < argbImage.getHeight(); ++y) {
            argbImage.setRGB(x, y, (argbImage.getRGB(x, y) & 0xFFFFFF) | ((y / 10 * 10) << 24));
        }
    }

    PDImageXObject ximage = JPEGFactory.createFromImage(argbImage);
    validate(ximage, 8, width, height, "jpg", PDDeviceRGB.INSTANCE.getName());
    assertNotNull(ximage.getSoftMask());
    validate(ximage.getSoftMask(), 8, width, height, "jpg", PDDeviceGray.INSTANCE.getName());
    assertTrue(colorCount(ximage.getSoftMask().getImage()) > image.getHeight() / 10);

    doWritePDF(document, ximage, testResultsDir, "jpeg-4bargb.pdf");
}

From source file:BlurredImage.java

public void paint(Graphics g) {
    try {/*from ww  w .  ja  v  a 2s  .  c om*/
        BufferedImage myImage = ImageIO.read(this.getClass().getResource("redrock.png"));

        BufferedImage filteredImage = new BufferedImage(myImage.getWidth(null), myImage.getHeight(null),
                BufferedImage.TYPE_BYTE_GRAY);

        Graphics g1 = filteredImage.getGraphics();
        g1.drawImage(myImage, 400, 200, null);

        float[] blurKernel = { 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f };

        BufferedImageOp blur = new ConvolveOp(new Kernel(3, 3, blurKernel));
        myImage = blur.filter(myImage, null);
        g1.dispose();

        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(myImage, null, 3, 3);
    } catch (Exception e) {

    }
}

From source file:com.ace.erp.controller.sys.company.OrganizationController.java

@RequestMapping(value = "/company/uploadLogo", method = RequestMethod.POST)
@ResponseBody//from   w  w  w.java  2  s  . c  om
public Response uploadLogo(@CurrentUser User user, String srcImageFile, int x, int y, int destWidth,
        int destHeight, int srcShowWidth, int srcShowHeight, HttpServletRequest request) {
    try {
        String path = request.getSession().getServletContext().getRealPath("/");
        String contextPath = request.getContextPath();
        Image img;
        ImageFilter cropFilter;
        //String srcFileName = FilenameUtils.getName(srcImageFile);
        String srcFileName = StringUtils.isNotBlank(contextPath) ? srcImageFile.replaceFirst(contextPath, "")
                : srcImageFile;
        // ???
        File srcFile = new File(path + "/" + srcFileName);

        BufferedImage bi = ImageIO.read(srcFile);
        //??????????
        int srcWidth = bi.getWidth(); // ?
        int srcHeight = bi.getHeight(); // ?
        if (srcShowWidth == 0)
            srcShowWidth = srcWidth;
        if (srcShowHeight == 0)
            srcShowHeight = srcHeight;

        if (srcShowWidth >= destWidth && srcShowHeight >= destHeight) {
            //???
            Image image = bi.getScaledInstance(srcShowWidth, srcShowHeight, Image.SCALE_DEFAULT);

            cropFilter = new CropImageFilter(x, y, destWidth, destHeight);

            img = Toolkit.getDefaultToolkit()
                    .createImage(new FilteredImageSource(image.getSource(), cropFilter));
            BufferedImage tag = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);
            Graphics g = tag.getGraphics();
            g.drawImage(img, 0, 0, null); // ??
            g.dispose();

            String ext = FilenameUtils.getExtension(srcImageFile);

            //path += HEADER_PIC;
            //User loginUser = SystemUtil.getLoginUser(request.getSession());
            //String fileName = user.getOrganizationList().get(0).getId()+"";
            File destImageFile = new File(path + "/" + System.currentTimeMillis() + ".jpg");
            // 
            ImageIO.write(tag, ext, destImageFile);

            //loginUser.setPicPath(SystemConst.SYSTEM_CONTEXT_PATH_VALUE + HEADER_PIC + "/" + fileName);
            //userService.update(loginUser);
            // 
            srcFile.delete();
            return new Response(new ResponseHeader(200, 20));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:net.pms.medialibrary.commons.helpers.FileImportHelper.java

/**
 * Creates a buffered image from an image
 * @param image the image/*from w  w w.  j av a  2  s . co  m*/
 * @return the buffered image
 */
public static BufferedImage getBufferedImage(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 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:MouseDragActionPanel.java

public void mouseDragged(MouseEvent evt) {
    int x = evt.getX();
    int y = evt.getY();

    if (currentSquareIndex >= 0) {
        Graphics g = getGraphics();
        g.setXORMode(getBackground());//from  www.ja  v  a  2 s  . c o  m
        ((Graphics2D) g).draw(squares[currentSquareIndex]);
        squares[currentSquareIndex].x = x;
        squares[currentSquareIndex].y = y;
        ((Graphics2D) g).draw(squares[currentSquareIndex]);
        g.dispose();
    }
}