gpdraw
Class DrawingTool

java.lang.Object
  extended by gpdraw.DrawingTool

public class DrawingTool
extends java.lang.Object

DrawingTool is a graphics environment that provides methods for a programmer to draw pictures by moving an object (called a DrawingTool) around on the screen The center of the graphics window is at location 0,0. Positive X is to the right; positive Y is up. Headings (angles) are measured in degrees counterclockwise from the positive X axis.

Example: ======= DrawingTool pencil; pencil = new DrawingTool(); // draw a square pencil.forward(75); pencil.turnLeft(90); pencil.forward(75); pencil.turnLeft(90); pencil.forward(75); pencil.turnLeft(90); pencil.forward(75);


Field Summary
protected  java.awt.Color color
           
protected  double direction
           
protected  boolean isDown
           
protected  SketchPadPanel pad
           
protected  int width
           
protected  double xPos
           
protected  double yPos
           
 
Constructor Summary
DrawingTool()
          Constructor.
DrawingTool(long delay)
          Constructor.
DrawingTool(SketchPad win)
          Constructor.
 
Method Summary
 void backward(double distance)
          This DrawingTool object is moved backward from current direction by distance pixels from the old (previous) location.
 void down()
          This object is set to drawing mode.
 void drawCircle(double radius)
          If the object is in drawing mode, Draws a circle of radius radius around the current location using the current width and color.
protected  void drawLine(double x, double y)
           
 void drawOval(double boundingBoxWidth, double boundingBoxHeight)
          Draws the outline of an oval.
 void drawRect(double recWidth, double recHeight)
          Draws the outline of the specified rectangle.
 void drawString(java.lang.String text)
          Draw the string text at the current location using the current color.
 void fillCircle(double radius)
          If the object is in drawing mode, Draws and fills a circle of radius radius around the current location using the current pen width and color.
 void fillOval(double boundingBoxWidth, double boundingBoxHeight)
          Fills the specified oval.
 void fillRect(double recWidth, double recHeight)
          Fills the specified rectangle.
 void forward(double distance)
          This DrawingTool object is moved forward from current direction by distance pixels from the old (previous) location.
 java.awt.Color getColor()
          Gets the color of the DrawingTool
 double getDirection()
          Gets the direction of this DrawingTool.
 SketchPadPanel getPadPanel()
          Returns the SketchPadPanel object for this DrawingTool.
 java.awt.geom.Point2D.Double getPosition()
          Gets the x and y coordinates of the current postion of this DrawingTool.
 int getWidth()
          Get the drawing width of the DrawingTool
 double getXPos()
          Gets the x coordinate of the current postion of this DrawingTool.
 double getYPos()
          Gets the y coordinate of the current postion of this DrawingTool.
 void home()
          The location of the DrawingTool object is set to the center of the SketchPad window with the drawing direction of the object set to up.
protected  void initialize(SketchPadPanel spadPanel)
          Initializes the state of this DrawingTool.
 boolean isDown()
          Get the drawing width of the DrawingTool
 void move(double distance)
          This DrawingTool object is moved in the current direction by distance pixels from the old (previous) location.
 void move(double x, double y)
          This DrawingTool object is moved from the current position to the position specified by the coordinates x and y.
 void setColor(java.awt.Color c)
          The color of the DrawingTool object is set to c.
 void setDirection(double deg)
          Sets the direction to d degrees.
 void setWidth(int width)
          Sets the width of the DrawingTool object is to width pixels.
 java.lang.String toString()
          Returns a string representation of this object.
 void turn(double degrees)
          Changes the current direction counterclockwise by degrees degrees from the current direction
protected  void turn(double x, double y)
           
 void turnLeft()
          Changes the current direction counterclockwise by 90 degrees from the current direction
 void turnLeft(double degrees)
          Changes the current direction counterclockwise by degrees degrees from the current direction
 void turnRight()
          Changes the current direction clockwise by 90 degrees from the current direction
 void turnRight(double degrees)
          Changes the current direction clockwise by degrees degrees from the current direction
 void up()
          This object is set to moving mode.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

xPos

protected double xPos

yPos

protected double yPos

direction

protected double direction

width

protected int width

isDown

protected boolean isDown

color

protected java.awt.Color color

pad

protected SketchPadPanel pad
Constructor Detail

DrawingTool

public DrawingTool()
Constructor. 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 black. The DrawingTool width is 1.


DrawingTool

public DrawingTool(long delay)
Constructor. 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 black. The DrawingTool width is 1.The drawing speed (in milliseconds) is determined by the delay parameter

Parameters:
delay - delay (ms) between DrawingTool drawing movements

DrawingTool

public DrawingTool(SketchPad win)
Constructor. 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 black. The DrawingTool width is 1.

Method Detail

initialize

protected void initialize(SketchPadPanel spadPanel)
Initializes the state of this DrawingTool.

Parameters:
spadPanel - SketchPadPanel that contains this DrawingTool

getColor

public java.awt.Color getColor()
Gets the color of the DrawingTool

Returns:
current color of this DrawingTool

getWidth

public int getWidth()
Get the drawing width of the DrawingTool

Returns:
current width of the DrawingTool

isDown

public boolean isDown()
Get the drawing width of the DrawingTool

Returns:
true if this DrawingTool is in drawing mode (down); false otherwise

getXPos

public double getXPos()
Gets the x coordinate of the current postion of this DrawingTool.

Returns:
x coordinate of this DrawingTool

getYPos

public double getYPos()
Gets the y coordinate of the current postion of this DrawingTool.

Returns:
y coordinate of this DrawingTool

getPosition

public java.awt.geom.Point2D.Double getPosition()
Gets the x and y coordinates of the current postion of this DrawingTool.

