Java Draw Image drawImageInRect(Graphics g2d, BufferedImage img, Rectangle rect, double xfactor, double yfactor)

Here you can find the source of drawImageInRect(Graphics g2d, BufferedImage img, Rectangle rect, double xfactor, double yfactor)

Description

draw Image In Rect

License

Apache License

Declaration

public static Rectangle drawImageInRect(Graphics g2d, BufferedImage img, Rectangle rect, double xfactor,
            double yfactor) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.awt.Graphics;
import java.awt.Graphics2D;

import java.awt.Rectangle;

import java.awt.geom.AffineTransform;

import java.awt.geom.Point2D;

import java.awt.image.BufferedImage;

public class Main {
    public static Rectangle drawImageInRect(Graphics g2d, BufferedImage img, Rectangle rect, double xfactor,
            double yfactor) {
        int panelWt = rect.width;
        int panelHt = rect.height;
        // System.out.println("Poly Bounds: " + bounds.toString());

        int x = rect.x + (int) (panelWt * xfactor);
        int y = rect.y + (int) (panelHt * yfactor);
        g2d.drawImage(img, x, y, null);//  w ww. j a va2s .  c  o m
        Rectangle imgRect = new Rectangle(x, y, img.getWidth(), img.getHeight());
        return imgRect;
    }

    public static Rectangle drawImageInRect(Graphics g2d, BufferedImage img, Rectangle rect, double xfactor,
            double yfactor, AffineTransform affine) {
        int panelWt = rect.width;
        int panelHt = rect.height;
        // System.out.println("Poly Bounds: " + bounds.toString());
        Point2D.Double[] pt = new Point2D.Double[1];
        Point2D.Double[] transPt = new Point2D.Double[1];

        double x = rect.x + panelWt * xfactor;
        double y = rect.y + panelHt * yfactor;
        pt[0] = new Point2D.Double(x, y);
        affine.transform(pt, 0, transPt, 0, 1);
        g2d.drawImage(img, (int) transPt[0].x, (int) transPt[0].y, null);
        Rectangle imgRect = new Rectangle((int) transPt[0].x, (int) transPt[0].y, img.getWidth(), img.getHeight());
        return imgRect;
    }

    public static void drawImage(BufferedImage srcImg, BufferedImage img2Draw, int w, int h) {
        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();
    }
}

Related

  1. drawImage(Image image, Graphics g, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2)
  2. drawImage(Image img, double x, double y, Graphics2D graphics)
  3. drawImageClip(Graphics g, BufferedImage image, ImageObserver observer)
  4. drawImageInOval(Graphics2D gr, Image img, Rectangle r, boolean noShrink)
  5. drawImageInPolygon(Graphics g2d, BufferedImage img, Polygon poly, double xfactor, double yfactor)
  6. drawImageInRect(Graphics2D gr, Image img, Rectangle r, boolean noShrink)
  7. drawImageWithClipping(Graphics g, BufferedImage img)
  8. drawInMiddle(Graphics g, Image image, String text)
  9. drawLabel(BufferedImage image, String text)