The Java Course provides a general introduction to programming in Java. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details.


Rectangles

Rectangles are similar to points, except that they have four instance variables, named x, y, width and height. Those names should sound familiar; they are the names of the variables we used to specify bounding boxes for the Mickey Mouse fractal in Section 4.14.

Other than that, everything is pretty much the same.

    Rectangle box = new Rectangle (0, 0, 100, 200);

creates a new Rectangle object and makes box refer to it. The figure shows the effect of this assignment.

If you print box, you get

java.awt.Rectangle[x=0,y=0,width=100,height=200]

Again, this is the result of a built-in Java method that knows how to print Rectangle objects.



Last Update: 2011-01-24