Rectangle
ClassPrograms can use Rectangle
objects to describe and draw four-sided
square and rectangular shapes.
Although the Rectangle
objects are meant to be displayed on
X11Pane displays that require only the origin and extent of a
rectangle to display it, the class defines each side’s line
separately, in case a program needs to work with all of the object’s
dimensions.
The methods drawWithPen
and fillWithPen
draw
either the sides of a rectangle or a filled rectangle, using
the line width and color defined by a Pen
object.
The methods draw
and fill
use a default line
width of one pixel and the color black.
Here is a simple example program.
int main () { X11Pane new xPane; InputEvent new e; Pen new bluePen; Rectangle new rectangle; xPane initialize 10, 10, 100, 100; xPane map; xPane raiseWindow; xPane openEventStream; /* * The rectangle's sides are four Line objects: * top, bottom, left, and right. There is also * a "dimensions" method that fills in all of * the sides' dimensions. */ rectangle top start x = 10; rectangle top start y = 10; rectangle right end x = 80; rectangle right end y = 80; bluePen width = 3; bluePen colorName = "blue"; while (TRUE) { xPane inputStream queueInput; if (xPane inputStream eventPending) { e become xPane inputStream inputQueue unshift; switch (e eventClass value) { case WINDELETE: xPane deleteAndClose; exit (0); break; case EXPOSE: case RESIZENOTIFY: /* * To draw only the outline of the rectangle, * use the "draw" method instead. */ rectangle fillWithPen xPane, bluePen; break; default: break; } } } }
top
right
bottom
left
Line
objects that contain the dimensions of each of the
rectangle’s sides. Each Line
instance variable in turn
contains Point
objects that contain the endpoints of
each side.
Note: Unlike Line
objects, which specify endpoints as
absolute window coordinates, Rectangle
objects specify the
endpoints of each side as relative to the origin of the
Rectangle
object; that is, as height and width dimensions.
See Line.
clear (X11Pane
paneObject)
Clear the rectangle defined by the receiver to the background color of
paneObject. This method requires that the dimensions of the
rectangle be completely defined. The easiest way to do this is with
the dimensions
method, below.
draw (X11Pane
paneObject)
Draw an outline of the receiver’s rectangle dimensions on paneObject’s display area, using a line width of one pixel and the color black.
drawWithPen (X11Pane
paneObject, Pen
penObject)
pDraw an outline of the receiver’s rectangle dimensions on paneObject’s display area, using the line width and color given by penObject.
dimensions (Integer
xOrigin, Integer
yOrigin, Integer
xSize, Integer
ySize)
A convenience method that fills in the dimensions of each of the receiver’s sides from the arguments, which specify the origin, height, and width of the receiver.
fill (X11Pane
paneObject)
Draw a solid rectangle on paneObject’s display area, using a default line width of one pixel and the default color, black.
fillWithPen (X11Pane
paneObject, Pen
penObject)
Draw a solid rectangle on paneObject’s display area, using the color given by penObject.