Java Draw Image drawImage(BufferedImage target, Point targetPoint, BufferedImage source)

Here you can find the source of drawImage(BufferedImage target, Point targetPoint, BufferedImage source)

Description

draw Image

License

Open Source License

Declaration

public static void drawImage(BufferedImage target, Point targetPoint, BufferedImage source) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

public class Main {
    public static void drawImage(BufferedImage target, Point targetPoint, BufferedImage source) {
        Rectangle area = new Rectangle(0, 0, source.getWidth(), source.getHeight());
        drawImage(target, targetPoint, source, area);
    }/* www  .  j a va 2 s  .  c o  m*/

    public static void drawImage(BufferedImage target, Point targetPoint, BufferedImage source,
            Rectangle sourceArea) {
        Rectangle copyFrom = (Rectangle) sourceArea.clone();
        Rectangle copyTo = new Rectangle(targetPoint.x, targetPoint.y, sourceArea.width, sourceArea.height);

        int d;

        if (copyTo.x < 0) {
            d = copyTo.x - 0;
            copyTo.x -= d;
            copyTo.width += d;
            copyFrom.x -= d;
            copyFrom.width += d;
        }
        if (copyTo.y < 0) {
            d = copyTo.y - 0;
            copyTo.y -= d;
            copyTo.height += d;
            copyFrom.y -= d;
            copyFrom.height += d;
        }

        if (copyTo.x + copyTo.width > target.getWidth()) {
            d = copyTo.x + copyTo.width - target.getWidth();
            copyTo.width -= d;
            copyFrom.width -= d;
        }
        if (copyTo.y + copyTo.height > target.getHeight()) {
            d = copyTo.y + copyTo.height - target.getHeight();
            copyTo.height -= d;
            copyFrom.height -= d;
        }

        int i = copyTo.x;
        int j = copyTo.y;
        int x1 = copyFrom.x;
        int y1 = copyFrom.y;
        int x2 = copyFrom.x + copyFrom.width;
        int y2 = copyFrom.y + copyFrom.height;

        for (int x = x1; x < x2; ++x) {
            for (int y = y1; y < y2; ++y) {
                target.setRGB(i, j++, source.getRGB(x, y));
            }

            ++i;
            j = copyTo.y;
        }
    }
}

Related

  1. drawCenteredImage(Rectangle2D bounds, Graphics2D g2, Image image, ImageObserver observer, int imageWidth, int imageHeight, boolean shrink)
  2. DrawCross(BufferedImage bi, Point pt)
  3. drawImage(BufferedImage bi, RenderedImage im)
  4. drawImage(BufferedImage originalImage, BufferedImage scaledImage, int width, int height)
  5. drawImage(BufferedImage srcImg, BufferedImage img2Draw, int w, int h)
  6. drawImage(final Graphics2D graphics, final Image image, final Rectangle rectangle)
  7. drawImage(Graphics g, BufferedImage im, Rectangle dst, Rectangle src)
  8. drawImage(Graphics graphics, Component comp, Image image)
  9. drawImage(Graphics2D g, BufferedImage image, int x, int y)