Class Dimension in Java

0
159
Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Class Dimension in Java:

Dimension class has two public instance variables that describe the width and height of something. The width and height fields are public and may be manipulated directly. The Dimension class encapsulates the width and height of a component into a single object.

The class structure  of Class Dimension in Java is given as:


public class java.awt.Dimension extends java.lang.Object implements java.io.Serializable{
//member elements:
public int height;//height of the component
public int width;//width of the component
//constructors:
public Dimension();//creates a Dimension object with height and width as zero.
public Dimension(Dimension d);//Creates a Dimension whose width and height are
//same as provided in the given dimension.
public Dimension(int width,int height);//creates a Dimension with the specified 
//width and height.
//Method
public String toString();//Returns a String representation of the Dimension.
public boolean equals(Object obj); // Overrides Object
public Dimension getSize();
public void setSize(Dimension d);
public void setSize(int width, int height);
} 

The details of the class structure are given as follows:

public int height;

public int height represents the height of the Dimension.

public int width;

public int width represents the width of the Dimension.

public Dimension();

public Dimension() constructs an empty Dimension object.

public Dimension (int width, int height);

public Dimension (int width, int height) constructs a Dimension object with an initial dimension of width x height.

Parameter
width – Initial width of the object
height – Initial height of the object

public Dimension (Dimension d);

public Dimension (Dimension d) constructs a Dimension object that is a clone of d.
This method returns
Parameter
d – Initial dimensions of the object

public boolean equals (Object obj);

public boolean equals (Object obj) method compares two Dimension instances.

This method returns true if this Dimension is equivalent to obj; false otherwise.

Parameter
obj – The object to compare.

public Dimension getSize();

public Dimension getSize() method returns the size of the Dimension.

This method returns the size of the Dimension.

public void setSize (Dimension d);

public void setSize (Dimension d) method changes the size of the Dimension.

Parameter
d – The new size.

public void setSize (int width, int height);

public void setSize (int width, int height) method changes the size of the Dimension.

Parameter
width – The new width.
height – The new height.

public String toString();

public String toString() method returns a string representation of the Dimension object.

This method returns a string representation of the Dimension object.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

LEAVE A REPLY

Please enter your comment!
Please enter your name here