Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
JavaFX | Pane Class
Next article icon

JavaFX | Point2D Class

Last Updated : 30 Jul, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Point2D class is a part of JavaFX. This class defines a 2-dimensional point in space. The Point2D class represents a 2D point by its x, y coordinates. It inherits java.lang.Object class.
Constructor of the class: 
 

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


Commonly Used Methods:

MethodExplanation
distance(double x1, double y1)Calculates the distance between this point and point (x1, y1).
distance(Point2D p)Calculates the distance between this point and point p.
equals(java.lang.Object obj)Returns whether this object is equal to the specified object or not
getX()Returns the x coordinate of the point
getY()Returns the y coordinate of the point
hashCode()Returns a hash code value for the point.


Below programs will illustrate the use of the Point2D class: 
 

1.Java program to create a point 2D object and display its coordinates and find its distance from origin: In this program we create a Point2D object named point2d_1 by using its x, y coordinates as arguments. We will get the values of x, y using the getX(), getY() function and after that display it. We are also calculating the distance of the points from the origin.

Java

// Java program to create a point 2D
// object and display its coordinates
// and find its distance from origin
import javafx.geometry.Point2D;
 
public class Point2D_1 {
 
    // Main Method
    public static void main(String args[])
    {
 
        // Create a point2D object
        Point2D point2d_1 = new Point2D(20.0f, 150.0f);
 
        double x, y;
 
        // get the coordinates of the point
        x = point2d_1.getX();
        y = point2d_1.getY();
 
        // display the coordinates of the point
        System.out.println("x coordinate = " + x
                      + ", y coordinate = " + y);
 
        // print its distance from origin
        System.out.println("distance from origin = "
                        + point2d_1.distance(0, 0));
    }
}
                      
                       

Output: 

x coordinate = 20.0, y coordinate = 150.0 distance from origin = 151.32745950421557

2.Java program to create 3 Point2D objects and display their coordinates and distance from the origin and check which of the 3 points are similar and their distances between two points: In this program we create 3 Point2D object named point2d_1, point2d_2, point2d_3 by passing its x, y coordinates as arguments. We get the x, y values using the getX(), getY() function and then display it. We are also calculating the distance of the point from the origin and displaying it for each of the three points. We are also displaying whether any two points are equal or not using equals() function and the distance between two points using the distance() function.
 

Java

// Java program to create 3 Point2D objects and display
// their coordinates and distance from origin and
// check which of the 3 points are similar and
// their distances between two points
import javafx.geometry.Point2D;
 
public class Point2D_2 {
 
    // Main Method
    public static void main(String args[])
    {
 
        // Create three point2D objects
        Point2D point2d_1 = new Point2D(120.0f, 50.0f);
        Point2D point2d_2 = new Point2D(120.0f, 50.0f);
        Point2D point2d_3 = new Point2D(200.0f, 120.0f);
 
        // Display the coordinates of the 3 points
        display(point2d_1);
        display(point2d_2);
        display(point2d_3);
 
        // Check whether any point is equal to other or not
        System.out.println("Point 1 equals Point 2 = "
                        + point2d_1.equals(point2d_2));
 
        System.out.println("Point 2 equals Point 3 = "
                        + point2d_2.equals(point2d_3));
 
        System.out.println("Point 3 equals Point 1 = "
                        + point2d_3.equals(point2d_1));
 
        // distance between two points
        System.out.println("Distance between point 1 and point 2 = "
                                  + point2d_1.distance(point2d_2));
 
        System.out.println("Distance between point 2 and point 3 = "
                                   + point2d_2.distance(point2d_3));
 
        System.out.println("Distance between point 3 and point 1 = "
                                    + point2d_3.distance(point2d_1));
    }
 
    // display method
    public static void display(Point2D point2d)
    {
 
        double x, y;
 
        // get the coordinates of the point
        x = point2d.getX();
        y = point2d.getY();
 
        // display the coordinates of the point
        System.out.println("x coordinate = " + x
                      + ", y coordinate = " + y);
 
        // print its distance from origin
        System.out.println("Distance from origin = "
                          + point2d.distance(0, 0));
    }
}
                      
                       


