Here you can find the source of drawImageInPolygon(Graphics g2d, BufferedImage img, Polygon poly, double xfactor, double yfactor)
public static Rectangle drawImageInPolygon(Graphics g2d, BufferedImage img, Polygon poly, double xfactor, double yfactor)
//package com.java2s; //License from project: Apache License import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Polygon; import java.awt.Rectangle; import java.awt.image.BufferedImage; public class Main { public static Rectangle drawImageInPolygon(Graphics g2d, BufferedImage img, Polygon poly, double xfactor, double yfactor) { int[] xPts = poly.xpoints, yPts = poly.ypoints; int minXPt = xPts[0], minYPt = yPts[0], maxXPt = xPts[0], maxYPt = yPts[0]; // System.out.println("Pt: 0 x: " + xPts[0] + " y: " + yPts[0]); for (int i = 1; i < poly.npoints; i++) { // System.out.println("Pt: " + i + " x: " + xPts[i] + " y: " + // yPts[i]); if (xPts[i] < minXPt) minXPt = xPts[i];/* w w w. j a va 2 s. c o m*/ if (yPts[i] < minYPt) minYPt = yPts[i]; if (xPts[i] > maxXPt) maxXPt = xPts[i]; if (yPts[i] > maxYPt) maxYPt = yPts[i]; } int panelWt = maxXPt - minXPt, panelHt = maxYPt - minYPt; // System.out.println("MinXPt: " + minXPt + " MinYPt: " + minYPt); // System.out.println("MaxXPt: " + maxXPt + " MaxYPt: " + maxYPt); // System.out.println("Width: " + panelWt + " Height: " + panelHt); Rectangle bounds = poly.getBounds(); panelWt = bounds.width; panelHt = bounds.height; // System.out.println("Poly Bounds: " + bounds.toString()); panelWt = xPts[1] - xPts[0]; panelHt = yPts[2] - yPts[1]; // System.out.println("New Width: " + panelWt + " Height: " + panelHt); int x = xPts[0] + (int) (panelWt * xfactor); int y = yPts[0] + (int) (panelHt * yfactor); // System.out.println("xFact: " + xfactor + " yFact: " + yfactor); // System.out.println("Poly Wt: " + panelWt + " Ht: " + panelHt + " x: " // + x + " y: " + y); g2d.drawImage(img, x, y, null); Rectangle imgRect = new Rectangle(x, y, img.getWidth(), img.getHeight()); // System.out.println("Image: " + img.toString()); // System.out.println("Image Rect: " + imgRect.toString()); 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(); } }