SUNY Geneseo, Department of Computer Science

Simple QuickDraw

Doug Baldwin
Last updated March 27, 1998

Introduction

QuickDraw is the two-dimensional graphics library on Macintosh computers. This document introduces some of the basic features of QuickDraw. These features are enough to draw basic pictures, but there's a lot of QuickDraw I don't discuss. For the full truth, see Apple Computer's Inside Macintosh series, particularly the volume entitled Imaging with QuickDraw. (The whole Inside Macintosh series is available on the World-Wide Web, the anchors above lead to the introductory page for the series, and the introductory page for Imaging with QuickDraw.)

QuickDraw consists of a collection of functions (for drawing things) and data types (for describing the things you want to draw). Any good Macintosh compiler will have declarations of these functions and types built in to it, so that you can use them in your programs without needing to write your own declarations for them, include special header files, etc. In the following, I introduce key types and functions through C++ declarations -- such declarations are a concise way to give you the information you need to use the functions and types, but they are not things that you will type directly into your own code. Note that many of the data types are described in C++ as "structs". Recall that a "struct" is basically an object with no messages, whose data members are all public for you to access as you wish.

To draw anything, you need to say where in your window you want it to appear. QuickDraw describes positions in windows as points in a two-dimensional coordinate system. All coordinates are measured in pixels (a pixel being a single dot on your display). The first dimension specifies horizontal positions, starting at 0 at the left side of the window and increasing as you move right. The second dimension specifies vertical position, starting at 0 at the top of the window and increasing as you move down (note that this is the opposite of how mathematicians traditionally think of Y coordinates). So, for example, the upper left corner of a window has coordinates (0,0). (I've left out lots of "fine print" about QuickDraw's coordinate system and how points, which are infinitesimally small, correspond to pixels, which aren't. You won't need to know this information at first, but if you ever do it is in the section of Imaging with QuickDraw entitled "QuickDraw's Coordinate Plane".)

Colors

At any given time, QuickDraw has two colors "at its fingertips": the "foreground" color and the "background" color. QuickDraw uses these colors just about as their names suggest. The foreground color is for drawing things that should be visible against the window's background, and the background color is for drawing that background. But beware that changing these colors does not change images or background already drawn, it just changes the colors that will be used for future operations.

struct RGBColor { int red; int green; int blue; }
This is the data type that represents colors. It represents a color by the intensities of red, green, and blue light that form that color. Each intensity can range from 0 to 65535.
void RGBForeColor( RGBColor* C )
Changes the foreground color for future drawing to C.
void RGBBackColor( RGBColor* C )
Changes the background color for future drawing to C

Lines

You draw lines by moving an imaginary pen to the place where you want the line to start, then dragging the pen to the place where you want the line to end.

void MoveTo( int H, int V )
Moves the pen (without drawing anything) to point (H, V).
void LineTo( int H, int V )
Draws from the pen's current position to point (H, V). Leaves the pen at this new position.

Rectangles

struct Rect { int left; int top; int right; int bottom; }
The data type that represents a rectangle. The rectangle's upper left corner is at point (left, top), and its lower right corner is at point (right, bottom).
void FrameRect( Rect* R )
Draws the outline of rectangle R in the current foreground color.
void PaintRect( Rect* R )
Fills rectangle R with the current foreground color.
void EraseRect( Rect* R )
Fills rectangle R with the current background color, effectively erasing it.

Ovals

Ovals, oddly enough, are defined by rectangles. The idea is that any oval can be defined by the rectangle that just fits around it (the oval's so-called "bounding rectangle"). Using bounding rectangles to define ovals means that you don't have to learn about a special data type just for defining ovals.

void FrameOval( Rect* R )
Draws the outline of an oval that has bounding rectangle R in the current foreground color.
void PaintOval( Rect* R )
Fills the oval whose bounding rectangle is R with the current foreground color.
void EraseOval( Rect* R )
Erases the oval whose bounding rectangle is R, by filling the oval with the current background color.

Input

Technically, QuickDraw displays graphics but doesn't do anything about graphical input from the user (e.g., mouse clicks or movement). You will find documentation on input of all sorts in the "Event Manager" chapter of Inside Macintosh. Macintosh input is fairly complex in its full generality, but some common kinds of input are surprisingly easy. In particular, the section of Inside Macintosh entitled "Reading the Mouse" explains how to do simple mouse input. Here are a couple of the most commonly used mouse input functions and data types:

struct Point { int v; int h; }
The data type that represents a point in a window. The v component is the point's coordinate in the vertical dimension, h is the coordinate in the horizontal dimension.
void GetMouse( Point* Where )
Sets the h and v components of Where to the current position of the mouse in the currently active window.
Boolean Button( void )
Returns True if the mouse button is down; returns False if it is up.

Wrapping Up

For examples of how to use these functions and data types, there is a "QuickDraw Demo" program in my CSci 219 folder, on the "CS Mac Server" file server.