/*
 * @(#)AffineTransformUtil.java 1.0 2000/3/10
 * Copyright (c) 2000 Larry Rodrigues
 */
package com.vistech.util;
import java.awt.Shape;
import java.awt.image.*;
import java.awt.geom.*;
import java.io.*;
/** @version 1.0  10 March 2000
  * @author Lawrence Rodrigues
  */
public class AffineTransformUtil {
   public static Shape inverseTransform(Shape sh, AffineTransform atx) {
     try {
        AffineTransform invtx = atx.createInverse();
        GeneralPath path = new GeneralPath();
        PathIterator pi = sh.getPathIterator(new AffineTransform());

        while(!pi.isDone()) {
              double[] curcoord = new double[6];
              int segType = pi.currentSegment(curcoord);
              Point2D destPoint = null;
              double[] destArray = null;
              double[] dest = new double[6];
              switch (segType) {
                  case PathIterator.SEG_MOVETO:
                    destPoint = atx.inverseTransform(new Point2D.Double(curcoord[0],curcoord[1]),
                                                     destPoint);
                    path.moveTo((float)destPoint.getX(), (float)destPoint.getY());
                    break;
                  case PathIterator.SEG_LINETO:
                    destPoint = atx.inverseTransform(new Point2D.Double(curcoord[0], curcoord[1]),
                                                     destPoint);
                    path.lineTo((float)destPoint.getX(), (float)destPoint.getY());
                    break;
                  case PathIterator.SEG_QUADTO:
                    atx.inverseTransform(curcoord,0, dest, 0,2);

                    path.quadTo((float)dest[0], (float)dest[1],
                                (float)dest[2], (float)dest[3]);
                    break;
                  case PathIterator.SEG_CUBICTO:
                    atx.inverseTransform(curcoord,0, dest, 0,3);

                    path.curveTo((float)dest[0], (float)dest[1],
                                 (float)dest[2], (float)dest[3],
                                 (float)dest[4], (float)dest[5]);
                    break;
                  case PathIterator.SEG_CLOSE :
                    path.closePath();
                    break;
              }
              pi.next();
        }
        return (Shape)path;
     }catch(Exception e) {
       e.printStackTrace();
       return null;
     }
   }
}