Java Code Examples for java.awt.geom.AffineTransform#setToTranslation()

The following examples show how to use java.awt.geom.AffineTransform#setToTranslation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MatrixTestCase.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Tests {@link MatrixSIS#convertAfter(int, Number, Number)} using {@link AffineTransform}
 * as a reference implementation. This test can be run only with matrices of size 3×3.
 * Consequently it is sub-classes responsibility to add a {@code testConvertAfter()} method
 * which invoke this method.
 *
 * @param  matrix  the matrix of size 3×3 to test.
 *
 * @since 0.6
 */
final void testConvertAfter(final MatrixSIS matrix) {
    initialize(6501103578268988251L);
    final AffineTransform pre = new AffineTransform();
    final AffineTransform at = AffineTransform.getShearInstance(nextNonZeroRandom(), nextNonZeroRandom());
    matrix.setElement(0, 1, at.getShearX());
    matrix.setElement(1, 0, at.getShearY());
    for (int i=0; i<NUMBER_OF_REPETITIONS; i++) {
        final Number scale  = nextNonZeroRandom();
        final Number offset = nextNonZeroRandom();
        final int tgtDim = (i & 1);
        switch (tgtDim) {
            default: pre.setToIdentity();
                     break;
            case 0:  pre.setToTranslation(offset.doubleValue(), 0);
                     pre.scale(scale.doubleValue(), 1);
                     break;
            case 1:  pre.setToTranslation(0, offset.doubleValue());
                     pre.scale(1, scale.doubleValue());
                     break;
        }
        at.preConcatenate(pre);
        matrix.convertAfter(tgtDim, scale, offset);
        assertCoefficientsEqual(at, matrix);
    }
}
 
Example 2
Source File: StarsTransition2D.java    From pumpernickel with MIT License 5 votes vote down vote up
protected void fit(GeneralPath p, float length, float centerX,
		float centerY, GeneralPath path, Dimension size, float progress) {
	Rectangle2D r = p.getBounds2D();
	AffineTransform t = new AffineTransform();
	t.translate(-r.getX() - r.getWidth() / 2, -r.getY() - r.getHeight() / 2);
	t.rotate((1 - progress) * 1);
	double scaleProgress = Math.pow(progress, 3) * .75f;
	t.scale(length / r.getWidth() * (.02 + 1.8 * scaleProgress),
			length / r.getWidth() * (.02 + 1.8 * scaleProgress));
	p.transform(t);

	if (progress > 1)
		progress = 1;
	if (progress < 0)
		progress = 0;
	Point2D endPoint = ShapeUtils.getPoint(path, 1);
	Point2D startPoint = ShapeUtils.getPoint(path, progress);
	Rectangle2D pathBounds = ShapeBounds.getBounds(path);
	AffineTransform pathTransform = RectangularTransform.create(pathBounds,
			new Rectangle2D.Float(0, 0, size.width + 100, size.height));
	pathTransform.transform(endPoint, endPoint);
	pathTransform.transform(startPoint, startPoint);
	r = p.getBounds();
	t.setToTranslation(-r.getCenterX() + centerX - endPoint.getX()
			+ startPoint.getX(),
			-r.getCenterY() + centerY - endPoint.getY() + startPoint.getY());

	p.transform(t);
}
 
Example 3
Source File: NestWorldMapPane.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
private Graphics2D prepareGraphics2D(final float offsetX, Graphics2D g2d) {
    if (offsetX != 0.0f) {
        g2d = (Graphics2D) g2d.create();
        final AffineTransform transform = g2d.getTransform();
        final AffineTransform offsetTrans = new AffineTransform();
        offsetTrans.setToTranslation(+offsetX, 0);
        transform.concatenate(offsetTrans);
        g2d.setTransform(transform);
    }
    return g2d;
}
 
