Constructing Objects
new Rectangle(5, 10, 20, 30)
Arguments initialize the data of the object
The new operator makes a Rectangle object
The new operator
returns an object and
is usually stored in a
variable,
for example, box, as below.
The process of creating a new object is called construction.
What is a constructor?
It is a special function.
It is called when a new object of the class is declared.
It provides a chance to initialize objects.
It always has the same name as the class in which it is defined.
It has no return type, even you can not write it as void.
Operator Overload:
Rectangle box2 = new Rectangle();
The box2 is located at the origin (0, 0), width 0, and height 0.
Modifier and Type | Method and Description |
---|---|
double |
getHeight() Returns the height of the bounding Rectangle in
double precision. |
double |
getWidth() Returns the width of the bounding Rectangle in
double precision. |
double |
getX() Returns the X coordinate of the bounding Rectangle
in double precision. |
double |
getY() Returns the Y coordinate of the bounding Rectangle
in double precision. |
void |
grow(int h, int v) Resizes the Rectangle both horizontally and
vertically. |
void |
move(int x, int y) Deprecated. As of JDK version 1.1, replaced by setLocation(int, int) . |
void |
resize(int width, int height) Deprecated. As of JDK version 1.1, replaced by setSize(int, int) . |
void |
setSize(int width, int height) Sets the size of this Rectangle to the specified
width and height. |
void |
translate(int dx, int dy) Translates this Rectangle the indicated distance,
to the right along the X coordinate axis, and downward along the Y
coordinate axis. |
The information to import to your work.