Java Draw Image drawImageInPolygon(Graphics g2d, BufferedImage img, Polygon poly, double xfactor, double yfactor)

Here you can find the source of drawImageInPolygon(Graphics g2d, BufferedImage img, Polygon poly, double xfactor, double yfactor)

Description

draw Image In Polygon

License

Apache License

Declaration

public static Rectangle drawImageInPolygon(Graphics g2d, BufferedImage img, Polygon poly, 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.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();
    }
}

Related

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