Example 4
Source File: StarsTransition2D.java    From Pixelitor with GNU General Public License v3.0 5 votes vote down vote up
protected static void fit(GeneralPath p, float length, float centerX, float centerY, GeneralPath path, Dimension size, float progress) {
    Rectangle2D r = p.getBounds2D();
    AffineTransform t = new AffineTransform();
    t.translate(-r.getX() - r.getWidth() / 2, -r.getY() - r.getHeight() / 2);
    t.rotate((1 - progress) * 1);
    double scaleProgress = Math.pow(progress, 3) * 0.75f;
    t.scale(length / r.getWidth() * (0.02 + 1.8 * scaleProgress), length / r
            .getWidth() * (0.02 + 1.8 * scaleProgress));
    p.transform(t);

    if (progress > 1) {
        progress = 1;
    }
    if (progress < 0) {
        progress = 0;
    }
    Point2D endPoint = ShapeUtils.getPoint(path, 1);
    Point2D startPoint = ShapeUtils.getPoint(path, progress);
    Rectangle2D pathBounds = ShapeBounds.getBounds(path);
    AffineTransform pathTransform = RectangularTransform.create(
            pathBounds,
            new Rectangle2D.Float(0, 0, size.width + 100, size.height)
    );
    pathTransform.transform(endPoint, endPoint);
    pathTransform.transform(startPoint, startPoint);
    r = p.getBounds();
    t.setToTranslation(-r.getCenterX() + centerX - endPoint.getX() + startPoint.getX(),
            -r.getCenterY() + centerY - endPoint.getY() + startPoint.getY());

    p.transform(t);
}
 
Example 5
Source File: RefractiveTransition2D.java    From pumpernickel with MIT License 4 votes vote down vote up
@Override
public Transition2DInstruction[] getInstructions(float progress,
		Dimension size) {
	List<Rectangle2D> v1 = new ArrayList<Rectangle2D>();
	List<Transition2DInstruction> v2 = new ArrayList<Transition2DInstruction>();

	float factor = .05f;

	float ySize = (size.height) * factor;
	float xSize = (size.width) * factor;
	for (float y = 0; y < size.height; y += ySize) {
		for (float x = 0; x < size.width; x += xSize) {
			v1.add(new Rectangle2D.Float(x, y, xSize, ySize));
		}
	}

	Point2D p1, p2;
	// 1 -> 0, 0 -> PI,
	float angleProgress = (float) ((1 - Math.pow(progress, .2)));
	v2.add(new ImageInstruction(true));
	for (int a = 0; a < v1.size(); a++) {
		try {
			Rectangle2D r = v1.get(a);
			p1 = new Point2D.Double(r.getCenterX(), r.getCenterY());
			AffineTransform transform = new AffineTransform();

			transform.setToRotation(-2 * Math.PI * angleProgress,
					size.width / 2, size.height / 2);
			transform.translate(size.width / 2, size.height / 2);
			transform.scale(progress, progress);
			transform.translate(-size.width / 2, -size.height / 2);

			p2 = new Point2D.Double();
			transform.transform(p1, p2);
			transform.setToTranslation(p2.getX() - p1.getX(), p2.getY()
					- p1.getY());
			v2.add(new ImageInstruction(false, (float) (Math.pow(progress,
					.4)), transform.createInverse(), r));

			transform.setToRotation(2 * Math.PI * angleProgress,
					size.width / 2, size.height / 2);

			p2 = new Point2D.Double();
			transform.transform(p1, p2);
			transform.setToTranslation(p2.getX() - p1.getX(), p2.getY()
					- p1.getY());
			v2.add(new ImageInstruction(false, progress * progress,
					transform.createInverse(), r));
		} catch (Exception e) {

		}
	}
	return v2.toArray(new Transition2DInstruction[v2.size()]);
}
 
Example 6
Source File: KaleidoscopeTransition2D.java    From pumpernickel with MIT License 4 votes vote down vote up
@Override
public Transition2DInstruction[] getInstructions(float progress,
		Dimension size) {
	List<Rectangle2D> v1 = new ArrayList<Rectangle2D>();
	List<Transition2DInstruction> v2 = new ArrayList<Transition2DInstruction>();

	float factor = .2f;

	float ySize = (size.height) * factor;
	float xSize = (size.width) * factor;
	for (float y = 0; y < size.height; y += ySize) {
		for (float x = 0; x < size.width; x += xSize) {
			v1.add(new Rectangle2D.Float(x, y, xSize, ySize));
		}
	}

	progress = 1 - progress;

	Point2D p1, p2;
	v2.add(new ImageInstruction(false));
	float dx = 10; // (float)(0*Math.cos(2*(1-progress)*Math.PI));
	float dy = 10; // (float)(0*Math.sin(2*(1-progress)*Math.PI));
	for (int a = 0; a < v1.size(); a++) {
		try {
			Rectangle2D r = v1.get(a);
			p1 = new Point2D.Double(r.getCenterX(), r.getCenterY());
			AffineTransform transform = new AffineTransform();

			transform.setToRotation(-2 * Math.PI
					* (1 - Math.sqrt(progress)), size.width / 2 + dx,
					size.height / 2 + dy);
			transform.translate(size.width / 2, size.height / 2);
			transform.scale(progress * progress, progress * progress);
			transform.translate(-size.width / 2, -size.height / 2);

			p2 = new Point2D.Double();
			transform.transform(p1, p2);
			transform.setToTranslation(p2.getX() - p1.getX(), p2.getY()
					- p1.getY());
			v2.add(new ImageInstruction(false, progress, transform
					.createInverse(), r));

			transform.setToRotation(-2 * Math.PI
					* (1 - Math.sqrt(progress)), size.width / 2 + dx,
					size.height / 2 + dy);

			p2 = new Point2D.Double();
			transform.transform(p1, p2);
			transform.setToTranslation(p2.getX() - p1.getX(), p2.getY()
					- p1.getY());
			v2.add(new ImageInstruction(true, progress * progress,
					transform.createInverse(), r));
		} catch (Exception e) {

		}
	}
	return v2.toArray(new Transition2DInstruction[v2.size()]);
}
 
