Prev Up Next
Go backward to Drawing Text
Go up to Drawing Methods
Go forward to Drawing Ovals

Drawing Rectangles


pen.fillRect(  left, top, width, height,  drawingColor )

pen.invertRect(  left, top, width, height )

pen.clearRect(  left, top, width, height )

pen.frameRect(  left, top, width, height,  drawingColor )

pen.invertRectFrame(  left, top, width, height )

pen.clearRectFrame(  left, top, width, height )

The number of methods provided for drawing rectangles is double that for lines and text. This is because when working with a rectangular area of the screen, one has the option of either just drawing (or clearing or inverting) the border of the rectangle or of filling the entire rectangular region.

Rectangles are particularly central to drawing within Java because, as we will see shortly, the drawing of circles and arcs is also accomplished by first specifying rectangular regions of the screen.

The most basic rectangle drawing method is "fillRect". By default, it fills a rectangular region of the screen with the current foreground color. Optionally, the last parameter to the method can be used to specify an alternate color to use when filling.

The rectangular region modified by "fillRect" and all the other rectangle drawing methods is specified by giving the coordinates of the upper left corner of the rectangle together with the rectangle's width and height. The two coordinate values are specified as the first two parameters with the width and height immediately after them.

If the four parameters that described the rectangle are omitted, "fillRect" and the other rectangle drawing primitives take the entire rectangle associated with the applet (or subcanvas) as their targets. Thus, one can paint the entire screen area of an applet red by simply saying:

pen.fillRect("Red");
or paint the entire applet region with the current foreground color by simply saying:
pen.fillRect();

As with the drawing methods discussed earlier, "drawRect" is accompanied by corresponding "clearRect" and "invertRect" methods. The former fills with the background color and the later reverses the colors of the pixels within a rectangle.

These three primitives are also associated with a matching set of primitives that affect just the border of a rectangle rather than its entire interior. The method "frameRect" can be used to color a one-pixel wide border of a rectangle. The methods "clearRectFrame" and "invertRectFrame" paint this border with the background color and reverse the colors of its pixels respectively.

To clarify the behavior of the primitives "fillRect" and "frameRect" an applet is included at the end of the section on oval drawing primitives that illustrates the behavior of both the rectangle and oval drawing methods.


Prev Up Next