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:de.mfo.jsurf.grid.RotationGrid.java

public static BufferedImage renderAnimGrid(int xAngleMin, int xAngleMax, int xSteps, int yAngleMin,
        int yAngleMax, int ySteps) {
    BufferedImage grid = new BufferedImage(ySteps * size, xSteps * size, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = (Graphics2D) grid.getGraphics();
    for (int x = 0; x < xSteps; ++x) {
        double xAngle = xAngleMin + (xAngleMax - xAngleMin) * (xSteps == 1 ? 0.5 : (x / (double) (xSteps - 1)));
        Matrix4d matRotX = new Matrix4d();
        matRotX.setIdentity();//from  w  w  w .ja v a 2 s  . co  m
        matRotX.rotX(Math.toRadians(xAngle));
        for (int y = 0; y < ySteps; ++y) {
            double yAngle = yAngleMin
                    + (yAngleMax - yAngleMin) * (ySteps == 1 ? 0.5 : (y / (double) (ySteps - 1)));
            Matrix4d matRotY = new Matrix4d();
            matRotY.setIdentity();
            matRotY.rotY(Math.toRadians(yAngle));
            additional_rotation.mul(matRotY, matRotX);
            BufferedImage bi = createBufferedImageFromRGB(draw(size, size, aam, aap));
            g2.drawImage(bi,
                    new AffineTransformOp(new AffineTransform(), AffineTransformOp.TYPE_NEAREST_NEIGHBOR),
                    (ySteps - 1 - y) * size, x * size);
        }
    }
    return grid;
}

From source file:components.FrameDemo2.java

protected static Image createFDImage() {
    //Create a 16x16 pixel image.
    BufferedImage bi = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);

    //Draw into it.
    Graphics g = bi.getGraphics();
    g.setColor(Color.BLACK);/* www .ja v a  2 s .c  o  m*/
    g.fillRect(0, 0, 15, 15);
    g.setColor(Color.RED);
    g.fillOval(5, 3, 6, 6);

    //Clean up.
    g.dispose();

    //Return it.
    return bi;
}

From source file:table.FrequencyTablePanel.java

public static BufferedImage createImage(JTable table) {
    JTableHeader tableHeaderComp = table.getTableHeader();
    int totalWidth = tableHeaderComp.getWidth();
    int totalHeight = tableHeaderComp.getHeight() + table.getHeight();
    BufferedImage tableImage = new BufferedImage(totalWidth, totalHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2D = (Graphics2D) tableImage.getGraphics();
    tableHeaderComp.paint(g2D);/* w  w  w. j  a v  a2 s. co m*/
    g2D.translate(0, tableHeaderComp.getHeight());
    table.paint(g2D);
    return tableImage;
}

From source file:com.klwork.common.utils.WebUtils.java

/**
 * ???//from  w ww. java 2 s. co m
 * @param request
 * @param response
 */
public static void VerificationCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    // 
    int width = 60, height = 20;
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    // ?
    Graphics g = image.getGraphics();

    // ??
    Random random = new Random();

    // 
    g.setColor(getRandColor(200, 250));
    g.fillRect(0, 0, width, height);

    // 
    g.setFont(new Font("Times New Roman", Font.PLAIN, 18));

    // 
    // g.setColor(new Color());
    // g.drawRect(0,0,width-1,height-1);

    // ?155?????
    g.setColor(getRandColor(160, 200));
    for (int i = 0; i < 155; i++) {
        int x = random.nextInt(width);
        int y = random.nextInt(height);
        int xl = random.nextInt(12);
        int yl = random.nextInt(12);
        g.drawLine(x, y, x + xl, y + yl);
    }

    String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQUSTUVWXYZ0123456789";
    // ????(4?)
    String sRand = "";
    for (int i = 0; i < 4; i++) {
        int start = random.nextInt(base.length());
        String rand = base.substring(start, start + 1);
        sRand = sRand.concat(rand);
        // ??
        g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
        g.drawString(rand, 13 * i + 6, 16);
    }

    // ??SESSION
    request.getSession().setAttribute("entrymrand", sRand);

    // 
    g.dispose();
    OutputStream out = response.getOutputStream();
    // ?
    ImageIO.write(image, "JPEG", out);

    out.flush();
    out.close();
}

From source file:com.liusoft.dlog4j.util.ImageUtils.java

/**
 * ?//www  . j  a v  a2s.c  om
 * @param orig_img
 * @param obj_filename
 * @param p_width
 * @param p_height
 * @throws IOException
 */
