12.2. Drawing Methods

There are a general set of methods that can be used to draw onto
the drawing area 'canvas'. These drawing methods can be used for any
gtk.gdk.Drawable subclass (either a
gtk.gdk.Window or a
gtk.gdk.Pixmap). The drawing methods are:

drawable.draw_point(gc, x, y)

gc is the Graphics Context to be used to do
the drawing.

x and y are the
coordinates of the point.

drawable.draw_line(gc, x1, y1, x2, y2)

gc is the Graphics Context.

x1 and y1 specify
the starting point of the line. x2 and
y2 specify the ending point of the line.

drawable.draw_rectangle(gc, filled, x, y, width, height)

where gc is the Graphics Context.

filled is a boolean indicating the
rectangle should be filled with the foreground color if
TRUE or not filled, if FALSE.

x and y are the top
left corner of the rectangle.

width and
height are the width and height of the
rectangle.

drawable.draw_arc(gc, filled, x, y, width, height, angle1, angle2)

gc is the Graphics Context.

filled is a boolean indicating the arc
should be filled with the foreground color if TRUE or not
filled, if FALSE.

x and y are the top
left corner of the bounding rectangle. width and
height are the width and height of the bounding
rectangle.

angle1 is the start angle of the arc,
relative to the 3 o'clock position, counter-clockwise, in 1/64ths of a
degree.

angle2 is the end angle of the arc,
relative to angle1, in 1/64ths of a degree counter
clockwise.

drawable.draw_polygon(gc, filled, points)

gc is the Graphics Context.

filled is a boolean indicating the polygon
should be filled with the foreground color if TRUE or not filled, if FALSE.

points is a list of coordinate pairs in
tuples e.g. [ (0,0), (2,5), (3,7), (4,11) ] of the points to be drawn as a
connected polygon.

width and height are
the width and height of the image bounding rectangle.

dith is the dither mode as described
below

For the draw_rgb_image() method,
rgb_buf is the RGB Image data packed in a string as a
sequence of 8-bit RGB pixel triplets. For the
draw_rgb_32_image() method,
buf is the RGB Image data packed in a string as a
sequence of 8-bit RGB pixel triplets with 8-bit padding (4 characters per
RGB pixel). For the draw_gray_image() method,
buf is the gray image data packed in a string as
8-bit pixel data.

rowstride is the number of characters from
the start of one row to the start of the next row of the image.
rowstride usually defaults to: 3 *
width for the
draw_rgb_image() method; 4 *
width for the
draw_rgb_32_image(); and,
width for the
draw_gray_image() method. If
rowstride is 0 the line will be replicated
height times.

The dither modes are:

RGB_DITHER_NONE # Never use dithering.
RGB_DITHER_NORMAL # Use dithering in 8 bits per pixel (and below) only.
RGB_DITHER_MAX # Use dithering in 16 bits per pixel and below.

The drawingarea.py
example program demonstrates the use of most of the
DrawingArea methods. It also puts the
DrawingArea inside a
ScrolledWindow and adds horizontal and vertical
Ruler widgets. Figure 12.1, “Drawing Area Example” shows
the program in operation: