Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which Point2D should I use

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.

EDIT:

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, ...).

EDIT2:

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.

like image 458
bvdb Avatar asked Sep 09 '15 08:09

bvdb


People also ask

What is point 2D?

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.

What is Point2D in Javafx?

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.

What is the syntax to create a Point2D?

Point2D(double x, double y): Create a point2D object with specified x and y coordinates.


1 Answers

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.

like image 154
AlmasB Avatar answered Sep 21 '22 02:09

AlmasB