Dimension2D class is a part of JavaFX. The Dimension2D class is an object that stores height and width. It inherits the java.lang.Object class.
Constructor of the class:
- Dimension2D(double width, double height): Creates a dimension object with specified width and height.
Commonly Used Methods:
| Method | Explanation |
|---|---|
| equals(Object obj) | Returns whether the two Dimension2D object are equal or not |
| getHeight() | Returns the height of the Dimension2d object |
| getWidth() | Returns the width of the Dimension2d object |
| hashCode() | Returns a hash code value for the Dimension2D object. |
Below programs will illustrate the use of the Dimension2D class:
- Java program to create a Dimension2D object and display its contents: This program creates a Dimension2D object named dimension. The height and width of the dimension is passed as arguments. Then the height and width are extracted from the object using getHeight() and getWidth() function and the respective values are displayed.
// Java program to create a Dimension2DÂ// object and display its contentsimportjavafx.geometry.*;ÂÂpublicclassDimension_1 {   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Â// create a dimension object       ÂDimension2D dimension =newDimension2D(20.0f,50.0f);       Âdoubleheight, width;       Â// get the height and width of the dimension2D       Âheight = dimension.getHeight();       Âwidth = dimension.getWidth();       Â// display the height and width of dimension2D       ÂSystem.out.println("Height = "+ height +" Width = "+ width);   Â}}Output:
Height = 50.0 Width = 20.0
- Java program to create 3 Dimension2D objects display its contents and how whether one object is equal to the other or not: This program creates three Dimension2D object named dimension_1, dimension_2, dimension_3. The height and width of the dimension is passed as arguments. Then the height and width are extracted from the object using getHeight() and getWidth() function and the respective values are displayed. The three objects are then compared using the equals() functions and the result is displayed.
// Java program to create 3 Dimension2D objects display// its contents and how whether one object is equal toÂ// the other or notimportjavafx.geometry.*;ÂÂpublicclassDimension_2 {   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Â// create three dimension objects       ÂDimension2D dimension_1 =newDimension2D(20.0f,50.0f);       ÂDimension2D dimension_2 =newDimension2D(120.0f,150.0f);       ÂDimension2D dimension_3 =newDimension2D(20.0f,50.0f);       Â// display the coordinates of the 3 dimensions       Âdisplay(dimension_1);       Âdisplay(dimension_2);       Âdisplay(dimension_3);       Â// check whether any dimension is equal to other or not       ÂSystem.out.println("Dimension 1 equals Dimension 2 = "                          Â+ dimension_1.equals(dimension_2));       ÂSystem.out.println("Dimension 2 equals Dimension 3 = "                           Â+ dimension_2.equals(dimension_3));       ÂSystem.out.println("Dimension 3 equals Dimension 1 = "                           Â+ dimension_3.equals(dimension_1));   Â}   Â// Display Method   Âpublicstaticvoiddisplay(Dimension2D dimension)   Â{       Âdoubleheight, width;       Â// get the cheight and width of the dimension       Âheight = dimension.getHeight();       Âwidth = dimension.getWidth();       Â// display the height and width of dimension2D       ÂSystem.out.println("Height = "+ height +" Width = "+ width);   Â}}Output:
Height = 50.0 Width = 20.0 Height = 150.0 Width = 120.0 Height = 50.0 Width = 20.0 Dimension 1 equals Dimension 2 = false Dimension 2 equals Dimension 3 = false Dimension 3 equals Dimension 1 = true
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/geometry/Dimension2D.html
