Example usage for java.awt.image BufferedImage getGraphics

List of usage examples for java.awt.image BufferedImage getGraphics

Introduction

In this page you can find the example usage for java.awt.image BufferedImage getGraphics.

Prototype

public java.awt.Graphics getGraphics() 

Source Link

Document

This method returns a Graphics2D , but is here for backwards compatibility.

Usage

From source file:Main.java

private Image createImage() {
    BufferedImage bufferedImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
    Graphics g = bufferedImage.getGraphics();
    g.drawString("www.java2s.com", 20, 20);

    return bufferedImage;
}

From source file:MainClass.java

private BufferedImage createImage() {
    BufferedImage bufferedImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
    Graphics g = bufferedImage.getGraphics();
    g.drawString("www.java2s.com", 20, 20);

    return bufferedImage;
}

From source file:net.cbtltd.server.UploadFileService.java

private static boolean getImage(String fn, int fullsizepixels, int thumbnailpixels) {

    ImageIcon image = new ImageIcon(fn);
    //      if(image.getIconHeight() > 0 && image.getIconWidth() > 0) {
    if (image.getImageLoadStatus() == MediaTracker.COMPLETE) {
        ImageIcon fullsizeImage = new ImageIcon(
                image.getImage().getScaledInstance(-1, fullsizepixels, Image.SCALE_SMOOTH));
        LOG.debug("\n UploadFileService setImage image= " + image + " width=" + fullsizeImage.getIconWidth()
                + "  height=" + fullsizeImage.getIconHeight());
        BufferedImage fullsizeBufferedImage = new BufferedImage(fullsizeImage.getIconWidth(),
                fullsizeImage.getIconHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics fullsizeGraphics = fullsizeBufferedImage.getGraphics();
        fullsizeGraphics.drawImage(fullsizeImage.getImage(), 0, 0, null);
        File fullsizeFile = new File(fn.substring(0, fn.lastIndexOf('.')) + ".jpg");
        fullsizeFile.delete();// w ww.ja  v  a 2  s. com

        try {
            ImageIO.write(fullsizeBufferedImage, FULLSIZE_JPEG, fullsizeFile);
        } catch (IOException x) {
            throw new RuntimeException("Error saving full sized image " + x.getMessage());
        }

        ImageIcon thumbnailImage = new ImageIcon(
                image.getImage().getScaledInstance(-1, thumbnailpixels, Image.SCALE_SMOOTH));
        File thumbnailFile = new File(fn.substring(0, fn.lastIndexOf('.')) + "Thumb.jpg");
        thumbnailFile.delete();
        BufferedImage thumbnailBufferedImage = new BufferedImage(thumbnailImage.getIconWidth(),
                thumbnailImage.getIconHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics thumbnailGraphics = thumbnailBufferedImage.getGraphics();
        thumbnailGraphics.drawImage(thumbnailImage.getImage(), 0, 0, null);
        try {
            ImageIO.write(thumbnailBufferedImage, FULLSIZE_JPEG, thumbnailFile);
        } catch (IOException x) {
            throw new RuntimeException("Error saving thumbnail image " + x.getMessage());
        }
        return true;
    } else {
        LOG.error("\n UploadFileService setImage image= " + image + " width=" + image.getIconWidth()
                + "  height=" + image.getIconHeight());
        return false;
    }
}

From source file:net.cbtltd.server.UploadFileService.java

private static boolean getImage(String fn, BufferedImage image, Map<String, byte[]> images) {
    int fullsizepixels = Text.FULLSIZE_PIXELS_VALUE;
    int thumbnailpixels = Text.THUMBNAIL_PIXELS_VALUE;
    //      ByteBuffer byteArray = new ByteBuffer();
    ByteArrayOutputStream bOutputReg = new ByteArrayOutputStream();
    ByteArrayOutputStream bOutputThumb = new ByteArrayOutputStream();
    ImageIcon imageIcon = new ImageIcon(image);
    //      if(image.getIconHeight() > 0 && image.getIconWidth() > 0) {
    if (imageIcon.getImageLoadStatus() == MediaTracker.COMPLETE) {
        ImageIcon fullsizeImage = new ImageIcon(
                imageIcon.getImage().getScaledInstance(-1, fullsizepixels, Image.SCALE_SMOOTH));
        LOG.debug("\n UploadFileService setImage image= " + imageIcon + " width=" + fullsizeImage.getIconWidth()
                + "  height=" + fullsizeImage.getIconHeight());
        BufferedImage fullsizeBufferedImage = new BufferedImage(fullsizeImage.getIconWidth(),
                fullsizeImage.getIconHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics fullsizeGraphics = fullsizeBufferedImage.getGraphics();
        fullsizeGraphics.drawImage(fullsizeImage.getImage(), 0, 0, null);
        String fullsizeFile = fn.substring(0, fn.lastIndexOf('.')) + ".jpg";

        try {//from  w ww  .  j av  a2s. co  m
            ImageIO.write(fullsizeBufferedImage, FULLSIZE_JPEG, bOutputReg);
            bOutputReg.flush();
            images.put(fullsizeFile, bOutputReg.toByteArray());
            bOutputReg.close();
        } catch (IOException x) {
            throw new RuntimeException("Error saving full sized image " + x.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage() + " Error saving full sized image: " + fullsizeFile);
        }

        ImageIcon thumbnailImage = new ImageIcon(
                imageIcon.getImage().getScaledInstance(-1, thumbnailpixels, Image.SCALE_SMOOTH));
        String thumbnailFile = fn.substring(0, fn.lastIndexOf('.')) + "Thumb.jpg";

        BufferedImage thumbnailBufferedImage = new BufferedImage(thumbnailImage.getIconWidth(),
                thumbnailImage.getIconHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics thumbnailGraphics = thumbnailBufferedImage.getGraphics();
        thumbnailGraphics.drawImage(thumbnailImage.getImage(), 0, 0, null);
        try {
            ImageIO.write(thumbnailBufferedImage, FULLSIZE_JPEG, bOutputThumb);
            bOutputThumb.flush();
            images.put(thumbnailFile, bOutputThumb.toByteArray());
            bOutputThumb.close();
        } catch (IOException x) {
            throw new RuntimeException("Error saving thumbnail image " + x.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage() + " Error saving thumbnail image: " + thumbnailFile);
        }

        return true;
    } else {
        LOG.error("\n UploadFileService setImage image= " + imageIcon + " width=" + imageIcon.getIconWidth()
                + "  height=" + imageIcon.getIconHeight());
        return false;
    }
}

From source file:Main.java

public void createThumbnail(File file) throws Exception {
    BufferedImage img = ImageIO.read(file);
    BufferedImage thumb = new BufferedImage(100, 200, BufferedImage.TYPE_INT_RGB);

    Graphics2D g2d = (Graphics2D) thumb.getGraphics();
    g2d.drawImage(img, 0, 0, thumb.getWidth() - 1, thumb.getHeight() - 1, 0, 0, img.getWidth() - 1,
            img.getHeight() - 1, null);//w ww.j a v  a  2  s.  co  m
    g2d.dispose();
    ImageIO.write(thumb, "PNG", new File("thumb.png"));
}

From source file:net.cbtltd.server.UploadFileService.java

private static boolean getCompressedImage(String fn, BufferedImage image, Map<String, byte[]> images) {
    int fullsizepixels = Text.FULLSIZE_PIXELS_VALUE;
    int thumbnailpixels = Text.THUMBNAIL_PIXELS_VALUE;
    //      ByteBuffer byteArray = new ByteBuffer();
    ByteArrayOutputStream bOutputReg = new ByteArrayOutputStream();
    ByteArrayOutputStream bOutputThumb = new ByteArrayOutputStream();
    ImageIcon imageIcon = new ImageIcon(image);
    try {/*w w w .  ja v  a  2s .co m*/
        GZIPOutputStream zipStreamReg = new GZIPOutputStream(bOutputReg);
        GZIPOutputStream zipStreamThumb = new GZIPOutputStream(bOutputThumb);

        if (imageIcon.getImageLoadStatus() == MediaTracker.COMPLETE) {
            ImageIcon fullsizeImage = new ImageIcon(
                    imageIcon.getImage().getScaledInstance(-1, fullsizepixels, Image.SCALE_SMOOTH));
            LOG.debug("\n UploadFileService setImage image= " + imageIcon + " width="
                    + fullsizeImage.getIconWidth() + "  height=" + fullsizeImage.getIconHeight());
            BufferedImage fullsizeBufferedImage = new BufferedImage(fullsizeImage.getIconWidth(),
                    fullsizeImage.getIconHeight(), BufferedImage.TYPE_INT_RGB);
            Graphics fullsizeGraphics = fullsizeBufferedImage.getGraphics();
            fullsizeGraphics.drawImage(fullsizeImage.getImage(), 0, 0, null);
            String fullsizeFile = fn.substring(0, fn.lastIndexOf('.')) + ".jpg";

            try {
                ImageIO.write(fullsizeBufferedImage, FULLSIZE_JPEG, zipStreamReg);
                zipStreamReg.close();
                bOutputReg.close();
                images.put(fullsizeFile, bOutputReg.toByteArray());
            } catch (IOException x) {
                throw new RuntimeException("Error saving full sized image " + x.getMessage());
            }

            ImageIcon thumbnailImage = new ImageIcon(
                    imageIcon.getImage().getScaledInstance(-1, thumbnailpixels, Image.SCALE_SMOOTH));
            String thumbnailFile = fn.substring(0, fn.lastIndexOf('.')) + "Thumb.jpg";

            BufferedImage thumbnailBufferedImage = new BufferedImage(thumbnailImage.getIconWidth(),
                    thumbnailImage.getIconHeight(), BufferedImage.TYPE_INT_RGB);
            Graphics thumbnailGraphics = thumbnailBufferedImage.getGraphics();
            thumbnailGraphics.drawImage(thumbnailImage.getImage(), 0, 0, null);
            try {
                ImageIO.write(thumbnailBufferedImage, FULLSIZE_JPEG, zipStreamThumb);
                zipStreamThumb.close();
                bOutputThumb.close();
                images.put(thumbnailFile, bOutputThumb.toByteArray());
            } catch (IOException x) {
                throw new RuntimeException("Error saving thumbnail image " + x.getMessage());
            }

            return true;
        } else {
            LOG.error("\n UploadFileService setImage image= " + imageIcon + " width=" + imageIcon.getIconWidth()
                    + "  height=" + imageIcon.getIconHeight());
            return false;
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return true;
}

From source file:com.liud.dailynote.ThumbnailatorTest.java

private void testYS8() throws IOException {
    String result = "src/main/resources/images/";
    OutputStream os = new FileOutputStream(result + "sijili_out.jpg");

    Image image = ImageIO.read(new File(result + "sijili.jpg"));

    BufferedImage bufferedImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
    bufferedImage.getGraphics().drawImage(image.getScaledInstance(100, 100, image.SCALE_SMOOTH), 0, 0, null);

    ImageIO.write(bufferedImage, "jpg", os);
    os.close();/*w ww  . j av  a  2s.co  m*/
}

From source file:GrayImage.java

public void paint(Graphics g) {
    Image myImage = new ImageIcon("yourImage.png").getImage();
    BufferedImage bufferedImage = new BufferedImage(myImage.getHeight(this), myImage.getWidth(this),
            BufferedImage.TYPE_BYTE_GRAY);

    Graphics gi = bufferedImage.getGraphics();
    gi.drawImage(myImage, 0, 0, null);/*from   w w w. j av a2s  .c o  m*/
    gi.dispose();

    Graphics2D g2d = (Graphics2D) g;
    g2d.drawImage(bufferedImage, null, 0, 0);
}

From source file:ImageOpByRomain.java

/**
 * <p>/*from w ww.j av a 2 s . c o m*/
 * Return a new compatible image that contains a copy of the specified image.
 * This method ensures an image is compatible with the hardware, and therefore
 * optimized for fast blitting operations.
 * </p>
 * 
 * @see #createCompatibleImage(java.awt.image.BufferedImage)
 * @see #createCompatibleImage(java.awt.image.BufferedImage, int, int)
 * @see #createCompatibleImage(int, int)
 * @see #createTranslucentCompatibleImage(int, int)
 * @see #loadCompatibleImage(java.net.URL)
 * @param image
 *            the image to copy into a new compatible image
 * @return a new compatible copy, with the same width and height and
 *         transparency and content, of <code>image</code>
 */
public static BufferedImage toCompatibleImage(BufferedImage image) {
    if (image.getColorModel().equals(CONFIGURATION.getColorModel())) {
        return image;
    }

    BufferedImage compatibleImage = CONFIGURATION.createCompatibleImage(image.getWidth(), image.getHeight(),
            image.getTransparency());
    Graphics g = compatibleImage.getGraphics();
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return compatibleImage;
}

From source file:BlurredImage.java

public void paint(Graphics g) {
    try {//from   w w 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) {

    }
}