Returns:
x and y coordinates of the current postion of this DrawingTool as a Point2D.Double.

getDirection

public double getDirection()
Gets the direction of this DrawingTool.

Returns:
direction of this DrawingTool in degrees

getPadPanel

public SketchPadPanel getPadPanel()
Returns the SketchPadPanel object for this DrawingTool.

Returns:
Returns the SketchPadPanel property

down

public void down()
This object is set to drawing mode.


home

public void home()
The location of the DrawingTool object is set to the center of the SketchPad window with the drawing direction of the object set to up.


move

public void move(double distance)
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.

Parameters:
distance - pixels to move from the current location

move

public void move(double x,
                 double y)
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. The direction is changed to the heading given by the move from the previous postion to the current position.

Parameters:
x - x coordinate of destination
y - y coordinate of destination

forward

public void forward(double distance)
This DrawingTool object is moved forward from 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. The direction is unchanged. A delay occurs following this method’s execution.

Parameters:
distance - pixels to move forwards from the current location

backward

public void backward(double distance)
This DrawingTool object is moved backward from 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. The direction is unchanged. A delay occurs following this method’s execution.

Parameters:
distance - pixels to move backwards from the current location

setColor

public void setColor(java.awt.Color c)
The color of the DrawingTool object is set to c.

Parameters:
c - a valid Color object

setWidth

public void setWidth(int width)
Sets the width of the DrawingTool object is to width pixels.

Parameters:
width - width in pixels of the DrawingTool object
Throws:
java.lang.RuntimeException - if width is < 1

setDirection

public void setDirection(double deg)
Sets the direction to d degrees. The orientation is deg degrees counterclockwise from the positive x-axis

Parameters:
deg - degrees counterclockwise from the positive x-axis

toString

public java.lang.String toString()
Returns a string representation of this object. The format is: "DrawingTool["+ "Position:" + xPos + "," + yPos + " " + "Color:" + color + " " + "Width:" + width + " " + "Is down:" + isDown + " " + "Direction:" + direction + ']'.

Overrides:
toString in class java.lang.Object
Returns:
a string representation of this object

turn

public void turn(double degrees)
Changes the current direction counterclockwise by degrees degrees from the current direction

Parameters:
degrees - degrees counterclockwise from the current direction

turnLeft

public void turnLeft(double degrees)
Changes the current direction counterclockwise by degrees degrees from the current direction

Parameters:
degrees - degrees counterclockwise from the current direction

turnLeft

public void turnLeft()
Changes the current direction counterclockwise by 90 degrees from the current direction

Parameters:
degrees - degrees counterclockwise from the current direction

turnRight

public void turnRight(double degrees)
Changes the current direction clockwise by degrees degrees from the current direction

Parameters:
degrees - degrees clockwise from the current direction

turnRight

public void turnRight()
Changes the current direction clockwise by 90 degrees from the current direction

Parameters:
degrees - degrees clockwise from the current direction

up

public void up()
This object is set to moving mode.


drawString

public void drawString(java.lang.String text)
Draw the string text at the current location using the current color.

Parameters:
text - string to be drawn

drawCircle

public void drawCircle(double radius)
If the object is in drawing mode, Draws a circle of radius radius around the current location using the current width and color.

Parameters:
radius - radius of circle to be drawn

fillCircle

public void fillCircle(double radius)
If the object is in drawing mode, Draws and fills a circle of radius radius around the current location using the current pen width and color.

Parameters:
radius - radius of the filled circle to be drawn

drawRect

public void drawRect(double recWidth,
                     double recHeight)
Draws the outline of the specified rectangle. The rectangle is centered at the current drawing position. The left and right edges of the rectangle are at x - recWidth/2 and x + recWidth/2. The top and bottom edges are at y - recHeight/2 and y + recHeight/2. The rectangle is drawn using the DrawingTool's current color.

Parameters:
recWidth - width of the rectangle to be drawn
recHeight - height of the rectangle to be drawn

fillRect

public void fillRect(double recWidth,
                     double recHeight)
Fills the specified rectangle. The rectangle is centered at the current drawing position. The left and right edges of the rectangle are at x - recWidth/2 and x + recWidth/2. The top and bottom edges are at y - recHeight/2 and y + recHeight/2. The rectangle is filled using the DrawingTool's current color.

Parameters:
recWidth - width of the rectangle to be filled
recHeight - height of the rectangle to be filled

drawOval

public void drawOval(double boundingBoxWidth,
                     double boundingBoxHeight)
Draws the outline of an oval. The result is a circle or ellipse that is centered around the current drawing position and fits in a rectangle specified by the width, and height arguments. The left and right edges of the bounding rectangle are at x - recWidth/2 and x + recWidth/2. The top and bottom edges are at y - recHeight/2 and y + recHeight/2. The oval is drawn using the DrawingTool's current color.

Parameters:
boundingBoxWidth - width of the oval to be drawn
boundingBoxHeight - height of the oval to be drawn

fillOval

public void fillOval(double boundingBoxWidth,
                     double boundingBoxHeight)
Fills the specified oval. The result is a filled circle or ellipse that is centered around the current drawing position and fits in a rectangle specified by the width, and height arguments. The left and right edges of the bounding rectangle are at x - recWidth/2 and x + recWidth/2. The top and bottom edges are at y - recHeight/2 and y + recHeight/2. The oval is drawn using the DrawingTool's current color.

Parameters:
boundingBoxWidth - width of the oval to be filled
boundingBoxHeight - height of the oval to be filled

drawLine

protected void drawLine(double x,
                        double y)

turn

protected void turn(double x,
                    double y)