Example usage for java.awt.image BufferedImage BufferedImage

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

Introduction

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

Prototype

public BufferedImage(int width, int height, int imageType) 

Source Link

Document

Constructs a BufferedImage of one of the predefined image types.

Usage

From source file:BufferedAnimate.java

public void paint(Graphics g) {
    if ((oldSize == null) || (oldSize != getSize())) {
        oldSize = getSize();//from w w w  .j a  v  a2s .co m
        buffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
    }
    if (insets == null) {
        insets = getInsets();
    }
    // Calculate each time in case of resize
    int x = insets.left;
    int y = insets.top;
    int width = getWidth() - insets.left - insets.right;
    int height = getHeight() - insets.top - insets.bottom;
    int start = 0;
    int steps = colors.length;
    int stepSize = 360 / steps;
    synchronized (colors) {
        Graphics bufferG = buffer.getGraphics();
        bufferG.setColor(Color.WHITE);
        bufferG.fillRect(x, y, width, height);
        for (int i = 0; i < steps; i++) {
            bufferG.setColor(colors[i]);
            bufferG.fillArc(x, y, width, height, start, stepSize);
            start += stepSize;
        }
    }
    g.drawImage(buffer, 0, 0, this);
}

From source file:Main.java

public static Image merge(Image left, Image right) {
    BufferedImage merged = new BufferedImage(left.getWidth(null) + right.getWidth(null), left.getHeight(null),
            BufferedImage.TYPE_INT_RGB);
    Graphics2D g = (Graphics2D) merged.getGraphics();
    g.drawImage(left, 0, 0, null);// w w w.ja  v a  2 s. c  om
    g.drawImage(right, left.getWidth(null), 0, null);
    return merged;
}

From source file:BasicDraw.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    int x = 10;/*  w  w  w.  ja va 2s.c  o m*/
    int y = 10;
    int width = 50;
    int height = 25;
    BufferedImage bi = new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB);

    TexturePaint texture = new TexturePaint(bi, new Rectangle(x, y, width, height));
    g2d.setPaint(texture);
}

From source file:Main.java

/**
 * Creates a new ARGB {@link BufferedImage} of the given width and height.
 *
 * @param width  The width of the new image.
 * @param height The height of the new image.
 * @return The newly created image./*from  ww  w .  jav a 2 s  .  co m*/
 */
public static BufferedImage newArgbBufferedImage(int width, int height) {
    return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
}

From source file:ChartServlet.java

/** Draw a Graphical Chart in response to a user request */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

    response.setContentType("image/jpeg");

    // Create an Image
    BufferedImage img = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB);

    // Get the Image's Graphics, and draw.
    Graphics2D g = img.createGraphics();

    // In real life this would call some charting software...
    g.setColor(Color.white);/*from   w  w  w  .j a  v  a  2 s  .c om*/
    g.fillRect(0, 0, W, H);
    g.setColor(Color.green);
    g.fillOval(100, 75, 50, 50);

    // Write the output
    OutputStream os = response.getOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(os);

    if (!ImageIO.write(img, "jpeg", ios)) {
        log("Boo hoo, failed to write JPEG");
    }
    ios.close();
    os.close();
}

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

/**
 * Creates a scaled copy of the source image.
 * /* w ww  .jav  a2 s. co m*/
 * @param src
 *            source image to be scaled
 * @param width
 *            the width for the new scaled image in pixels
 * @param height
 *            the height for the new scaled image in pixels
 * @return a copy of the source image scaled to <tt>width</tt> x
 *         <tt>height</tt> pixels.
 */
public static BufferedImage scaleImage(BufferedImage src, int width, int height) {
    Image scaled = src.getScaledInstance(width, height, 0);
    BufferedImage ret = null;
    /*
     * ColorModel cm = src.getColorModel(); if (cm instanceof
     * IndexColorModel) { ret = new BufferedImage( width, height,
     * src.getType(), (IndexColorModel) cm ); } else { ret = new
     * BufferedImage( src.getWidth(), src.getHeight(), src.getType() ); }
     * Graphics2D g = ret.createGraphics(); //clear alpha channel Composite
     * comp = g.getComposite();
     * g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR,
     * 0.0f)); Rectangle2D.Double d = new
     * Rectangle2D.Double(0,0,ret.getWidth(),ret.getHeight()); g.fill(d);
     * g.setComposite(comp);
     */
    ret = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = ret.createGraphics();
    // copy image
    g.drawImage(scaled, 0, 0, null);
    return ret;
}

From source file:Main.java

/**
 * Creates and returns image from the given text.
 * @param text input text/*from www  .j a v  a  2 s  .  c  o m*/
 * @param font text font
 * @return image with input text
 */
public static BufferedImage createImageFromText(String text, Font font) {
    //You may want to change these setting, or make them parameters
    boolean isAntiAliased = true;
    boolean usesFractionalMetrics = false;
    FontRenderContext frc = new FontRenderContext(null, isAntiAliased, usesFractionalMetrics);
    TextLayout layout = new TextLayout(text, font, frc);
    Rectangle2D bounds = layout.getBounds();
    int w = (int) Math.ceil(bounds.getWidth());
    int h = (int) Math.ceil(bounds.getHeight()) + 2;
    BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); //for example;
    Graphics2D g = image.createGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, w, h);
    g.setColor(Color.BLACK);
    g.setFont(font);
    Object antiAliased = isAntiAliased ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON
            : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF;
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, antiAliased);
    Object fractionalMetrics = usesFractionalMetrics ? RenderingHints.VALUE_FRACTIONALMETRICS_ON
            : RenderingHints.VALUE_FRACTIONALMETRICS_OFF;
    g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, fractionalMetrics);
    g.drawString(text, (float) -bounds.getX(), (float) -bounds.getY());
    g.dispose();

    return image;
}

From source file:ColorPan.java

public void initialize() {
    int width = getSize().width;
    int height = getSize().height;
    int[] data = new int[width * height];
    int index = 0;
    for (int i = 0; i < height; i++) {
        int red = (i * 255) / (height - 1);
        for (int j = 0; j < width; j++) {
            int green = (j * 255) / (width - 1);
            int blue = 128;
            data[index++] = (red << 16) | (green << 8) | blue;
        }//from www  .  j  a va 2  s .c  o  m
    }

    image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    image.setRGB(0, 0, width, height, data, 0, width);
}

From source file:QRCode.java

public static String createQRCode(String arg) {
    int size = 125;
    String fileType = "png";
    File myFile = null;//from w w w . j  a v  a2s .com
    UUID uuid = UUID.nameUUIDFromBytes(arg.getBytes());

    try {
        myFile = File.createTempFile("temp-file-name", ".png");

        Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix byteMatrix = qrCodeWriter.encode(uuid.toString(), BarcodeFormat.QR_CODE, size, size, hintMap);
        int CrunchifyWidth = byteMatrix.getWidth();
        BufferedImage image = new BufferedImage(CrunchifyWidth, CrunchifyWidth, BufferedImage.TYPE_INT_RGB);
        image.createGraphics();

        Graphics2D graphics = (Graphics2D) image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, CrunchifyWidth, CrunchifyWidth);
        graphics.setColor(Color.BLACK);

        for (int i = 0; i < CrunchifyWidth; i++) {
            for (int j = 0; j < CrunchifyWidth; j++) {
                if (byteMatrix.get(i, j)) {
                    graphics.fillRect(i, j, 1, 1);
                }
            }
        }
        ImageIO.write(image, fileType, myFile);
        //System.out.println("\n\nYou have successfully created QR Code " + myFile.getCanonicalPath());
        return myFile.getCanonicalPath();
    } catch (WriterException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}