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:CompositingDST_ATOP.java

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

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

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

    gbi.setPaint(Color.red);/*from   w w  w  .  ja va  2  s  .  co  m*/
    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);
}

From source file:CompositingDST.java

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

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

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

    gbi.setPaint(Color.red);/*from w  w  w.  ja  va2 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);
}

From source file:CompositingDST_ATOP.java

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

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

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

    gbi.setPaint(Color.red);/*from ww w. j a  va2 s.  com*/
    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);
}

From source file:Main.java

public MyPanel() {
    this.setLayout(new GridLayout());
    this.setPreferredSize(new Dimension(W, H));
    int w = W / 2;
    int h = H / 2;
    int r = w / 5;
    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bi.createGraphics();
    g.setColor(Color.gray);//from w ww. ja va 2s  .  com
    g.fillRect(0, 0, w, h);
    g.setColor(Color.blue);
    g.fillRect(w / 2 - r, h / 2 - r / 2, 2 * r, r);
    g.dispose();
    this.add(new JLabel(new ImageIcon(bi), JLabel.CENTER));
}

From source file:TexturePaintDemo.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);
    Graphics2D big = bi.createGraphics();
    big.setColor(Color.blue);/*from w ww.ja va  2s . c  o  m*/
    big.fillRect(0, 0, 5, 5);
    big.setColor(Color.lightGray);
    big.fillOval(0, 0, 5, 5);
    Rectangle r = new Rectangle(0, 0, 5, 5);
    g2.setPaint(new TexturePaint(bi, r));

    Rectangle rect = new Rectangle(5, 5, 200, 200);

    g2.fill(rect);
}

From source file:ImageOpByRomain.java

/**
 * <p>//from   www  .java 2s.c  o  m
 * Returns a thumbnail of a source image. <code>newSize</code> defines the
 * length of the longest dimension of the thumbnail. The other dimension is
 * then computed according to the dimensions ratio of the original picture.
 * </p>
 * <p>
 * This method favors speed over quality. When the new size is less than half
 * the longest dimension of the source image,
 * {@link #createThumbnail(BufferedImage, int)} or
 * {@link #createThumbnail(BufferedImage, int, int)} should be used instead to
 * ensure the quality of the result without sacrificing too much performance.
 * </p>
 * 
 * @see #createThumbnailFast(java.awt.image.BufferedImage, int, int)
 * @see #createThumbnail(java.awt.image.BufferedImage, int)
 * @see #createThumbnail(java.awt.image.BufferedImage, int, int)
 * @param image
 *            the source image
 * @param newSize
 *            the length of the largest dimension of the thumbnail
 * @return a new compatible <code>BufferedImage</code> containing a
 *         thumbnail of <code>image</code>
 * @throws IllegalArgumentException
 *             if <code>newSize</code> is larger than the largest dimension
 *             of <code>image</code> or &lt;= 0
 */
public static BufferedImage createThumbnailFast(BufferedImage image, int newSize) {
    float ratio;
    int width = image.getWidth();
    int height = image.getHeight();

    if (width > height) {
        if (newSize >= width) {
            throw new IllegalArgumentException("newSize must be lower than" + " the image width");
        } else if (newSize <= 0) {
            throw new IllegalArgumentException("newSize must" + " be greater than 0");
        }

        ratio = (float) width / (float) height;
        width = newSize;
        height = (int) (newSize / ratio);
    } else {
        if (newSize >= height) {
            throw new IllegalArgumentException("newSize must be lower than" + " the image height");
        } else if (newSize <= 0) {
            throw new IllegalArgumentException("newSize must" + " be greater than 0");
        }

        ratio = (float) height / (float) width;
        height = newSize;
        width = (int) (newSize / ratio);
    }

    BufferedImage temp = createCompatibleImage(image, width, height);
    Graphics2D g2 = temp.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2.drawImage(image, 0, 0, temp.getWidth(), temp.getHeight(), null);
    g2.dispose();

    return temp;
}

From source file:Main.java

