Java Utililty Methods Draw Image

List of utility methods to do Draw Image

Description

The list of methods to do Draw Image are organized into topic(s).

Method

voiddrawBytes(BufferedImage image, byte[] buf)
draw Bytes
DataBuffer dataBuffer = image.getRaster().getDataBuffer();
for (int i = 0; i < buf.length; i += 2) {
    int pix = (buf[i] & 0xff) << 8 | (buf[i + 1] & 0xff);
    dataBuffer.setElem(i / 2, pix);
voiddrawCenterCrop(Graphics2D g, BufferedImage source, Rectangle dstRect)
Draws the given BufferedImage to the canvas, centered and cropped to fill the bounds defined by the destination rectangle, and with preserved aspect ratio.
final int srcWidth = source.getWidth();
final int srcHeight = source.getHeight();
if (srcWidth * 1.0 / srcHeight > dstRect.width * 1.0 / dstRect.height) {
    final int scaledWidth = dstRect.height * srcWidth / srcHeight;
    final int scaledHeight = dstRect.height;
    Image scaledImage = scaledImage(source, scaledWidth, scaledHeight);
    g.drawImage(scaledImage, dstRect.x, dstRect.y, dstRect.x + dstRect.width, dstRect.y + dstRect.height,
            0 + (scaledWidth - dstRect.width) / 2, 0, 0 + (scaledWidth - dstRect.width) / 2 + dstRect.width,
...
voiddrawCenteredImage(Rectangle2D bounds, Graphics2D g2, Image image, ImageObserver observer, int imageWidth, int imageHeight, boolean shrink)
draw Centered Image
int x = (int) bounds.getX();
int y = (int) bounds.getY();
int w = Math.min(imageWidth, (int) bounds.getWidth());
int h = Math.min(imageHeight, (int) bounds.getHeight());
if (shrink && ((imageWidth > bounds.getWidth()) || (imageHeight > bounds.getHeight()))) {
    double scaleWidth = bounds.getWidth() / imageWidth;
    double scaleHeight = bounds.getHeight() / imageHeight;
    if (scaleWidth < scaleHeight) {
...
voidDrawCross(BufferedImage bi, Point pt)
Draw Cross
final int max_r = 50, min_r = 10, color = 0xffffff;
for (int x = pt.x - max_r; x < pt.x - min_r; x++) {
    if (x >= 0 && x < bi.getWidth())
        bi.setRGB(x, pt.y, color);
for (int x = pt.x + min_r; x < pt.x + max_r; x++) {
    if (x >= 0 && x < bi.getWidth())
        bi.setRGB(x, pt.y, color);
...
voiddrawImage(BufferedImage bi, RenderedImage im)
draw Image
Graphics2D g = bi.createGraphics();
g.drawRenderedImage(im, new AffineTransform());
g.dispose();
voiddrawImage(BufferedImage originalImage, BufferedImage scaledImage, int width, int height)
draw Image
Graphics2D g = scaledImage.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(originalImage, 0, 0, width, height, 0, 0, originalImage.getWidth(), originalImage.getHeight(),
        null);
g.dispose();
voiddrawImage(BufferedImage srcImg, BufferedImage img2Draw, int w, int h)
draw Image
if (w == -1)
    w = (int) (srcImg.getWidth() / 2);
if (h == -1)
    h = (int) (srcImg.getHeight() / 2);
System.out.println("AWT Image Wt: " + w + " And Ht: " + h);
Graphics2D g2 = srcImg.createGraphics();
g2.drawImage(img2Draw, w, h, null);
g2.dispose();
...
voiddrawImage(BufferedImage target, Point targetPoint, BufferedImage source)
draw Image
Rectangle area = new Rectangle(0, 0, source.getWidth(), source.getHeight());
drawImage(target, targetPoint, source, area);
voiddrawImage(final Graphics2D graphics, final Image image, final Rectangle rectangle)
draw Image
final int imageWidth = image.getWidth(null);
final int imageHeight = image.getHeight(null);
graphics.drawImage(image, rectangle.x, rectangle.y, rectangle.x + rectangle.width,
        rectangle.y + rectangle.height, 0, 0, imageWidth, imageHeight, null);
voiddrawImage(Graphics g, BufferedImage im, Rectangle dst, Rectangle src)
draw Image
g.drawImage(im, dst.x, dst.y, dst.x + dst.width, dst.y + dst.height, src.x, src.y, src.x + src.width,
        src.y + src.height, null);