Previous Table of Contents Next


6.2.7. Graphics

Icon supports a wide variety of graphics facilities. Shapes can be drawn, and text can be written in various fonts and styles in windows. Image files can be read and written, users can interact with windows using a mouse, and so on.

6.2.7.1. Windows

Here is a typical window, with a frame provided by the graphics system:

A window consists of a rectangular array of small dots called pixels. Pixels can be illuminated and colored. The window shown here is 500 pixels wide and 200 pixels high.

The upper-left pixel in a window has x,y coordinates (0,0). Pixel positions increase to the right (in the x-direction) and down (in the y-direction):

Note that the axes are arranged in a way that corresponds to reading and writing. For drawing, however, the orientation of the y axis is the opposite of the familiar Cartesian coordinate system.

Because pixel numbering starts at 0, the lower-right pixel in the window shown is numbered (499,199).

Windows are created in a manner similar to opening files. The function WOpen() opens a window. Its arguments specify attributes of the window. For example,

    WOpen(“size=500,200”)

opens a window that is 500 pixels wide and 200 pixels high like the one shown.

There are many other window attributes. There are defaults for all of them, and most can be changed after the window is opened.

A window has a background color and a foreground color. The background color initially fills the window. Drawing and writing text to a window are done in the foreground color, replacing pixels already in the window. The default background and foreground colors are white and black, respectively.

Other background and foreground colors can be specified when a window is open, using the attributes fg and bg. For example,

    WOpen(“size=500, 200”, “bg=dark gray”, “fg=white”)

produces the window:

Subsequent drawing and writing are done in white.

The function WAttrib() is used to change window attributes after a window has been opened. For example, the foreground color can be changed to black by

    WAttrib(“fg=black”)

Subsequent drawing and writing are done in black.

6.2.7.2. Drawing

Drawing can be used to produce points, lines, and various shapes in a window. Any portion of a shape that falls outside the boundaries of the window is not drawn.

Icon has a large repertoire of drawing functions, including

DrawPoint(x, y)—Draws a point (x, y).
DrawLine(x1, y1, x2, y2)—Draws a line from (x1,y1) to (x2,y2).
DrawRectangle(x, y, w, h)—Draws a rectangle whose upper-left corner is at (x,y) and whose width and height are w and h, respectively.
DrawPolygon(x1, y1, … xn, yn)—Draws a polygon with vertices at (x1,y1) … (xn,yn); if the first and last vertices are not the same, a line is drawn from the last to the first to close the shape.
DrawCircle(x, y, r)—Draws a circle of radius r with its center at (x,y).

For closed shapes, there are corresponding functions that fill the interior with the foreground color: FillRectangle(), FillPolygon(), and FillCircle().

All drawing functions take multiple sets of arguments to draw multiple shapes with a single call.

Here is a code segment that produces a simple drawing:

    WOpen(“size=400,400”)
    every i := 5 to 150 by 2 do
       DrawCircle(i + 50, i + 50, i)

The result is shown here:

The artifacts are produced by the moiré effect resulting from interference when attempting to draw on an array of pixels of fixed size with values of a higher resolution.

6.2.7.3. Writing to Windows

Writing to a window is similar to writing to a file, except the function used is WWrite() instead of write(). For example,

    WWrite(“ Earthling go home!”)

produces the following result on a window with a white background and a black foreground:

Character positions are numbered by rows and columns. The initial position is at character (1,1). Positions increase to the right as characters are written. WWrite() ends a line and advances the text position to the beginning of the next row. Text in a window scrolls when it reaches the bottom.

In the preceding example, the initial blank in the string written provides space so that the E does not touch the frame.

The size and appearance of text are determined by the font used. A font specification consists of a family that determines the font’s general appearance, a size in pixels that determines the text height, and style characteristics. The fonts available depend on the platform on which Icon runs. The font is specified by the font attribute, as in

    WAttrib(“font=Times,10,italic”)

which specifies the Times family in a 10-pixel size, italicized.

6.2.7.4. Color

Colors can be specified by name using a system based on Berk (1982). Examples are “red”, “greenish–blue”, and “light blue green”. The syntax of a color name is

The available hues are red, blue, green, cyan, yellow, magenta, violet, brown, orange, purple, pink, white, gray, and black. The items in brackets are optional modifiers. Words are separated by blanks or dashes, as indicated by the previous examples.

Names that fall outside this naming system are passed to the underlying graphics system, allowing the use of system-dependent color names.

Colors also can be specified numerically according to the amount of red, blue, and green light they contain. Amounts vary from 0 (no light) to 216–1, representing the maximum intensity. A numerical specification is given as a string in which the intensities are separated by commas. For example,

    WOpen(“bg=”0,0,50000”)

opens a window with a bright blue background.


Previous Table of Contents Next