The jdk8 contains 3 different Point2D
classes.
java.awt.geom.Point2D
in the rt.jar
javafx.geometry.Point2D
in the jfxrt.jar
com.sun.javafx.geom.Point2D
in the jfxrt.jar
Which Point2D
class should I use ?
Use/application: I want to perform geometric calculations to determine if a point intersects with a line. (e.g. Line2D.contains(Point2D)
)
Given the fact that I'm also using other javafx features (i.e. also in the javafx.*
package). My first guess, would be to use the javafx.geometry.Point2D
class. However while this package does contain a Point2D
class, it does not contain a Line2D
class, but the other 2 packages do contain a Line2D
package.
On the other hand, I don't want to pick a class that will be deprecated in the near future.
Perhaps a minor detail: the Point2D
class of the awt
and com.sun
package use float
's for defining their points, which requires a lot of casting. While the javafx
version uses double
, which is pretty convenient, since javafx also prefers double
for arranging components (e.g. getPrefWidth
, getLayoutX
, ...).
Actually the Line2D
class is not a big help. The contains
methods always return false
. So, it looks like I have to write my own intersect method anyway.
The Point2D class defines a point representing a location in (x,y) coordinate space. This class is only the abstract superclass for all objects that store a 2D coordinate. The actual storage representation of the coordinates is left to the subclass.
add(Point2D point) Returns a point with the coordinates of the specified point added to the coordinates of this point. double. angle(double x, double y) Computes the angle (in degrees) between the vector represented by this point and the specified vector.
Point2D(double x, double y): Create a point2D object with specified x and y coordinates.
java.awt
is a different UI toolkit to JavaFX. It is not recommended to mix classes from different libraries, especially provided that you already use JavaFX features.
Everything that starts with com.sun
should be avoided as it is private API and there is no guarantee that it will continue working in the next update.
Your best course of action in this scenario is to use javafx.geometry.Point2D
and implement your own Line2D
. As an alternative you can use the JavaFX scene graph and its Circle
(with radius of 0.5) and Line
(with stroke width of 1) classes to help you with your calculations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With