Example 7
Source File: MirageTransition2D.java    From pumpernickel with MIT License 4 votes vote down vote up
@Override
public Transition2DInstruction[] getInstructions(float progress,
		Dimension size) {
	float ySize = (size.height) * .1f;
	float xSize = (size.width) * .1f;
	List<Rectangle2D> v = new ArrayList<Rectangle2D>();
	for (float y = 0; y < size.height; y += ySize) {
		for (float x = 0; x < size.width; x += xSize) {
			v.add(new Rectangle2D.Float(x, y, xSize, ySize));
		}
	}
	Transition2DInstruction[] instr = new Transition2DInstruction[2 * v
			.size()];
	Random random = new Random();
	Point2D center = new Point2D.Double(size.width / 2, size.height / 2);
	double max = Math.sqrt(size.width * size.width / 4 + size.height
			* size.height / 4);
	Point2D p1, p2;
	try {
		for (int a = 0; a < v.size(); a++) {
			float progress2 = (float) Math.pow(progress,
					.9 + .2 * random.nextFloat());
			Rectangle2D r = v.get(a);
			AffineTransform transform = new AffineTransform();
			Shape shape;

			transform.setToRotation(-Math.PI + Math.PI * (1 - progress2),
					size.width / 2, size.height / 2);
			p1 = new Point2D.Double(r.getCenterX(), r.getCenterY());
			p2 = new Point2D.Double();
			transform.transform(p1, p2);
			// transform.setToTranslation(p1.getX()-p2.getX(),p1.getY()-p2.getY());
			transform.setToTranslation(p2.getX() - p1.getX(), p2.getY()
					- p1.getY());
			shape = transform.createInverse().createTransformedShape(r);
			if (a == 0) {
				instr[a] = new ImageInstruction(true);
			} else {
				instr[2 * a + 0] = new ImageInstruction(true, 0, transform,
						shape);
			}
			transform.setToRotation(-1 * Math.PI * (1 - progress),
					size.width / 2, size.height / 2);
			transform.translate(size.width / 2, size.height / 2);
			transform.scale(1 / progress2, 1 / progress2);
			transform.translate(-size.width / 2, -size.height / 2);

			p1 = new Point2D.Double(r.getCenterX(), r.getCenterY());
			p2 = new Point2D.Double();
			transform.transform(p1, p2);
			// transform.setToTranslation(p1.getX()-p2.getX(),p1.getY()-p2.getY());
			transform.setToTranslation(p2.getX() - p1.getX(), p2.getY()
					- p1.getY());
			shape = r; // transform.createInverse().createTransformedShape(r);
			float progress3 = (float) ((1 - progress2) * (1 - p1
					.distance(center) / max));
			/**
			 * This doesn't look terrible if you don't use opacity... but it
			 * looks better with it...
			 * 
			 */
			// instr[2*a+1] = new
			// ImageInstruction(false,transform.createInverse(),shape);
			instr[2 * a + 1] = new ImageInstruction(false, 1 - progress3,
					transform.createInverse(), shape);
		}
	} catch (Exception e) {

	}
	return instr;
}
 
