The Rectangle class
In the drawPolygon method above, you will have noticed that the single argument was an instance of Polygon. Within the Java library, there are two classes that enable us to handle two-dimensional shapes efficiently. These are Rectangle, which deals with rectangles, and Polygon, which is more generalized and deals with any two dimensional shape. The advantage of using these classes is that they not only hold all of the relevant bits of information about a shape but also contain useful methods for comparing and manipulating shapes.
We will illustrate this with a brief look at the Rectangle class.
There are many constructors for Rectangle using a variety of arguments. Some of the common ones are:
// top left at (0, 0) width 0, height 0
Rectangle r1 = new Rectangle( );
// top left at (0, 0)
Rectangle r2 = new Rectangle(width, height);
// top left at (x, y)
Rectangle r3 = new Rectangle(x, y, width, height);
All of the above constructors have arguments that are integers. However, there are also constructors that use a Point argument. A Point is an object that can hold useful information (x- and y-values) and useful methods for manipulation. The simplest way to create a Point is:
Point p1 = new Point (20, 50);
After the above statement, p1 represents the point (20, 50). We can get access to the point's x- and y-coordinates using p1.x and p1.y respectively.
The constructor using the Point argument p1 can be invoked as follows:
Rectangle r4 = new Rectangle (p1);
This creates a rectangle with its top left-hand position at (20, 50) and both its width and height set to 0.
Table shows some useful methods and fields of the Rectangle class.
These are just a selection of the methods you can use for drawing. These and a number of other drawing methods can be found in the class java.awt.Graphics. Again, you will need to look at the Java reference material to see the full range of available methods.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your The Rectangle class homework and assignments? Live The Rectangle class experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer The Rectangle class homework help, java assignment help and The Rectangle class projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours