Here you can find the source of drawImageInRect(Graphics g2d, BufferedImage img, Rectangle rect, double xfactor, double yfactor)
public static Rectangle drawImageInRect(Graphics g2d, BufferedImage img, Rectangle rect, double xfactor, double yfactor)
//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(); } }