Example 8
Source File: SVGElement.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
static public AffineTransform parseSingleTransform(final String val) throws SVGException {
	final Matcher matchWord = Pattern.compile("[-.\\w]+").matcher("");

	final AffineTransform retXform = new AffineTransform();

	matchWord.reset(val);
	if (!matchWord.find()) {
		// Return identity transformation if no data present (eg, empty string)
		return retXform;
	}

	final String function = matchWord.group().toLowerCase();

	final LinkedList<String> termList = new LinkedList<>();
	while (matchWord.find()) {
		termList.add(matchWord.group());
	}

	final double[] terms = new double[termList.size()];
	final Iterator<String> it = termList.iterator();
	int count = 0;
	while (it.hasNext()) {
		terms[count++] = XMLParseUtil.parseDouble(it.next());
	}

	// Calculate transformation
	if (function.equals("matrix")) {
		retXform.setTransform(terms[0], terms[1], terms[2], terms[3], terms[4], terms[5]);
	} else if (function.equals("translate")) {
		retXform.setToTranslation(terms[0], terms[1]);
	} else if (function.equals("scale")) {
		if (terms.length > 1) {
			retXform.setToScale(terms[0], terms[1]);
		} else {
			retXform.setToScale(terms[0], terms[0]);
		}
	} else if (function.equals("rotate")) {
		if (terms.length > 2) {
			retXform.setToRotation(Math.toRadians(terms[0]), terms[1], terms[2]);
		} else {
			retXform.setToRotation(Math.toRadians(terms[0]));
		}
	} else if (function.equals("skewx")) {
		retXform.setToShear(Math.toRadians(terms[0]), 0.0);
	} else if (function.equals("skewy")) {
		retXform.setToShear(0.0, Math.toRadians(terms[0]));
	} else {
		throw new SVGException("Unknown transform type");
	}

	return retXform;
}
 
Example 9
Source File: RefractiveTransition2D.java    From Pixelitor with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Transition2DInstruction[] getInstructions(float progress,
                                                 Dimension size) {
    List<Rectangle2D> v1 = new ArrayList<>();
    List<Transition2DInstruction> v2 = new ArrayList<>();

    float factor = 0.05f;

    float ySize = size.height * factor;
    float xSize = size.width * factor;
    for (float y = 0; y < size.height; y += ySize) {
        for (float x = 0; x < size.width; x += xSize) {
            v1.add(new Rectangle2D.Float(x, y, xSize, ySize));
        }
    }


    Point2D p1, p2;
    // 1 -> 0, 0 -> PI,
    float angleProgress = (float) (1 - Math.pow(progress, 0.2));
    v2.add(new ImageInstruction(true));
    for (Rectangle2D rectangle2D : v1) {
        try {
            Rectangle2D r = rectangle2D;
            p1 = new Point2D.Double(r.getCenterX(), r.getCenterY());
            AffineTransform transform = new AffineTransform();

            transform.setToRotation(-2 * PI * angleProgress, size.width / 2, size.height / 2);
            transform.translate(size.width / 2, size.height / 2);
            transform.scale(progress, progress);
            transform.translate(-size.width / 2, -size.height / 2);

            p2 = new Point2D.Double();
            transform.transform(p1, p2);
            transform.setToTranslation(p2.getX() - p1.getX(), p2.getY() - p1.getY());
            v2.add(new ImageInstruction(false, (float) Math.pow(progress, 0.4), transform.createInverse(), r));

            transform.setToRotation(2 * PI * angleProgress, size.width / 2, size.height / 2);

            p2 = new Point2D.Double();
            transform.transform(p1, p2);
            transform.setToTranslation(p2.getX() - p1.getX(), p2.getY() - p1.getY());
            v2.add(new ImageInstruction(false, progress * progress, transform.createInverse(), r));
        } catch (Exception e) {

        }
    }
    return v2.toArray(new Transition2DInstruction[v2.size()]);
}
 