public static String createPreviewImage(InputStream orig_img, String obj_filename, int p_width, int p_height)
        throws IOException {
    String extendName = StringUtils.getFileExtend(obj_filename).toLowerCase();

    FileOutputStream newimage = null;
    InputStream fis = orig_img;
    try {
        if ("gif".equalsIgnoreCase(extendName)) {
            GifImage gifImage = GifDecoder.decode(fis);
            fis.close();
            fis = null;
            GifImage newGif = GifTransformer.resize(gifImage, p_width, p_height, false);
            newimage = new FileOutputStream(obj_filename);
            GifEncoder.encode(newGif, newimage);
        } else {
            BufferedImage orig_portrait = (BufferedImage) ImageIO.read(fis);
            fis.close();
            fis = null;
            // ?JPG?
            BufferedImage bi = new BufferedImage(p_width, p_height, BufferedImage.TYPE_INT_RGB);
            bi.getGraphics().drawImage(orig_portrait, 0, 0, p_width, p_height, null);
            if (!obj_filename.endsWith(".jpg"))
                obj_filename += ".jpg";
            newimage = new FileOutputStream(obj_filename);
            ImageIO.write(bi, "jpg", newimage);
        }
    } finally {
        if (newimage != null)
            newimage.close();
        if (fis != null)
            fis.close();
    }
    return obj_filename;
}

From source file:FrameDemo2.java

protected static Image createFDImage() {
    // Create a 16x16 pixel image.
    BufferedImage bi = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);

    // Draw into it.
    Graphics g = bi.getGraphics();
    g.setColor(Color.BLACK);//from w w w.  j  a  v a  2 s  .c  o m
    g.fillRect(0, 0, 15, 15);
    g.setColor(Color.RED);
    g.fillOval(5, 3, 6, 6);

    // Clean up.
    g.dispose();

    // Return it.
    return bi;
}

From source file:de.fhg.igd.swingrcp.SwingRCPUtilities.java

/**
 * Applies the given transparency mask to a buffered image. Always creates a
 * new buffered image containing an alpha channel. Copies the old image into
 * the new one and then sets the alpha pixels according to the given mask.
 * //from  ww  w. j  a  v  a  2 s. c  om
 * @param img the old image
 * @param mask the alpha mask
 * @return the new image with alpha channel applied
 * @throws IllegalArgumentException if the image's size does not match the
 *             mask's size
 */
public static BufferedImage applyTransparencyMask(BufferedImage img, ImageData mask) {
    if (mask.width != img.getWidth() || mask.height != img.getHeight()) {
        throw new IllegalArgumentException("Image size does not match the mask size");
    }

    // copy image and also convert to RGBA
    BufferedImage result = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics g = result.getGraphics();
    g.drawImage(img, 0, 0, null);

    WritableRaster alphaRaster = result.getAlphaRaster();
    int alpha0[] = new int[] { 0 };
    int alpha255[] = new int[] { 255 };
    for (int y = 0; y < img.getHeight(); y++) {
        for (int x = 0; x < img.getWidth(); x++) {
            alphaRaster.setPixel(x, y, mask.getPixel(x, y) == 0 ? alpha0 : alpha255);
        }
    }

    return result;
}

From source file:com.igormaznitsa.sciareto.ui.UiUtils.java

@Nonnull
public static Image makeBadgedRightBottom(@Nonnull final Image base, @Nonnull final Image badge) {
    final int width = Math.max(base.getWidth(null), badge.getWidth(null));
    final int height = Math.max(base.getHeight(null), badge.getHeight(null));
    final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    final Graphics gfx = result.getGraphics();
    try {/* w w  w.j  ava 2  s . co m*/
        gfx.drawImage(base, (width - base.getWidth(null)) / 2, (height - base.getHeight(null)) / 2, null);
        gfx.drawImage(badge, width - badge.getWidth(null) - 1, height - badge.getHeight(null) - 1, null);
    } finally {
        gfx.dispose();
    }
    return result;
}

From source file:com.igormaznitsa.sciareto.ui.UiUtils.java

@Nonnull
public static Image makeBadgedRightTop(@Nonnull final Image base, @Nonnull final Image badge) {
    final int width = Math.max(base.getWidth(null), badge.getWidth(null));
    final int height = Math.max(base.getHeight(null), badge.getHeight(null));
    final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    final Graphics gfx = result.getGraphics();
    try {//from   w  w w. j  a v a  2 s  .c  o m
        gfx.drawImage(base, (width - base.getWidth(null)) / 2, (height - base.getHeight(null)) / 2, null);
        gfx.drawImage(badge, width - badge.getWidth(null) - 1, 1, null);
    } finally {
        gfx.dispose();
    }
    return result;
}

From source file:de.fhg.igd.swingrcp.SwingRCPUtilities.java

/**
 * Create a SWT Image from an {@link ImageIcon}
 * //  w  ww. j  a  v a  2 s. c om
 * {@link "http://www.eclipseproject.de/modules.php?name=Forums&file=viewtopic&t=5489"}
 * {@link "http://www.9php.com/FAQ/cxsjl/java/2007/11/5033330101296.html"}
 * 
 * @param icon the {@link ImageIcon}
 * @return the SWT {@link ImageData}
 */
public static ImageData convertToSWT(ImageIcon icon) {
    BufferedImage img = GraphicsUtilities.createCompatibleTranslucentImage(icon.getIconWidth(),
            icon.getIconHeight());

    img.getGraphics().drawImage(icon.getImage(), 0, 0, null);

    return convertToSWT(img);
}