JavaFX | Dimension2D Class
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 Methodpublicstaticvoidmain(String args[]){// create a dimension objectDimension2D dimension =newDimension2D(20.0f,50.0f);doubleheight, width;// get the height and width of the dimension2Dheight = dimension.getHeight();width = dimension.getWidth();// display the height and width of dimension2DSystem.out.println("Height = "+ height +" Width = "+ width);}}chevron_rightfilter_noneOutput:
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 Methodpublicstaticvoidmain(String args[]){// create three dimension objectsDimension2D 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 dimensionsdisplay(dimension_1);display(dimension_2);display(dimension_3);// check whether any dimension is equal to other or notSystem.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 Methodpublicstaticvoiddisplay(Dimension2D dimension){doubleheight, width;// get the cheight and width of the dimensionheight = dimension.getHeight();width = dimension.getWidth();// display the height and width of dimension2DSystem.out.println("Height = "+ height +" Width = "+ width);}}chevron_rightfilter_noneOutput:
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
Recommended Posts:
- JavaFX | Tab Class
- JavaFX | Pos Class
- JavaFX | VBox Class
- JavaFX | FileChooser Class
- JavaFX | LinearGradient Class
- JavaFX | TextAlignment Class
- JavaFX | DirectoryChooser Class
- JavaFX | FontPosture Class
- JavaFX | Font Class
- JavaFX | Pane Class
- JavaFX | Group Class
- JavaFX | BorderPane Class
- JavaFX | AnchorPane Class
- JavaFX | FlowPane Class
- JavaFX | FontWeight Class
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



