This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

AffineTransform - please help me out

 
Sunit Naik
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Please help me out
Thanks
 
Campbell Ritchie
Sheriff
Pie
Posts: 50270
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Pie
    Posts: 1535
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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 !
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic