Example usage for java.awt.geom AffineTransform transform

List of usage examples for java.awt.geom AffineTransform transform

Introduction

In this page you can find the example usage for java.awt.geom AffineTransform transform.

Prototype

public void transform(double[] srcPts, int srcOff, float[] dstPts, int dstOff, int numPts) 

Source Link

Document

Transforms an array of double precision coordinates by this transform and stores the results into an array of floats.

Usage

From source file:net.sf.mcf2pdf.mcfelements.util.ImageUtil.java

/**
 * Rotates the given buffered image by the given angle, and returns a newly
 * created image, containing the rotated image.
 *
 * @param img Image to rotate./*w  ww .  ja  v a  2  s  .c  o m*/
 * @param angle Angle, in radians, by which to rotate the image.
 * @param drawOffset Receives the offset which is required to draw the image,
 * relative to the original (0,0) corner, so that the center of the image is
 * still on the same position.
 *
 * @return A newly created image containing the rotated image.
 */
public static BufferedImage rotateImage(BufferedImage img, float angle, Point drawOffset) {
    int w = img.getWidth();
    int h = img.getHeight();

    AffineTransform tf = AffineTransform.getRotateInstance(angle, w / 2.0, h / 2.0);

    // get coordinates for all corners to determine real image size
    Point2D[] ptSrc = new Point2D[4];
    ptSrc[0] = new Point(0, 0);
    ptSrc[1] = new Point(w, 0);
    ptSrc[2] = new Point(w, h);
    ptSrc[3] = new Point(0, h);

    Point2D[] ptTgt = new Point2D[4];
    tf.transform(ptSrc, 0, ptTgt, 0, ptSrc.length);

    Rectangle rc = new Rectangle(0, 0, w, h);

    for (Point2D p : ptTgt) {
        if (p.getX() < rc.x) {
            rc.width += rc.x - p.getX();
            rc.x = (int) p.getX();
        }
        if (p.getY() < rc.y) {
            rc.height += rc.y - p.getY();
            rc.y = (int) p.getY();
        }
        if (p.getX() > rc.x + rc.width)
            rc.width = (int) (p.getX() - rc.x);
        if (p.getY() > rc.y + rc.height)
            rc.height = (int) (p.getY() - rc.y);
    }

    BufferedImage imgTgt = new BufferedImage(rc.width, rc.height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = imgTgt.createGraphics();

    // create a NEW rotation transformation around new center
    tf = AffineTransform.getRotateInstance(angle, rc.getWidth() / 2, rc.getHeight() / 2);
    g2d.setTransform(tf);
    g2d.drawImage(img, -rc.x, -rc.y, null);
    g2d.dispose();

    drawOffset.x += rc.x;
    drawOffset.y += rc.y;

    return imgTgt;
}

From source file:Clip.java

/**
 * Transform the clip contents. A new clip region will be created
 * which is the bounding box of the transformed region.
 * @param at the affine transform/*from  w  w  w . ja va  2  s  .c  om*/
 */
public void transform(AffineTransform at) {
    // make the extra corner points valid
    clip[2] = clip[0];
    clip[3] = clip[7];
    clip[4] = clip[6];
    clip[5] = clip[1];

    // transform the points
    at.transform(clip, 0, clip, 0, 4);

    // make safe against rotation
    double xmin = clip[0], ymin = clip[1];
    double xmax = clip[6], ymax = clip[7];
    for (int i = 0; i < 7; i += 2) {
        if (clip[i] < xmin)
            xmin = clip[i];
        if (clip[i] > xmax)
            xmax = clip[i];
        if (clip[i + 1] < ymin)
            ymin = clip[i + 1];
        if (clip[i + 1] > ymax)
            ymax = clip[i + 1];
    }
    clip[0] = xmin;
    clip[1] = ymin;
    clip[6] = xmax;
    clip[7] = ymax;
}

From source file:TransferableScribblePane.java

public PathIterator getPathIterator(final AffineTransform transform) {
    return new PathIterator() {
        int curseg = -1; // current segment

        // Copy the current segment for thread-safety, so we don't
        // mess up of a segment is added while we're iterating
        int numsegs = PolyLine.this.numsegs;

        public boolean isDone() {
            return curseg >= numsegs;
        }/*from ww w .  j a v a  2 s. co  m*/

        public void next() {
            curseg++;
        }

        // Get coordinates and type of current segment as floats
        public int currentSegment(float[] data) {
            int segtype;
            if (curseg == -1) { // First time we're called
                data[0] = x0; // Data is the origin point
                data[1] = y0;
                segtype = SEG_MOVETO; // Returned as a moveto segment
            } else { // Otherwise, the data is a segment endpoint
                data[0] = x0 + coords[curseg * 2];
                data[1] = y0 + coords[curseg * 2 + 1];
                segtype = SEG_LINETO; // Returned as a lineto segment
            }
            // If a tranform was specified, transform point in place
            if (transform != null)
                transform.transform(data, 0, data, 0, 1);
            return segtype;
        }

        // Same as last method, but use doubles
        public int currentSegment(double[] data) {
            int segtype;
            if (curseg == -1) {
                data[0] = x0;
                data[1] = y0;
                segtype = SEG_MOVETO;
            } else {
                data[0] = x0 + coords[curseg * 2];
                data[1] = y0 + coords[curseg * 2 + 1];
                segtype = SEG_LINETO;
            }
            if (transform != null)
                transform.transform(data, 0, data, 0, 1);
            return segtype;
        }

        // This only matters for closed shapes
        public int getWindingRule() {
            return WIND_NON_ZERO;
        }
    };
}

From source file:org.apache.fop.afp.AFPGraphics2D.java

/**
 * Handle the Batik drawing event/*from www .  jav a  2 s  .c  o m*/
 *
 * @param shape
 *            the shape to draw
 * @param fill
 *            true if the shape is to be drawn filled
 */
private void doDrawing(Shape shape, boolean fill) {
    if (!fill) {
        graphicsObj.newSegment();
    }

    graphicsObj.setColor(gc.getColor());

    applyPaint(gc.getPaint(), fill);

    if (fill) {
        graphicsObj.beginArea();
    } else {
        applyStroke(gc.getStroke());
    }

    AffineTransform trans = gc.getTransform();
    PathIterator iter = shape.getPathIterator(trans);
    if (shape instanceof Line2D) {
        double[] dstPts = new double[6];
        iter.currentSegment(dstPts);
        int[] coords = new int[4];
        coords[X1] = (int) Math.round(dstPts[X]);
        coords[Y1] = (int) Math.round(dstPts[Y]);
        iter.next();
        iter.currentSegment(dstPts);
        coords[X2] = (int) Math.round(dstPts[X]);
        coords[Y2] = (int) Math.round(dstPts[Y]);
        graphicsObj.addLine(coords);
    } else if (shape instanceof Rectangle2D) {
        double[] dstPts = new double[6];
        iter.currentSegment(dstPts);
        int[] coords = new int[4];
        coords[X2] = (int) Math.round(dstPts[X]);
        coords[Y2] = (int) Math.round(dstPts[Y]);
        iter.next();
        iter.next();
        iter.currentSegment(dstPts);
        coords[X1] = (int) Math.round(dstPts[X]);
        coords[Y1] = (int) Math.round(dstPts[Y]);
        graphicsObj.addBox(coords);
    } else if (shape instanceof Ellipse2D) {
        double[] dstPts = new double[6];
        Ellipse2D elip = (Ellipse2D) shape;
        double scale = trans.getScaleX();
        double radiusWidth = elip.getWidth() / 2;
        double radiusHeight = elip.getHeight() / 2;
        graphicsObj.setArcParams((int) Math.round(radiusWidth * scale), (int) Math.round(radiusHeight * scale),
                0, 0);
        double[] srcPts = new double[] { elip.getCenterX(), elip.getCenterY() };
        trans.transform(srcPts, 0, dstPts, 0, 1);
        final int mh = 1;
        final int mhr = 0;
        graphicsObj.addFullArc((int) Math.round(dstPts[X]), (int) Math.round(dstPts[Y]), mh, mhr);
    } else {
        processPathIterator(iter);
    }

    if (fill) {
        graphicsObj.endArea();
    }
}

From source file:org.apache.fop.render.pdf.pdfbox.PSPDFGraphics2D.java

private void transformCoords(float[] coords, Paint paint, boolean axialShading) {
    try {/*from w  w w .  j a  v  a 2  s .c  o m*/
        Field f = paint.getClass().getDeclaredField("matrix");
        f.setAccessible(true);
        Matrix ctm = (Matrix) f.get(paint);
        AffineTransform at = ctm.createAffineTransform();
        if (axialShading) {
            at.transform(coords, 0, coords, 0, 2);
        } else {
            at.transform(coords, 0, coords, 0, 1);
            at.transform(coords, 3, coords, 3, 1);
            coords[2] *= ctm.getScalingFactorX();
            coords[5] *= ctm.getScalingFactorX();
        }

    } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.pdfbox.pdmodel.graphics.shading.AxialShadingContext.java

/**
 * Constructor creates an instance to be used for fill operations.
 * /*from w ww .ja  v a  2  s  .c o m*/
 * @param shadingType2 the shading type to be used
 * @param colorModelValue the color model to be used
 * @param xform transformation for user to device space
 * @param ctm current transformation matrix
 * @param pageHeight height of the current page
 * 
 */
public AxialShadingContext(PDShadingType2 shadingType2, ColorModel colorModelValue, AffineTransform xform,
        Matrix ctm, int pageHeight) {
    coords = shadingType2.getCoords().toFloatArray();
    if (ctm != null) {
        // the shading is used in combination with the sh-operator
        float[] coordsTemp = new float[coords.length];
        // transform the coords from shading to user space
        ctm.createAffineTransform().transform(coords, 0, coordsTemp, 0, 2);
        // move the 0,0-reference
        coordsTemp[1] = pageHeight - coordsTemp[1];
        coordsTemp[3] = pageHeight - coordsTemp[3];
        // transform the coords from user to device space
        xform.transform(coordsTemp, 0, coords, 0, 2);
    } else {
        // the shading is used as pattern colorspace in combination
        // with a fill-, stroke- or showText-operator
        float translateY = (float) xform.getTranslateY();
        // move the 0,0-reference including the y-translation from user to device space
        coords[1] = pageHeight + translateY - coords[1];
        coords[3] = pageHeight + translateY - coords[3];
    }
    // colorSpace 
    try {
        PDColorSpace cs = shadingType2.getColorSpace();
        if (!(cs instanceof PDDeviceRGB)) {
            // we have to create an instance of the shading colorspace if it isn't RGB
            shadingColorSpace = cs.getJavaColorSpace();
        }
    } catch (IOException exception) {
        LOG.error("error while creating colorSpace", exception);
    }
    // colorModel
    if (colorModelValue != null) {
        colorModel = colorModelValue;
    } else {
        try {
            // TODO bpc != 8 ??  
            colorModel = shadingType2.getColorSpace().createColorModel(8);
        } catch (IOException exception) {
            LOG.error("error while creating colorModel", exception);
        }
    }
    // shading function
    try {
        function = shadingType2.getFunction();
    } catch (IOException exception) {
        LOG.error("error while creating a function", exception);
    }
    // domain values
    if (shadingType2.getDomain() != null) {
        domain = shadingType2.getDomain().toFloatArray();
    } else {
        // set default values
        domain = new float[] { 0, 1 };
    }
    // extend values
    COSArray extendValues = shadingType2.getExtend();
    if (shadingType2.getExtend() != null) {
        extend = new boolean[2];
        extend[0] = ((COSBoolean) extendValues.get(0)).getValue();
        extend[1] = ((COSBoolean) extendValues.get(1)).getValue();
    } else {
        // set default values
        extend = new boolean[] { false, false };
    }
    // calculate some constants to be used in getRaster
    x1x0 = coords[2] - coords[0];
    y1y0 = coords[3] - coords[1];
    d1d0 = domain[1] - domain[0];
    denom = Math.pow(x1x0, 2) + Math.pow(y1y0, 2);
    // TODO take a possible Background value into account

}

From source file:org.apache.pdfbox.pdmodel.graphics.shading.RadialShadingContext.java

/**
 * Constructor creates an instance to be used for fill operations.
 * //from ww w  . j  av  a  2s  .com
 * @param shadingType3 the shading type to be used
 * @param colorModelValue the color model to be used
 * @param xform transformation for user to device space
 * @param ctm current transformation matrix
 * @param pageHeight height of the current page
 * 
 */
public RadialShadingContext(PDShadingType3 shadingType3, ColorModel colorModelValue, AffineTransform xform,
        Matrix ctm, int pageHeight) {
    coords = shadingType3.getCoords().toFloatArray();
    if (ctm != null) {
        // the shading is used in combination with the sh-operator
        float[] coordsTemp = new float[coords.length];
        // transform the coords from shading to user space
        ctm.createAffineTransform().transform(coords, 0, coordsTemp, 0, 1);
        ctm.createAffineTransform().transform(coords, 3, coordsTemp, 3, 1);
        // move the 0,0-reference
        coordsTemp[1] = pageHeight - coordsTemp[1];
        coordsTemp[4] = pageHeight - coordsTemp[4];
        // transform the coords from user to device space
        xform.transform(coordsTemp, 0, coords, 0, 1);
        xform.transform(coordsTemp, 3, coords, 3, 1);
    } else {
        // the shading is used as pattern colorspace in combination
        // with a fill-, stroke- or showText-operator
        float translateY = (float) xform.getTranslateY();
        // move the 0,0-reference including the y-translation from user to device space
        coords[1] = pageHeight + translateY - coords[1];
        coords[4] = pageHeight + translateY - coords[4];
    }
    // colorSpace 
    try {
        PDColorSpace cs = shadingType3.getColorSpace();
        if (!(cs instanceof PDDeviceRGB)) {
            // we have to create an instance of the shading colorspace if it isn't RGB
            shadingColorSpace = cs.getJavaColorSpace();
        }
    } catch (IOException exception) {
        LOG.error("error while creating colorSpace", exception);
    }
    // colorModel
    if (colorModelValue != null) {
        colorModel = colorModelValue;
    } else {
        try {
            // TODO bpc != 8 ??  
            colorModel = shadingType3.getColorSpace().createColorModel(8);
        } catch (IOException exception) {
            LOG.error("error while creating colorModel", exception);
        }
    }
    // shading function
    try {
        function = shadingType3.getFunction();
    } catch (IOException exception) {
        LOG.error("error while creating a function", exception);
    }
    // domain values
    if (shadingType3.getDomain() != null) {
        domain = shadingType3.getDomain().toFloatArray();
    } else {
        // set default values
        domain = new float[] { 0, 1 };
    }
    // extend values
    COSArray extendValues = shadingType3.getExtend();
    if (shadingType3.getExtend() != null) {
        extend = new boolean[2];
        extend[0] = ((COSBoolean) extendValues.get(0)).getValue();
        extend[1] = ((COSBoolean) extendValues.get(1)).getValue();
    } else {
        // set default values
        extend = new boolean[] { false, false };
    }
    // calculate some constants to be used in getRaster
    x1x0 = coords[3] - coords[0];
    y1y0 = coords[4] - coords[1];
    r1r0 = coords[5] - coords[2];
    x1x0pow2 = Math.pow(x1x0, 2);
    y1y0pow2 = Math.pow(y1y0, 2);
    r0pow2 = Math.pow(coords[2], 2);
    denom = x1x0pow2 + y1y0pow2 - Math.pow(r1r0, 2);
    d1d0 = domain[1] - domain[0];
    // TODO take a possible Background value into account

}

From source file:org.apache.pdfbox.pdmodel.graphics.shading.ShadingContext.java

private void transformBBox(Matrix matrix, AffineTransform xform) {
    float[] bboxTab = new float[4];
    bboxTab[0] = bboxRect.getLowerLeftX();
    bboxTab[1] = bboxRect.getLowerLeftY();
    bboxTab[2] = bboxRect.getUpperRightX();
    bboxTab[3] = bboxRect.getUpperRightY();

    // transform the coords using the given matrix
    matrix.createAffineTransform().transform(bboxTab, 0, bboxTab, 0, 2);

    xform.transform(bboxTab, 0, bboxTab, 0, 2);
    minBBoxX = Math.min(bboxTab[0], bboxTab[2]);
    minBBoxY = Math.min(bboxTab[1], bboxTab[3]);
    maxBBoxX = Math.max(bboxTab[0], bboxTab[2]);
    maxBBoxY = Math.max(bboxTab[1], bboxTab[3]);
    if (minBBoxX >= maxBBoxX || minBBoxY >= maxBBoxY) {
        LOG.warn("empty BBox is ignored");
        bboxRect = null;//from w  w w . j  a  va 2s . c o m
    }
}

From source file:tufts.vue.LWComponent.java

/**
 * This will take the given zeroRect rectangle in local coordinates, and transform it
 * into map coordinates, setting mapRect and returning it.  If mapRect is null,
 * a new rectangle will be created and returned.
 *///from   ww w.  j  ava2 s .c om
public Rectangle2D transformZeroToMapRect(Rectangle2D zeroRect, Rectangle2D mapRect) {
    final AffineTransform tx = getZeroTransform();
    final double[] points = new double[4];

    points[0] = zeroRect.getX();
    points[1] = zeroRect.getY();
    points[2] = points[0] + zeroRect.getWidth();
    points[3] = points[1] + zeroRect.getHeight();
    tx.transform(points, 0, points, 0, 2);

    if (mapRect == null)
        mapRect = new Rectangle2D.Float();

    mapRect.setRect(points[0], points[1], points[2] - points[0], points[3] - points[1]);

    return mapRect;

    // Non-rotating & non-transform using version:
    //         final double scale = getMapScale();
    //         // would this be right? scale the x/y first?
    //         if (scale != 1) {
    //             rect.x *= scale;
    //             rect.y *= scale;
    //             rect.width *= scale;
    //             rect.height *= scale;
    //         }
    //         if (this instanceof LWLink) {
    //             // todo: eventually rewrite this routine entirely to use the transformations
    //             // (will need that if ever want to handle rotation, as well as to skip this
    //             // special case for links).
    //             rect.x += getParent().getMapX();
    //             rect.y += getParent().getMapY();
    //         } else {
    //             rect.x += getMapX();
    //             rect.y += getMapY();
    //         }

}