Differences between revisions 6 and 7
Revision 6 as of 2009-12-10 15:52:37
Size: 4856
Editor: JonathanWood
Comment:
Revision 7 as of 2010-03-15 12:30:12
Size: 0
Editor: JonathanWood
Comment: confusing usage for new users
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Example Usage:

{{{
Element target = ...

target = TransformUtil.translate(target, 10, 20);
...

target = TransformUtil.scale(target, .42);
...
}}}


Wrap, modify, and/or package to your taste...


{{{
package com.vizflicks.util;

import java.awt.geom.AffineTransform;
import org.apache.batik.parser.AWTTransformProducer;
import org.apache.batik.parser.TransformListParser;
import org.w3c.dom.Element;

/**
 *
 * @author Jonathan Wood
 */
public final class TransformUtil {

     private static final String TRANSFORM_LITERAL = "transform";

    public static final AffineTransform getAffineTransform(Element element) {
        TransformListParser p = new TransformListParser();
        AWTTransformProducer tp = new AWTTransformProducer();
        p.setTransformListHandler(tp);
        p.parse(element.getAttributeNS(null, TRANSFORM_LITERAL));
        return tp.getAffineTransform();
    }

    public static final Element translate(Element element, double x, double y) {
        AffineTransform at = getAffineTransform(element);
        at = translate(at, x, y);
        element.setAttributeNS(null, TRANSFORM_LITERAL, getTransform(at));
        return element;
    }

    public static final Element scale(Element element, double scale) {
        AffineTransform at = getAffineTransform(element);
        at = scale(at, scale);
        element.setAttributeNS(null, TRANSFORM_LITERAL, getTransform(at));
        return element;
    }

    public static final Element scale(Element element, double x, double y) {
        AffineTransform at = getAffineTransform(element);
        at = scale(at, x, y);
        element.setAttributeNS(null, TRANSFORM_LITERAL, getTransform(at));
        return element;
    }

    public static final Element rotate(Element element, double angle) {
        AffineTransform at = getAffineTransform(element);
        at = rotate(at, angle);
        element.setAttributeNS(null, TRANSFORM_LITERAL, getTransform(at));
        return element;
    }

    public static final Element rotate(Element element, double x, double y) {
        AffineTransform at = getAffineTransform(element);
        at = rotate(at, x, y);
        element.setAttributeNS(null, TRANSFORM_LITERAL, getTransform(at));
        return element;
    }

    public static final Element rotate(Element element, double x, double y, double ax, double ay) {
        AffineTransform at = getAffineTransform(element);
        at = rotate(at, x, y, ax, ay);
        element.setAttributeNS(null, TRANSFORM_LITERAL, getTransform(at));
        return element;
    }

    public static final Element shear(Element element, double x, double y) {
        AffineTransform at = getAffineTransform(element);
        at = shear(at, x, y);
        element.setAttributeNS(null, TRANSFORM_LITERAL, getTransform(at));
        return element;
    }

    public static final AffineTransform rotate(AffineTransform at, double angle) {
        at.concatenate(AffineTransform.getRotateInstance(angle));
        return at;
    }

    public static final AffineTransform rotate(AffineTransform at, double x, double y) {
        at.concatenate(AffineTransform.getRotateInstance(x, y));
        return at;
    }

    public static final AffineTransform rotate(AffineTransform at, double x, double y, double ax, double ay) {
        at.concatenate(AffineTransform.getRotateInstance(x, y, ax, ay));
        return at;
    }

    public static final AffineTransform scale(AffineTransform at, double scale) {
        at.concatenate(AffineTransform.getScaleInstance(scale, scale));
        return at;
    }

    public static final AffineTransform scale(AffineTransform at, double x, double y) {
        at.concatenate(AffineTransform.getScaleInstance(x, y));
        return at;
    }

    public static final AffineTransform translate(AffineTransform at, double x, double y) {
        at.concatenate(AffineTransform.getTranslateInstance(x, y));
        return at;
    }

    public static final AffineTransform shear(AffineTransform at, double x, double y) {
        at.concatenate(AffineTransform.getShearInstance(x, y));
        return at;
    }

    public static final String getTransform(AffineTransform at) {
        double[] matrix = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
        at.getMatrix(matrix);
        StringBuilder sb = new StringBuilder();
        sb.append("matrix(");
        sb.append(matrix[0]);
        sb.append(" ");
        sb.append(matrix[1]);
        sb.append(" ");
        sb.append(matrix[2]);
        sb.append(" ");
        sb.append(matrix[3]);
        sb.append(" ");
        sb.append(matrix[4]);
        sb.append(" ");
        sb.append(matrix[5]);
        sb.append(")");
        return sb.toString();
    }
}
}}}