Is there a method for actually rotating the co-ordinates of 2D shapes?

Hallo

I'm trying to create a Graphics2D-powered program that needs to include polygons that can rotate. Now, as far as I can tell, affinetransform's methods will only ever transform the co-ordinate system of the User Space. So, for example, a GeneralPath object's co-ordinates will not change at all. Its useful only for rotating the Graphics2D display, not the underlying object.

But I want to actually transform the *co-ordinates* of the Shape - e.g. select a rotation point (the centroid) of a Shape, and get a new set of co-ordinates for its points that rotates it by x radians. This is because I'm not using it just for display - it will be used to check what points are inside it, and needs to stick to the sprite it's used by.

Is there any way of doing this - using a Shape object persistently, and just moving it and rotating it? Or do I need to make a new one every time, with new co-ordinates? Will I need to write my own rotate function? (Doable, just basic trig, but I kinda hoped there'd be a method somewhere...!)

Cheers...

[1074 byte] By [Dan.Aktivixa] at [2007-11-27 5:29:55]
# 1
You can use AffineTransform directly to transforn coordinate pairs, so at a pinch you can just transform all you verticies.
malcolmmca at 2007-7-12 14:53:31 > top of Java-index,Java Essentials,Java Programming...
# 2
Would you mind elaborating? Do you have an example?
Dan.Aktivixa at 2007-7-12 14:53:31 > top of Java-index,Java Essentials,Java Programming...
# 3
> Would you mind elaborating? Do you have an example?Would you mind first trying yourself?Here's the API doc: http://java.sun.com/j2se/1.5.0/docs/api/java/awt/geom/AffineTransform.htmlIf you have a specific question about it, feel free to post it.
prometheuzza at 2007-7-12 14:53:31 > top of Java-index,Java Essentials,Java Programming...
# 4

sorry, the 'would you mind' wasn't meant to sound snotty! its amazing how bad forums can be for getting feeling across. I meant to say something like 'oh hell, I can't see how to do this. I don't suppose you happen to have an example lying around the place, do you?'

I'll give it a go...

Dan.Aktivixa at 2007-7-12 14:53:31 > top of Java-index,Java Essentials,Java Programming...
# 5

> sorry, the 'would you mind' wasn't meant to sound

> snotty! its amazing how bad forums can be for getting

> feeling across. I meant to say something like 'oh

> hell, I can't see how to do this. I don't suppose you

> happen to have an example lying around the place, do

> you?'

>

> I'll give it a go...

I sounded a bit harsh perhaps. Sorry.

I only saw someone give an answer and a couple of minutes later a follow-up asking for "example code", which lead me to believe you did not do any research/googling on what was suggested. I now read your original post and saw that you already knew of the AffineTransform class.

Anyway, when I want to see some classes "in action" I always go to http://www.exampledepot.com . It also has some snippets on how to use the AffineTransform class:

http://www.google.com/custom?domains=exampledepot.com&q=AffineTransform&sa=Google+Search&sitesearch=exampledepot.com&client=pub-6001183370374757&forid=1&ie=ISO-8859-1&oe=ISO-8859-1&cof=GALT%3A%23008000%3BGL%3A1%3BDIV%3A%23336699%3BVLC%3A663399%3BAH%3Acenter%3BBGC%3AFFFFFF%3BLBGC%3A336699%3BALC%3A0000FF%3BLC%3A0000FF%3BT%3A000000%3BGFNT%3A0000FF%3BGIMP%3A0000FF%3BFORID%3A1%3B&hl=en

Good luck.

prometheuzza at 2007-7-12 14:53:31 > top of Java-index,Java Essentials,Java Programming...
# 6

Cheers both,

The exampledepot link really helped:

http://www.exampledepot.com/egs/java.awt.geom/TransformShape.html

Used that to come up with:

-

AffineTransform tx = new AffineTransform();

tx.rotate((picalc), x,y); [where x and y are the centroid anchor point of your shape]

GeneralPath newShape = new GeneralPath(tx.createTransformedShape(oldShape));

or

oldShape = new GeneralPath(tx.createTransformedShape(oldShape));

-

Which works - but isn't this rather a lot of processing, when you could (in theory, if someone wrote the methods!) pass an AffineTransform to an accessor method on the existing object?

Anyhoo, five points to each replier! Thanks muchly

Dan.Aktivixa at 2007-7-12 14:53:31 > top of Java-index,Java Essentials,Java Programming...