private Image createImage(Color color) {
    BufferedImage bImg = new BufferedImage(Width, Height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bImg.createGraphics();
    g2.setBackground(color);//from www  .jav a 2s .c o  m
    g2.clearRect(0, 0, Width, Height);
    g2.dispose();
    return bImg;
}

From source file:GraphicsInfo.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    GraphicsConfiguration gc = g2d.getDeviceConfiguration();
    printModelType(gc.getColorModel());/*from   w ww .  ja v  a  2  s . co  m*/
    BufferedImage bi = new BufferedImage(20, 20, BufferedImage.TYPE_BYTE_INDEXED);
    Graphics2D g2d2 = bi.createGraphics();
    GraphicsConfiguration gc2 = g2d2.getDeviceConfiguration();
    printModelType(gc2.getColorModel());
    bi = new BufferedImage(20, 20, BufferedImage.TYPE_INT_ARGB);
    g2d2 = bi.createGraphics();
    gc2 = g2d2.getDeviceConfiguration();
    printModelType(gc2.getColorModel());
    bi = new BufferedImage(20, 20, BufferedImage.TYPE_USHORT_565_RGB);
    g2d2 = bi.createGraphics();
    gc2 = g2d2.getDeviceConfiguration();
    printModelType(gc2.getColorModel());
}

From source file:TexturedText.java

private BufferedImage getTextureImage() {
    // Create the test image.
    int size = 8;
    BufferedImage bi = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bi.createGraphics();
    g2.setPaint(Color.red);//from  ww w  .  j  av  a 2  s  .c om
    g2.fillRect(0, 0, size / 2, size / 2);
    g2.setPaint(Color.yellow);
    g2.fillRect(size / 2, 0, size, size / 2);
    g2.setPaint(Color.green);
    g2.fillRect(0, size / 2, size / 2, size);
    g2.setPaint(Color.blue);
    g2.fillRect(size / 2, size / 2, size, size);
    return bi;
}

From source file:ImageOpByRomain.java

/**
 * <p>//  ww w  .j av  a2s . co  m
 * Returns a thumbnail of a source image.
 * </p>
 * <p>
 * This method offers a good trade-off between speed and quality. The result
 * looks better than
 * {@link #createThumbnailFast(java.awt.image.BufferedImage, int)} when the
 * new size is less than half the longest dimension of the source image, yet
 * the rendering speed is almost similar.
 * </p>
 * 
 * @see #createThumbnailFast(java.awt.image.BufferedImage, int)
 * @see #createThumbnailFast(java.awt.image.BufferedImage, int, int)
 * @see #createThumbnail(java.awt.image.BufferedImage, int)
 * @param image
 *            the source image
 * @param newWidth
 *            the width of the thumbnail
 * @param newHeight
 *            the height of the thumbnail
 * @return a new compatible <code>BufferedImage</code> containing a
 *         thumbnail of <code>image</code>
 * @throws IllegalArgumentException
 *             if <code>newWidth</code> is larger than the width of
 *             <code>image</code> or if code>newHeight</code> is larger
 *             than the height of <code>image or if one the dimensions is not
 *             &gt; 0</code>
 */
public static BufferedImage createThumbnail(BufferedImage image, int newWidth, int newHeight) {
    int width = image.getWidth();
    int height = image.getHeight();

    if (newWidth >= width || newHeight >= height) {
        throw new IllegalArgumentException(
                "newWidth and newHeight cannot" + " be greater than the image" + " dimensions");
    } else if (newWidth <= 0 || newHeight <= 0) {
        throw new IllegalArgumentException("newWidth and newHeight must" + " be greater than 0");
    }

    BufferedImage thumb = image;

    do {
        if (width > newWidth) {
            width /= 2;
            if (width < newWidth) {
                width = newWidth;
            }
        }

        if (height > newHeight) {
            height /= 2;
            if (height < newHeight) {
                height = newHeight;
            }
        }

        BufferedImage temp = createCompatibleImage(image, width, height);
        Graphics2D g2 = temp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(thumb, 0, 0, temp.getWidth(), temp.getHeight(), null);
        g2.dispose();

        thumb = temp;
    } while (width != newWidth || height != newHeight);

    return thumb;
}