CS100 Spring 1998

Class Graphics

and

Assignment 1

 

Table of Contents:

1. Introduction

2. The pixels in the drawing window

3. Basic methods for drawing objects: setColor, getColor, drawLine,

drawOval, fillOval, drawRect, fillRect, drawString.

1. 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. Here, we outline the basics of drawing objects. Class Graphics can do much more; we outline only its simplest functions. For more information, see Lewis/Loftus.

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.

2. 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 as shown below -- the diagram shows the first 4 rows and 5 columns of pixels. Notice that (0, 0) is in the upper left corner.

(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) ...

The column number is the horizontal coordinate, sometimes called the x-coordinate; the row number is the vertical coordinate, sometimes called the y-coordinate. For example, the pixel labeled (3,0) is in column 3, row 0; its x-coordinate is 3, and its y-coordinate is 0.

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 be drawn on a 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).

 

 

 

3. 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, see Lewis/Loftus.

// 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: // Set the current color to red

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 variable 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 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 drawOval(int x, int y, int w, int h)

Example call: // Draw a circle with center (15,20) and radius 2

drawLine(13, 18, 4, 4);

Note: The upper-left corner has x-coordinate 15 - 2 = 13, and y-coordinate 20 - 2 = 18. The width and height are each two times the radius, i.e. 4.

// Fill a solid ellipse, filled with the current color; x, y, w, h

// have the same meaning as in method drawOval.

public 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 public drawRect(int x, int y, int w, int h)

// Draw a solid rectangle using the current color; x, y, w, h

// have the same meaning as in method drawRect.

public public fillRect(int x, int y, int w, int h)

// Write String s, using the current color, with the baseline of

// the first character of s at the point (x, y)

public public drawString(String s, int x, int y)

Example call: // Print the string "Hi" at position 100, 150

drawString("Hi", 100, 150);