Example 10
Source File: KaleidoscopeTransition2D.java    From Pixelitor with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Transition2DInstruction[] getInstructions(float progress,
                                                 Dimension size) {
    List<Rectangle2D> v1 = new ArrayList<>();
    List<Transition2DInstruction> v2 = new ArrayList<>();

    float factor = 0.2f;

    float ySize = size.height * factor;
    float xSize = size.width * factor;
    for (float y = 0; y < size.height; y += ySize) {
        for (float x = 0; x < size.width; x += xSize) {
            v1.add(new Rectangle2D.Float(x, y, xSize, ySize));
        }
    }

    progress = 1 - progress;

    Point2D p1, p2;
    v2.add(new ImageInstruction(false));
    float dx = 10; //(float)(0*FastMath.cos(2*(1-progress)*Math.PI));
    float dy = 10; //(float)(0*FastMath.sin(2*(1-progress)*Math.PI));
    for (Rectangle2D rectangle2D : v1) {
        try {
            Rectangle2D r = rectangle2D;
            p1 = new Point2D.Double(r.getCenterX(), r.getCenterY());
            AffineTransform transform = new AffineTransform();

            transform.setToRotation(-2 * PI * (1 - Math
                    .sqrt(progress)), size.width / 2 + dx, size.height / 2 + dy);
            transform.translate(size.width / 2, size.height / 2);
            transform.scale(progress * progress, progress * progress);
            transform.translate(-size.width / 2, -size.height / 2);

            p2 = new Point2D.Double();
            transform.transform(p1, p2);
            transform.setToTranslation(p2.getX() - p1.getX(), p2.getY() - p1.getY());
            v2.add(new ImageInstruction(false, progress, transform.createInverse(), r));

            transform.setToRotation(-2 * PI * (1 - Math
                    .sqrt(progress)), size.width / 2 + dx, size.height / 2 + dy);

            p2 = new Point2D.Double();
            transform.transform(p1, p2);
            transform.setToTranslation(p2.getX() - p1.getX(), p2.getY() - p1.getY());
            v2.add(new ImageInstruction(true, progress * progress, transform.createInverse(), r));
        } catch (Exception e) {

        }
    }
    return v2.toArray(new Transition2DInstruction[v2.size()]);
}
 
Example 11
Source File: MirageTransition2D.java    From Pixelitor with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Transition2DInstruction[] getInstructions(float progress,
                                                 Dimension size) {
    float ySize = size.height * 0.1f;
    float xSize = size.width * 0.1f;
    List<Rectangle2D> v = new ArrayList<>();
    for (float y = 0; y < size.height; y += ySize) {
        for (float x = 0; x < size.width; x += xSize) {
            v.add(new Rectangle2D.Float(x, y, xSize, ySize));
        }
    }
    Transition2DInstruction[] instr = new Transition2DInstruction[2 * v.size()];
    Random random = new Random();
    Point2D center = new Point2D.Double(size.width / 2, size.height / 2);
    double max = Math.sqrt(size.width * size.width / 4 + size.height * size.height / 4);
    Point2D p1, p2;
    try {
        for (int a = 0; a < v.size(); a++) {
            float progress2 = (float) Math.pow(progress, 0.9 + 0.2 * random.nextFloat());
            Rectangle2D r = v.get(a);
            AffineTransform transform = new AffineTransform();
            Shape shape;

            transform.setToRotation(-PI + PI * (1 - progress2), size.width / 2, size.height / 2);
            p1 = new Point2D.Double(r.getCenterX(), r.getCenterY());
            p2 = new Point2D.Double();
            transform.transform(p1, p2);
            //transform.setToTranslation(p1.getX()-p2.getX(),p1.getY()-p2.getY());
            transform.setToTranslation(p2.getX() - p1.getX(), p2.getY() - p1.getY());
            shape = transform.createInverse().createTransformedShape(r);
            if (a == 0) {
                instr[a] = new ImageInstruction(true);
            } else {
                instr[2 * a + 0] = new ImageInstruction(true, 0, transform, shape);
            }
            transform.setToRotation(-1 * PI * (1 - progress), size.width / 2, size.height / 2);
            transform.translate(size.width / 2, size.height / 2);
            transform.scale(1 / progress2, 1 / progress2);
            transform.translate(-size.width / 2, -size.height / 2);

            p1 = new Point2D.Double(r.getCenterX(), r.getCenterY());
            p2 = new Point2D.Double();
            transform.transform(p1, p2);
            //transform.setToTranslation(p1.getX()-p2.getX(),p1.getY()-p2.getY());
            transform.setToTranslation(p2.getX() - p1.getX(), p2.getY() - p1.getY());
            shape = r; //transform.createInverse().createTransformedShape(r);
            float progress3 = (float) ((1 - progress2) * (1 - p1.distance(center) / max));
            /* This doesn't look terrible if you don't use opacity... but it looks better with it...

             */
            //instr[2*a+1] = new ImageInstruction(false,transform.createInverse(),shape);
            instr[2 * a + 1] = new ImageInstruction(false, 1 - progress3, transform.createInverse(), shape);
        }
    } catch (Exception e) {

    }
    return instr;
}