6  Wednesday Afternoon:

6.1  Goals

Continue designing methods for self-referential and mutually referential data.

Introduce a simple drawing teachpack.

6.2  Simple Drawing

In order to draw the shapes like the circles, rectangles, and the combo shapes we have seen, we must have some place where the drawings will become visible. Doing so requires that we use a tool (teachpack) that provides the drawing surface and allows us to control its contents. We will first learn to draw basic shapes. We will then see how the drawing can change over the time (creating an animation), or in response to some events, such as a key press. These three features: the drawing canvas, the change over time, and the response to keyboard, mouse, or other events is at the core of many computer applications today.

To be able to see the drawing there must be a world (a stage, a canvas) where the drawing becomes visible. ProfessorJ provides such world through a library. To import the code form the library, start the code with the follwing import statement:

import draw.*;

This is the way we can use classes that have been defined elsewhere.

The colors

The library contains several classes such as Red, Black, Blue, Yellow, Green, White that represent colors for our drawings. So, if we want the circle to be shown in red, we must specify the red color as

new Red()

The World

The class World is the key class in this library. It provides a canvas into which we will draw, and methods that allow us to define what our program should do in response to various events, such as the user hitting a key or the timer advancing another tick ahead.

The class World contains the following useful methods for drawing and erasing basic colored shapes:

// draw a solid disk
boolean drawDisk(Posn center, int radius, Color c);

// draw a solid rectangle
boolean drawRect(Posn nw, int width, int height, Color c);

// draw a circle
boolean drawCircle(Posn center, int radius, Color c);

// draw a line
boolean drawLine(Posn from, Posn to, Color c);

// clear a solid disk
boolean clearDisk(Posn center, int radius, Color c);

// clear a solid rectangle
boolean clearRect(Posn nw, int width, int height, Color c);

// clear a circle
boolean clearCircle(Posn center, int radius, Color c);

// clear a line
boolean clearLine(Posn from, Posn to, Color c);

Before we start drawing, we create an instance of the World. We can then show and destroy the canvas that holds our drwaing by invoking the following methods:

// create a drawing canvas of the given size and show it
boolean start(int width, int height);

// destroy the drawing canvas
boolean stop();

Drawing shapes

To make it possible to draw shapes, we add convenience methods that will draw the shape instance in the given world to our classes that represent the geometry of shapes. For example, we may add the following methods to the class Circle:

  // draw this circle in the given World
  boolean draw(World w) {
    return w.drawDisk(this.center, this.radius, new Red());
  } 

  // clear this circle in the given World
  boolean clear(World w) {
    return w.clearDisk(this.center, this.radius, new Red());
  }