Output: 

x coordinate = 120.0, y coordinate = 50.0 Distance from origin = 130.0 x coordinate = 120.0, y coordinate = 50.0 Distance from origin = 130.0 x coordinate = 200.0, y coordinate = 120.0 Distance from origin = 233.23807579381202 Point 1 equals Point 2 = true Point 2 equals Point 3 = false Point 3 equals Point 1 = false Distance between point 1 and point 2 = 0.0 Distance between point 2 and point 3 = 106.30145812734649 Distance between point 3 and point 1 = 106.30145812734649


Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javafx/2/api/javafx/geometry/Point2D.html
 



Next Article
JavaFX | Pane Class
author
andrew1234
Improve
Article Tags :
  • Java
  • JavaFX
Practice Tags :
  • Java

Similar Reads

  • JavaFX | Pos Class
    Pos class is a part of JavaFX. Pos class contains values which state the horizontal and vertical positioning or alignment. Pos class inherits Enum class. Commonly Used Methods: Method Explanation getHpos() Returns the horizontal alignment. getVpos() Returns the vertical alignment. valueOf(String n)
    4 min read
  • JavaFX | Light.Point Class
    Light.Point class is a part of JavaFX. Light.Point class represents a point light source in 3D space. Light.Point class extends the Light class. Constructors of the class: Point(): Creates a new Point Light object with default values. Point(double x, double y, double z, Color color): Creates a new P
    4 min read
  • JavaFX | LineTo class
    LineTo class is a part of JavaFX. LineTo class draws a line from the current position to specified x and y coordinate. LineTo class inherits PathElement class. Constructor of the class: LineTo(): Creates a new LineTo object. LineTo(double x, double y): Creates a new LineTo object with specified x, y
    4 min read
  • JavaFX | Pane Class
    Pane class is a part of JavaFX. Pane class acts as a base class of all layout panes. Basically, it fulfills the need to expose the children list as public so that users of the subclass can freely add/remove children. Pane class inherits Region class. StackPane must be used in case of an application
    4 min read
  • JavaFX | VLineTo Class
    VLineTo class is a part of JavaFX. VLineTo class creates a vertical line path from the current position to specified Y coordinate. VLineTo class inherits PathElement class. Constructor of the class: VLineTo(): Creates an empty object of VLineTo. VLineTo(double y): Creates an object of VLineTo with s
    4 min read
  • JavaFX | Popup Class
    Popup class is a part of JavaFX. Popup class creates a popup with no content, a null fill and is transparent. Popup class is used to display a notification, buttons, or a drop-down menu and so forth. The popup has no decorations. It essentially acts as a specialized scene/window which has no decorat
    4 min read
  • JavaFX | PieChart Class
    PieChart class is a part of JavaFX. PieChart class is used to create a pie chart. The chart content is populated by pie slices based on data set on the PieChart.The layout of the pie chart is set to clockwise by default. PieChart inherits Chart class.  Constructor of the class are: PieChart(): Creat
    4 min read
  • JavaFX | Slider Class
    A Slider is a Control in JavaFX which is used to display a continuous or discrete range of valid numeric choices and allows the user to interact with the control. A slider is rendered as a vertical or horizontal bar with a knob that the user can slide to indicate the desired value. A slider can also
    5 min read
  • JavaFX | SplitPane Class
    SplitPane class is a part of JavaFX. SplitPane class is a control which contains two or more sides separated by a divider. Sides can be dragged by the user to give more space to one of the sides which cause the other side to shrink by an equal amount. SplitPane class inherits Control class. Construc
    3 min read
  • JavaFX | Stop Class
    Stop class is a part of JavaFX. Stop class contains an offset and an color. Defines one element of the ramp of colors to use on a gradient. Stop class inherits Object class Constructor of the class: Stop(double o, Color c): Create a new Stop object with specified offset and color. Commonly Used Meth
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences