Example usage for java.awt.image BufferedImage createGraphics

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

Introduction

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

Prototype

public Graphics2D createGraphics() 

Source Link

Document

Creates a Graphics2D , which can be used to draw into this BufferedImage .

Usage

From source file:Utils.java

/** 
 * Renders multiple paragraphs of text in an array to an image (created and returned). 
 *
 * @param font The font to use/*from   ww w  .j  ava 2s.c  om*/
 * @param textColor The color of the text
 * @param text The message in an array of strings (one paragraph in each
 * @param width The width the text should be limited to
 * @return An image with the text rendered into it
 */
public static BufferedImage renderTextToImage(Font font, Color textColor, String text[], int width) {
    LinkedList<BufferedImage> images = new LinkedList<BufferedImage>();

    int totalHeight = 0;

    for (String paragraph : text) {
        BufferedImage paraImage = renderTextToImage(font, textColor, paragraph, width);
        totalHeight += paraImage.getHeight();
        images.add(paraImage);
    }

    BufferedImage image = createCompatibleImage(width, totalHeight);
    Graphics2D graphics = (Graphics2D) image.createGraphics();

    int y = 0;

    for (BufferedImage paraImage : images) {
        graphics.drawImage(paraImage, 0, y, null);
        y += paraImage.getHeight();
    }

    graphics.dispose();
    return image;
}

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

/**
 * /* www. j a  v a  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:com.ables.pix.utility.PictureOps.java

public static BufferedImage tilt(BufferedImage image, double rotation) {
    double sin = Math.abs(Math.sin(getAngle()));
    double cos = Math.abs(Math.cos(getAngle()));
    int w = image.getWidth(), h = image.getHeight();

    int neww = (int) Math.floor(w * cos + sin * h), newh = (int) Math.floor(h * cos + sin * w);
    GraphicsConfiguration gc = getDefaultConfiguration();
    BufferedImage rotated = gc.createCompatibleImage(neww, newh);
    Graphics2D g = rotated.createGraphics();
    g.translate((neww - w) / 2, (newh - h / 2));
    g.rotate(getAngle(), w / 2, h / 2);//ww  w.j  a  v a  2 s.c o  m
    g.drawRenderedImage(image, null);
    g.dispose();
    return rotated;
}

From source file:org.web4thejob.web.util.MediaUtil.java

public static BufferedImage createThumbnail(byte[] bytes) {
    Image image = getImage(bytes);
    BufferedImage bufferedImage = new BufferedImage(image.getWidth(), image.getHeight(),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = bufferedImage.createGraphics();
    g.drawImage(image.toImageIcon().getImage(), 0, 0, null);
    g.dispose();//from   w w  w.  jav a  2 s .co  m
    return createThumbnail(bufferedImage);
}

From source file:net.chris54721.infinitycubed.utils.Utils.java

public static BufferedImage toBufferedImage(Image img) {
    if (img instanceof BufferedImage)
        return (BufferedImage) img;
    BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D bGr = bimage.createGraphics();
    bGr.drawImage(img, 0, 0, null);/*from w  w w . jav a 2  s.  c  om*/
    bGr.dispose();
    return bimage;
}

From source file:Main.java

public static BufferedImage getFlippedImage(BufferedImage bi) {
    BufferedImage flipped = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
    AffineTransform tran = AffineTransform.getTranslateInstance(0, bi.getHeight());
    AffineTransform flip = AffineTransform.getScaleInstance(1d, -1d);
    tran.concatenate(flip);//from   w ww.  ja  v  a  2  s. c  o  m

    Graphics2D g = flipped.createGraphics();
    g.setTransform(tran);
    g.drawImage(bi, 0, 0, null);
    g.dispose();

    return flipped;
}

From source file:ar.com.zauber.common.image.impl.AbstractImage.java

/**
 * Creates a thumbnail//  w  w  w.j  a  v  a  2 s  . c om
 * 
 * @param is data source
 * @param os data source
 * @throws IOException if there is a problem reading is
 */
