Example usage for java.awt.image BufferedImage TYPE_INT_ARGB

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

Introduction

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

Prototype

int TYPE_INT_ARGB

To view the source code for java.awt.image BufferedImage TYPE_INT_ARGB.

Click Source Link

Document

Represents an image with 8-bit RGBA color components packed into integer pixels.

Usage

From source file:Main.java

/**
 * Resizes an image using trilinear filtering.
 * @param source   the source image.//  w  w  w .  jav a2s . c  om
 * @param newWidth   the new image width.
 * @param newHeight   the new image height.
 */
public static BufferedImage performResizeTrilinear(BufferedImage source, int newWidth, int newHeight) {
    BufferedImage out = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = out.createGraphics();
    g2d.setComposite(AlphaComposite.Src);
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.drawImage(source, 0, 0, newWidth, newHeight, null);
    g2d.dispose();
    return out;
}

From source file:Main.java

private static BufferedImage dye(BufferedImage image, Color color) {
    int w = image.getWidth();
    int h = image.getHeight();
    BufferedImage dyed = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = dyed.createGraphics();
    g.drawImage(image, 0, 0, null);/*from w w w .  j  a v  a 2s.  c o m*/
    g.setComposite(AlphaComposite.SrcAtop);
    g.setColor(color);
    g.fillRect(0, 0, w, h);
    g.dispose();
    return dyed;
}

From source file:Main.java

public static Image resize(Image i, int scale) {
    BufferedImage resizedImage = new BufferedImage(scale, scale, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(i, 0, 0, scale, scale, null);
    g.dispose();/*from  w  w  w  .  j a  v  a  2  s  .com*/
    g.setComposite(AlphaComposite.Src);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    return resizedImage;
}

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

/**
 * /*from  ww  w .j  a  va 2  s . 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:Main.java

public static BufferedImage createRotatedTextImage(String text, int angle, Font ft) {
    Graphics2D g2d = null;/*w w  w. j  a v  a  2 s  .  c  o  m*/
    try {
        if (text == null || text.trim().length() == 0) {
            return null;
        }

        BufferedImage stringImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);

        g2d = (Graphics2D) stringImage.getGraphics();
        g2d.setFont(ft);

        FontMetrics fm = g2d.getFontMetrics();
        Rectangle2D bounds = fm.getStringBounds(text, g2d);

        TextLayout tl = new TextLayout(text, ft, g2d.getFontRenderContext());

        g2d.dispose();
        g2d = null;

        return createRotatedImage(tl, (int) bounds.getWidth(), (int) bounds.getHeight(), angle);
    } catch (Exception e) {
        e.printStackTrace();

        if (g2d != null) {
            g2d.dispose();
        }
    }

    return null;
}

From source file:Main.java

/**
 * Changes the opacity of a given image.
 * @param src The source image.//from  w ww. j  a v a  2  s. com
 * @param opacity The opacity to change to.
 * @return
 */
public static BufferedImage imgUtilAdjustImageTransparency(BufferedImage src, float opacity) {
    if (src == null)
        return null;
    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity);
    BufferedImage cpy = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = (Graphics2D) cpy.getGraphics();
    g.setComposite(ac);
    g.drawImage(src, 0, 0, null);
    g.dispose();
    return cpy;
}

From source file:Main.java

/**
 * Determine the appropriate {@link WritablePixelFormat} type that can
 * be used to transfer data into the indicated BufferedImage.
 * /* ww  w . ja  v a2s .  co m*/
 * @param bimg the BufferedImage that will be used as a destination for
 *             a {@code PixelReader<IntBuffer>#getPixels()} operation.
 * @return 
 */
private static WritablePixelFormat<IntBuffer> getAssociatedPixelFormat(BufferedImage bimg) {
    switch (bimg.getType()) {
    // We lie here for xRGB, but we vetted that the src data was opaque
    // so we can ignore the alpha.  We use ArgbPre instead of Argb
    // just to get a loop that does not have divides in it if the
    // PixelReader happens to not know the data is opaque.
    case BufferedImage.TYPE_INT_RGB:
    case BufferedImage.TYPE_INT_ARGB_PRE:
        return PixelFormat.getIntArgbPreInstance();
    case BufferedImage.TYPE_INT_ARGB:
        return PixelFormat.getIntArgbInstance();
    default:
        // Should not happen...
        throw new InternalError("Failed to validate BufferedImage type");
    }
}

From source file:Main.java

/**
 * Creates (in a rather clumsy way) the font metrics for a given {@link Font}.
 * //from w w w  .ja  v a2  s.c om
 * @param aFont
 *          the font instance to create the font metrics for, cannot be
 *          <code>null</code>.
 * @return a font metrics, never <code>null</code>.
 */
private static FontMetrics createFontMetrics(final Font aFont) {
    BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
    Graphics canvas = img.getGraphics();

    try {
        return canvas.getFontMetrics(aFont);
    } finally {
        canvas.dispose();
        canvas = null;
        img = null;
    }
}

From source file:Main.java

/**
 * Smoothly scales the given {@link BufferedImage} to the given width and height using the
 * {@link Image#SCALE_SMOOTH} algorithm (generally bicubic resampling or bilinear filtering).
 *
 * @param source The source image.//from w w w. jav  a 2 s.  c  o m
 * @param width  The destination width to scale to.
 * @param height The destination height to scale to.
 * @return A new, scaled image.
 */
public static BufferedImage scaledImage(BufferedImage source, int width, int height) {
    Image scaledImage = source.getScaledInstance(width, height, Image.SCALE_SMOOTH);
    BufferedImage scaledBufImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics g = scaledBufImage.createGraphics();
    g.drawImage(scaledImage, 0, 0, null);
    g.dispose();
    return scaledBufImage;
}

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;

    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f);

    BufferedImage buffImg = new BufferedImage(60, 60, BufferedImage.TYPE_INT_ARGB);
    Graphics2D gbi = buffImg.createGraphics();

    gbi.setPaint(Color.red);/*  w w  w.j  a  v a2  s . c om*/
    gbi.fillRect(0, 0, 40, 40);
    gbi.setComposite(ac);

    gbi.setPaint(Color.green);
    gbi.fillRect(5, 5, 40, 40);

    g2d.drawImage(buffImg, 20, 20, null);
}