Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

Sunit Naik
Greenhorn
Posts: 7
Hello Ranchers,

I am using AffineTransform class to rotate an image. I need to get hold of the image coordinates at each instant, so that I can determine when the image is hitting another object. Is there any way I can get - say something like the rectangular coordinates i.e. the 4 coordinates of the image ?

I am using g2d.drawImage(image, X, Y, this) to draw the image, but X,Y are always pointing to the top left coordinate of the image.

Thanks

Campbell Ritchie
Sheriff
Posts: 50749
83
Difficult. I would suggest you don't rotate the Graphics2D object, but take a clone of it, and rotate that.
That way you will have an un-rotated object to draw your next shape.
You can certainly get the size of an Image in terms of a rectangle, and work out its bounds.

Try
• Using the matrix given in the Graphics2D method to work out the 4 corners of the new image area
• Work out a Rectangle (or Rectangle2D) of the same size and rotation
• Try their intersects method with all the other shapes around.

• There is bound to be a better way of doing it, which I don't know.

Anybody else got a better idea??

CR

Craig Wood
Ranch Hand
Posts: 1535
There are many ways to approach this. You can construct transforms to both locate the
rotated image and to create a transformedShape for hit–testing.
Here's an illustration of the concept. Use/move the mouse to hit–test the rotated image.
If testing for collisions with other shapes you could use something like this

in the mouseDragged method or in (a method called from) your animation event code.

Sunit Naik
Greenhorn
Posts: 7
rotate.createTransformedShape(rect) did the trick for me, I was looking for something exactly like this

Thanks a lot . . both Campbell Ritchie and Craig Wood

Javaranch rocks !