2

I've been getting this class cast exception when trying to to implement an AffineTransform.

Call to AffineTransform:

public Shape moveToAndRotate(double x, double y, double theta)
{
    double cx = getBounds2D().getCenterX();
    double cy = getBounds2D().getCenterY();

    at.translate(cx, cy);
    at.translate(x, y);
    at.rotate(Math.toRadians(theta));
    at.translate(-cx, -cy);
    return at.createTransformedShape(yingYang);
}

This is resides in a custom shape class (YingYang).

public class YingYang implements Shape
{
    private Area yingYang = new Area();
    private AffineTransform at = new AffineTransform();
    ...
}

When ever I make a call I get a class cast exception when I try to cast this back to a YingYang either from the drawing panel or within the class it self (if I change the return type to YingYang.

I make the call like this:

YingYang newShape = (YingYang) shape.moveToAndRotate(newLoc1.x, newLoc1.y, theta);

This is the error:

java.lang.ClassCastException: java.awt.geom.Path2D$Double cannot be cast to Animation.YingYang

Any ideas since YingYang implements shape one would think that I shouldn't have to cast this at all. Am I missing a key concept?

jbolt
  • 688
  • 3
  • 16
  • 37
  • You left out critically important bits, like the definition of `at` and the variable `shape`. You need to show enough code for us to see what you're doing. – Jim Garrison Oct 27 '13 at 03:19
  • YingYang implements Shape...but `createTransformedShape` returns a Path2D -- that is, it's no long the YingYang instance, but a completely new shape object, unrelated to YingYang, which is why you can't cast it as such. – Ash Oct 27 '13 at 03:20
  • Notice what the error is: you're apparently trying to cast a [Path2D.Double](http://docs.oracle.com/javase/7/docs/api/java/awt/geom/Path2D.Double.html) to a YingYang, not a Path2D to a YingYang. – lealand Oct 27 '13 at 03:23
  • According to the API `createTransformedShape(Shape pSrc)` returns a Shape not a Path2D.Double – jbolt Oct 27 '13 at 03:39
  • @jbolt: Shape is just the interface, Path2D is the concrete implementation. createTransformedShape could choose whichever concrete implementation is appropriate for the transformation. The implementation can be any Shape, and you don't really need to know the implementation...unless you try and cast it as something else. See ns47731's answer, which I think sums up the situation pretty well. – Ash Oct 27 '13 at 04:06

1 Answers1

2

You are getting a class cast exception because you can only go up the inheritance tree. Meaning YinYang is a Shape but a Shape isnt necessarily a YinYang. createTransformedShape is returning a Path2D which is a Shape. But that Shape is not a YinYang.

You could either keep the variable yinYang = new Area(); inside your YinYang class or make your YinYang extend the area.

So intead of YinYang -> has an Area. It would be YinYang -> is an Area

If you really need to leave the extends inheritance open you can implement a shape and implement all the methods to go to the yinYang variable.

Then make a constructor like the following

private class YinYang extends Area {
    public YinYang(Shape shape) {
        super(shape);
    }
}

public Shape moveToAndRotate(double x, double y, double theta)
{
    double cx = getBounds2D().getCenterX();
    double cy = getBounds2D().getCenterY();

    at.translate(cx, cy);
    at.translate(x, y);
    at.rotate(Math.toRadians(theta));
    at.translate(-cx, -cy);
    return at.createTransformedShape(yingYang);
}

YingYang shape = new YingYang(shape.moveToAndRotate(newLoc1.x, newLoc1.y, theta));
ug_
  • 11,267
  • 2
  • 35
  • 52
  • I'm going to move to a new question since I now have a different problem and it seems the cast issue is solved. Now I get a problem with the shape returning zero bounds. The [(http://stackoverflow.com/questions/19615100/java-call-to-create-new-area-doesnt-create-shape)] question is here. – jbolt Oct 27 '13 at 05:58