CS100A  CS100B   Fall 1998  Class Graphics

David Gries

Table of Contents

Introduction

One can draw lines, circles, ovals, rectangles, and so forth, as well as text, in a window called the "drawing window". The drawing is done using a "class" called Graphics. We outline the basics of drawing objects here. Class Graphics can do much more; we outline only its simplest functions here. For more information, read class Graphics, and also turn to page xx of the text by Homes.

Later, you will probably rely on the documentation for class Graphics when using it. For now, it will be easier to refer to this shorter document.

The pixels in the drawing window

Class Graphics contains procedures for drawing lines, circles, ovals, rectangles, and so forth, as well as text, in a window on the computer monitor. The window is viewed as a rectangular grid of points, called pixels (picture elements) and numbered shown below.
 

(0,0) 

(1,0) 

(2,0) 

(3,0) 

(4,0) 

... 

(0,1) 

(1.1) 

(2,1) 

(3,1) 

(4,1) 

... 

(0,2) 

(1,2) 

(2,2) 

(3,2) 

(4,2) 

... 

(0,3) 

(1,3) 

(2,3) 

(3,3) 

(4,3) 

... 

A pair (x,y) gives the x-coordinate and the y-coordinate of a pixel. The x-coordinate indicates horizontal distance from the left;the y-coordinate, vertical distance down. For example, the pixel labeled (4,2) is 4 pixels to the right and 2 units down..

The number of rows and columns is limited only by the size of the drawing window on the monitor screen. In fact, your program can request objects to drawn on the part of the window that can't be seen.

When you request the drawing of an object at position (3,1), say, the object is placed with its upper-left part at (3,1).

Methods for drawing objects in the drawing window

Class Graphics has a number of methods for drawing objects and text in the drawing window. We give headings for basic methods here. For information on other features and methods of class Graphics, turn to page ... Holmes.

// Set the color to use in future drawing operations to c. The
// possible values for c are Color.black, Color.blue, Color.cyan,
// Color.magenta, Color.darkGray, Color.gray, Color.green,
// Color.lightGray, Color.orange, Color.pink, Color.red,
// Color.white, and Color.yellow.
public void setColor(Color c)
Example call: setColor(Color.red);


// yield current color --the color being used to draw in the
// drawing window.
public Color getColor()
Example call: // Save the current color in c
                  Color c= getColor();
// Draw a line from (x1, y1) to (x2, y2), using the current color.
public void drawLine(int x1, int y1, int x2, int y2)
Example call: // Draw a line from (1,5) to (2,10)
                  drawLine(1, 5,  2, 10);
// Draw a circle an ellipse that fits exactly within the rectangle
// with top-left corner (x,y), width w, and height h. Use the
// current color. If h=w, the rectangle is a square, so the
// ellipse is a circle
public void drawOval(int x, int y, int w, int h)
Example call: // Draw a circle with center (15,20) and radius 5
                  drawOval(10, 15, 10, 10);


// Fill the ellipse that would be drawn by drawOval(x,y,w,h); with
// the current color.
public void fillOval(int x, int y, int w, int h)


// Draw the outline of a rectangle with width w and height h, with
// the left-top corner at point (x,y), using the current color.
public void drawRect(int x, int y, int w, int h)


// Fill the rectangle that would be drawn by drawRect(x,y,w,h);
// with the current color
public void fillRect(int x, int y, int w, int h)


// Write String s, using this current color, with the baseline of
// the first character of s point (x, y)
public void drawString(String s, int x, int y)


// yield a String (which can be printed) representation of the
// contents of thegraphics window
public String toString()