Handout A1.1  

DrawingTool Class Specifications

These classes are not part of Java but are available through the library named gpdraw. You must have the file gpdraw.jar in the appropriate directory where Java can access it. To have these classes available in your program, use this command:

import gpdraw.*;

Other features of gpdraw will be covered in later lessons.

DrawingTool

protected double xPos
protected double yPos
protected double direction;
protected int width;
protected boolean isDown;
protected Color color;
...

<<constructors>>

DrawingTool()
DrawingTool(SketchPad)
...

<<accessors>>

public Color getColor()
public double getDirection()

public int getWidth()
public String toString()
...

<<modifiers>>

public void down()
public void drawString(String)
public void drawCircle(double)
public void forward(double)
public void home()
public void move(double, double)
public void setColor(Color)
public void setDirection(double)
public void setWidth(int)
public void turn (double)
public void turnLeft(double)
public void turnRight(double)
public void up()
...

 

Invariant

A DrawingTool object

  • Appears in a SketchPad Window (this window is 250 pixels wide and 250 pixels high initially, but can be constructed with different dimensions.)
  • The origin (0, 0) is at the center of the drawing window.
  • Is directed either up, down, left, or right.
  • Is either in drawing mode or in moving mode.

Constructor Methods

public DrawingTool()

postcondition

  • A new DrawingTool is created and placed in the center (0, 0) of a SketchPad window that is 250 pixels wide and 250 pixels high.
  • This object is set to drawing mode.
  • The direction for this object is up (90º).
  • The DrawingTool color is set to blue.
  • The DrawingTool width is 1.

public DrawingTool(SketchPad win)

postcondition

  • A new DrawingTool is created and placed in the center (0, 0) of the SketchPad window win.
  • This object is set to drawing mode.
  • The direction for this object is up (90º).
  • The DrawingTool color is set to blue.
  • The DrawingTool width is 1.

Accessor Methods

public String toString();

postcondition
      result = color

public Color getColor();

postcondition
      result = color

public double getDirection();

postcondition
      result = direction

public int getWidth();

postcondition
      result = width

Modifier Methods

public void down();

    postcondition
  • This object is set to drawing mode.

public drawString(String text);

    postcondition
  • The string text is drawn at the current location using the current color.

public drawCircle (double r);

    postcondition
  • If the object is in drawing mode, a circle of radius r is drawn around the current location using the current width and color.

public forward(double distance);

    postcondition
  • This DrawingTool object is moved in the current direction by distance pixels from the old (previous) location.
  • If this object is in drawing mode, a line segment is drawn across the distance path just traversed.
  • A 0.5 second delay occurs following this method’s execution.

public home();

    postcondition
  • The location of the DrawingTool object is set to the center of the SketchPad window.
  • The drawing direction of the object is up.

public move(double x, double y);

    postcondition
  • This DrawingTool object is moved from the current position to the position specified by the coordinates x and y.
  • If this object is in drawing mode, a line segment is drawn from the old (previous) position to the absolute position specified by x and y.

public setColor(Color c);

    precondition
  • c is a valid Color
  • requires the following at the beginning of your program to set the color to red

    import java.awt.Color;
    .
    .
    setColor(Color.red);

    postcondition
  • The color of the DrawingTool object is set to c.

public setDirection(double d);

    postcondition
  • Sets the direction to d degrees. The orientation is d degrees counterclockwise from the positive x-axis

public setWidth(int w);

    precondition
  • w is >= 1
    postcondition
  • The width of the DrawingTool object is set to w pixels.

public turn(double d);

    postcondition
  • Changes the current direction counterclockwise by d degrees from the current direction.

public turnLeft(double degrees);

    postcondition
  • Changes the current direction counterclockwise by d degrees from the current direction.

public turnRight(double degrees);

    postcondition
  • Changes the current direction clockwise by d degrees from the current direction.

public up();

    postcondition
  • This object is set to moving mode.