public static void createThumbnail(final InputStream is, final OutputStream os, final int target)
        throws IOException {
    final float compression = 0.85F;
    ImageWriter writer = null;
    MemoryCacheImageOutputStream mos = null;
    Graphics2D graphics2D = null;
    try {
        final BufferedImage bi = ImageIO.read(is);
        final Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("JPG");
        if (!iter.hasNext()) {
            throw new IllegalStateException("can't find JPG subsystem");
        }

        int w = bi.getWidth(), h = bi.getHeight();
        if (w < target && h < target) {
            // nothing to recalculate, ya es chiquita.
        } else {
            if (w > h) {
                h = target * bi.getHeight() / bi.getWidth();
                w = target;
            } else {
                w = target * bi.getWidth() / bi.getHeight();
                h = target;
            }
        }
        // draw original image to thumbnail image object and
        // scale it to the new size on-the-fly
        final BufferedImage thumbImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        graphics2D = thumbImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(bi, 0, 0, w, h, null);

        writer = (ImageWriter) iter.next();
        final ImageWriteParam iwp = writer.getDefaultWriteParam();
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setCompressionQuality(compression);
        mos = new MemoryCacheImageOutputStream(os);
        writer.setOutput(mos);
        writer.write(null, new IIOImage(thumbImage, null, null), iwp);
    } finally {
        if (writer != null) {
            writer.dispose();
        }
        if (mos != null) {
            mos.close();
        }
        if (graphics2D != null) {
            graphics2D.dispose();
        }
        is.close();
        os.close();
    }
}

From source file:eu.planets_project.tb.impl.data.util.ImageThumbnail.java

/**
 * Create a scaled jpeg of an image. The width/height ratio is preserved.
 * /*from www.ja  v  a2s  .com*/
 * <p>
 * If image is smaller than thumbWidth x thumbHeight, it will be magnified,
 * otherwise it will be scaled down.
 * </p>
 * 
 * @param image
 *            the image to reduce
 * @param thumbWidth
 *            the maximum width of the thumbnail
 * @param thumbHeight
 *            the maximum heigth of the thumbnail
 * @param quality
 *            the jpeg quality ot the thumbnail
 * @param out
 *            a stream where the thumbnail data is written to
 */
public static void createThumb(Image image, int thumbWidth, int thumbHeight, OutputStream out)
        throws Exception {
    int imageWidth = image.getWidth(null);
    int imageHeight = image.getHeight(null);
    double thumbRatio = (double) thumbWidth / (double) thumbHeight;
    double imageRatio = (double) imageWidth / (double) imageHeight;
    if (thumbRatio < imageRatio) {
        thumbHeight = (int) (thumbWidth / imageRatio);
    } else {
        thumbWidth = (int) (thumbHeight * imageRatio);
    }
    // draw original image to thumbnail image object and
    // scale it to the new size on-the-fly
    BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics2D = thumbImage.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    graphics2D.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
            RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
    // save thumbnail image to out stream
    log.debug("Start writing rescaled image...");
    ImageIO.write(thumbImage, "png", out);
    log.debug("DONE writing rescaled image...");
}

From source file:com.partagames.imageresizetool.SimpleImageResizeTool.java

/**
 * Scales an image to the desired dimensions.
 *
 * @param img  Original image//from  www  .j ava 2s. c o  m
 * @param newW Target width
 * @param newH Target height
 * @return Scaled image
 */
public static BufferedImage scale(BufferedImage img, int newW, int newH) {
    int w = img.getWidth();
    int h = img.getHeight();
    final BufferedImage dimg = new BufferedImage(newW, newH, img.getType());
    final Graphics2D g = dimg.createGraphics();

    // use provided rendering hint, default is bilinear 
    switch (scalingHint) {
    case "n":
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
        break;
    case "b":
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        break;
    }

    g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);
    g.dispose();
    return dimg;
}

From source file:net.mindengine.oculus.frontend.web.controllers.project.ProjectEditController.java

private static BufferedImage resize(BufferedImage image, int width, int height) {
    BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = resizedImage.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

    g.drawImage(image, 0, 0, width, height, null);
    g.dispose();/* w  ww. j a  v  a 2s  .  c o  m*/
    return resizedImage;
}