Java AWT | Dimension Class

Dimension class is a part of Java AWT. It contains the height and width of a component in integer as well as double precision. The use of Dimension class is that many functions of Java AWT and Swing return dimension object.

Constructors of the Dimension class

  1. Dimension() : It will create a new Object with height and width set to zero.
  2. Dimension(Dimension d) : It will create a new Object with same height and width as of the specified object.
  3. Dimension(int w, int h) : It will create a new object with specified height and width.

Methods of Dimension Class



method explanantion
equals(Object o) checks whether the two dimension object are equal or not.
getHeight() returns the height of the dimension object
getWidth() returns the width of the dimension object
getSize() returns the size of dimension object.
hashCode() returns the hashcode for dimension.
setSize(Dimension d) set size of the object to specified dimension
setSize(double width, double height) set the height and width to specified double value
setSize(int width, int height) set the height and width to specified integer value

Below programs illustrate the Dimension Class :

  • Program to show the functions of dimension class(Integer precision).
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to show the functions
    // of dimension class(Integer precision)
    import java.awt.*;
    class dimen {
      
        // Main Method
        public static void main(String args[])
        {
      
            // create dimension
            Dimension d = new Dimension();
            Dimension d1 = new Dimension(20, 30);
            Dimension d2 = new Dimension(d1);
      
            // set height and width of dimension d
            d.setSize(30, 30);
      
            // equating dimensions
            System.out.println("Dimension d and d1 "
                        "are equal = " + d.equals(d1));
                          
            System.out.println("Dimension d and d1 "
                    "are equal = " + d1.equals(d2));
      
            // print hashcode
            System.out.println("Hashcode of Dimension "
                                "d = " + d.hashCode());
      
            // display dimension
            display(d, "Dimesnion d");
            display(d1, "Dimesnion d1");
            display(d2, "Dimesnion d2");
        }
      
        // display dimension
        public static void display(Dimension d, String s)
        {
            System.out.println(s +" Height = " + d.getHeight() + 
                                    " Width= " + d.getWidth());
        }
    }

    chevron_right

    
    

    Output:

    Dimension d and d1 are equal = false
    Dimension d and d1 are equal = true
    Hashcode of Dimension d = 1860
    Dimesnion d Height = 30.0 Width= 30.0
    Dimesnion d1 Height = 30.0 Width= 20.0
    Dimesnion d2 Height = 30.0 Width= 20.0
    
  • Program to show the functions of dimension class(Double precision).
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to show the functions
    // of dimension class(Double precision)
    import java.awt.*;
      
    class dimen {
          
        // Main Method
        public static void main(String args[])
        {
              
            // create dimension
            Dimension d = new Dimension();
            Dimension d1 = new Dimension(20, 30);
            Dimension d2 = new Dimension(d1);
      
            // set height and width of dimension d
            d.setSize(30.3, 30.45);
      
            // equating dimensions
            System.out.println("Dimension d and d1"
                    "are equal = " + d.equals(d1));
                          
            System.out.println("Dimension d and d1"
                    "are equal = " + d1.equals(d2));
      
            // print hashcode
            System.out.println("Hashcode of Dimension d = "
                                        + d.hashCode());
      
            // display dimension
            System.out.println("See the values are rounded"
                            "off to ceiling in dimension d");
            display(d, "Dimesnion d");
            display(d1, "Dimesnion d1");
            display(d2, "Dimesnion d2");
        }
      
        // display dimension
        public static void display(Dimension d, String s)
        {
            System.out.println(s + " Height = " + d.getHeight() 
                                + " Width = " + d.getWidth());
        }
    }

    chevron_right

    
    

    Output:

    Dimension d and d1are equal = false
    Dimension d and d1are equal = true
    Hashcode of Dimension d = 1984
    See the values are roundedoff to ceiling in dimension d
    Dimesnion d Height = 31.0 Width = 31.0
    Dimesnion d1 Height = 30.0 Width = 20.0
    Dimesnion d2 Height = 30.0 Width = 20.0
    

Reference :https://docs.oracle.com/javase/7/docs/api/java/awt/Dimension.html



My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.