0

I an using GraphicsContext (gc) inside an AnimationTimer. I am scaling gc, drawing some objects, rotating gc and then drawing an image.

The image appears rotated as planned!

I need to be able to get the rotated rectangle that represents the image so I can check for proximity of the 4 (rotated) corners to other, non rotated objects.

I cannot see how to do this.

I have tried creating a rotated Rectangle to 'mirror' the objects rotation but I cannot see how to draw the Rectangle in the gc.

There is no gc.draw(Node), gc.draw(Rectangle) or gc.draw(Polygon). I need to draw it to confirm on screen that the two match.

My only other option is to break out the geometry set and rotate the points myself but it seem odd that javafx cannot do what I need.

Could somebody please help.

  • This seems like it would be simpler if you used the [scene graph](https://docs.oracle.com/javase/8/javafx/scene-graph-tutorial/scenegraph.htm) instead of a [canvas](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/canvas/Canvas.html). – jewelsea Mar 16 '15 at 23:15
  • [gc.fillRect(x,y,w,h)](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/canvas/GraphicsContext.html#fillRect-double-double-double-double-) is probably the equivalent of the `gc.draw(Rectangle)` method you are searching for but cannot find. – jewelsea Mar 16 '15 at 23:19
  • [gc.getTransform()](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/canvas/GraphicsContext.html#getTransform--) allows you to get the current graphics context transformation matrix. You can apply that transformation matrix to a co-ordinate using [transform.transform(x,y)](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/transform/Affine.html#transform-double-double-) to get the co-ordinates of a transformed point. – jewelsea Mar 16 '15 at 23:25
  • One you have the transformed points for the rectangle edges, you can use the Point2D class to check for proximity using [point.distance(x,y)](http://docs.oracle.com/javase/8/javafx/api/javafx/geometry/Point2D.html#distance-double-double-) between those points and other objects plotted on your graphics context. – jewelsea Mar 16 '15 at 23:27
  • I'm a little hesitant to write the code for this, but I guess you can take the information from these comments and code up an answer (if the information in the comments is indeed what you are looking to do). This previous solution might form a basis: [How to draw image rotated on JavaFX Canvas?](http://stackoverflow.com/questions/18260421/how-to-draw-image-rotated-on-javafx-canvas) – jewelsea Mar 16 '15 at 23:28
  • OK. I will have a look at transforming a point on the image around the image centre. Thanks for the quick reply:-) – Stuart Davies Mar 17 '15 at 00:03

0 Answers0