Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaJavaFX | Dimension2D Class

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:

  1. 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:

  1. 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 contents
    import javafx.geometry.*;
      
    public class Dimension_1 {
      
        // Main Method
        public static void main(String args[])
        {
      
            // create a dimension object
            Dimension2D dimension = new Dimension2D(20.0f, 50.0f);
      
            double height, 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
    
  2. 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 not
    import javafx.geometry.*;
      
    public class Dimension_2 {
      
        // Main Method
        public static void main(String args[])
        {
      
            // create three dimension objects
            Dimension2D dimension_1 = new Dimension2D(20.0f, 50.0f);
            Dimension2D dimension_2 = new Dimension2D(120.0f, 150.0f);
            Dimension2D dimension_3 = new Dimension2D(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
        public static void display(Dimension2D dimension)
        {
      
            double height, 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

RELATED ARTICLES

Most Popular

Recent Comments