gpdraw
Class Pen

java.lang.Object
  extended by gpdraw.Pen

public class Pen
extends java.lang.Object

Pen is graphics environment that provides methods for a programmer to draw pictures by moving an object (called a Pen) 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: ======= // draw a square Pen.forward(75); Pen.turnLeft(90); Pen.forward(75); Pen.turnLeft(90); Pen.forward(75); Pen.turnLeft(75); Pen.forward(75);

Since:
JDK1.4
Version:
1.11, 08/12/06
Author:
George Peck

Method Summary
static void backward(double distance)
          This drawing position is moved backward from current location by distance pixels from the old (previous) location.
static void clear()
          Clears the background to the current color
static void clear(java.awt.Color bg)
          Clears the background to specified Color
static void clear(int colorCode)
          Clears the background to the color specified by the given color code
static void create(int width, int height)
          Create a canvas with drawing area that is widthheight pixels high.
static void create(int width, int height, int dly)
          Create a canvas with drawing area that is widthheight pixels high with a drawing delay of delay ms.
static void destroy()
           
static void down()
          Sets drawing mode to true.
static void drawCircle(double r)
          If the object is in drawing mode, Draws a circle of radius radius around the current location using the current width and color.
static void drawImage(java.lang.String fileName)
          Loads and draws the image specified by the file name fileName.
static void drawImage(java.lang.String fileName, double w, double h)
          Loads and draws the image specified by the file name fileName.
static void drawOval(double w, double h)
          Draws the outline of an oval.
static void drawRect(double w, double h)
          Draws the outline of the specified rectangle.
static void drawString(java.lang.String s)
          Draw the string text at the current location using the current color and font.
static void fillCircle(double r)
          Fills the specified circle.
static void fillOff()
          Sets the drawing mode so that only the outline of all circle, oval and rectangle shapes are drawn..
static void fillOn()
          Sets the drawing mode so that all circle, oval and rectangle shapes are filled with the current color when drawn.
static void fillOval(double w, double h)
          Fills the specified oval.
static void fillRect(double w, double h)
          Fills the specified rectangle.
static void forward(double distance)
          This drawing position is moved forward from current location by distance pixels from the old (previous) location.
static java.awt.Color getBackgroundColor()
          Gets the background color of the drawing area
static java.awt.Color getColor()
          Gets the color of the Pen
static double getDirection()
          Gets the current drawing orientation.
 boolean getFill()
          Get the state of the fill flag
static int getHeight()
          Get the height of the drawing area
 int getPenWidth()
          Get the drawing width of the Pen
static java.awt.geom.Point2D.Double getPosition()
          Gets the x and y coordinates of the current postion of the current drawing position.
 boolean getTranslate()
          Get the state of the translation flag
static int getWidth()
          Get the width of the drawing area
static double getXPos()
          Gets the x coordinate of the current Pen postion
static double getYPos()
          Gets the y coordinate of the current Pen postion
static void home()
          The drawing location is set to the center of the drawing area with the drawing direction set to the right.
static boolean isDown()
          Get the drawing state of the Pen
static void move(double d)
          This drawing position is moved in the current direction by distance pixels from the old (previous) location.
static void move(double x, double y)
          This drawing position is moved from the current position to the position specified by the coordinates x and y.
static void move(int x, int y)
           
static void pause(int delay)
          Pauses for delay milliseconds.
static void pixel(int x, int y)
          Plots a single pixel at the point (x, y) using the current drawing color.
static void play(java.lang.String fileName)
          Plays a wav or midi sound.
static void print()
          Prints the SketchPad panel area confirming from user with a print dialog
static void render()
          Repaint the drawing area.
static void save(java.lang.String fileName)
          Write the display area to a file.
static void setColor(java.awt.Color c)
          The foreground drawing color is set to c.
static void setColor(int colorCode)
          The color of the Pen is set using the colorCode as follows:
static void setColorHSB(int h, int s, int b)
          The foreground drawing color is set using hue-saturation-brightness.
static void setColorRGB(int r, int g, int b)
          The foreground drawing color is set using red-green-blue.
static void setDirection(double d)
          Sets the direction to d degrees.
static void setFont(java.awt.Font font)
           
static void setWidth(int width)
          Sets the drawing width to width pixels.
static void translateOff()
          Sets the origin of the drawing area to the lower left-hand corner of the window.
static void translateOn()
          Sets the origin of the drawing area to the center of the window.
static void turn(double angle)
          Changes the current direction counterclockwise by angle degrees from the current direction
static void turnLeft(double degrees)
          Changes the current direction counterclockwise by degrees degrees from the current direction
static void turnRight(double degrees)
          Changes the current direction clockwise by degrees degrees from the current direction
static void up()
          Sets drawing mode to false (movement mode).
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

create

public static void create(int width,
                          int height,
                          int dly)
Create a canvas with drawing area that is widthheight pixels high with a drawing delay of delay ms.

Parameters:
width - width of this DrawingTool SketchPad
height - height of this DrawingTool SketchPad
dly - drawing delay (ms)

create

public static void create(int width,
                          int height)
Create a canvas with drawing area that is widthheight pixels high.

Parameters:
width - width of this DrawingTool SketchPad
height - height of this DrawingTool SketchPad

destroy

public static void destroy()

getWidth

public static int getWidth()
Get the width of the drawing area

Returns:
current width of the drawing area

getHeight

public static int getHeight()
Get the height of the drawing area

Returns:
current height of the drawing area

getXPos

public static double getXPos()
Gets the x coordinate of the current Pen postion

Returns:
x coordinate of the current Pen postion

getYPos

public static double getYPos()
Gets the y coordinate of the current Pen postion

Returns:
y coordinate of the current Pen postion

getDirection

public static double getDirection()
Gets the current drawing orientation.

Returns:
current drawing orientation in degrees

isDown

public static boolean isDown()
Get the drawing state of the Pen

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

getColor

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

Returns:
current Pen color

getBackgroundColor

public static java.awt.Color getBackgroundColor()
Gets the background color of the drawing area

Returns:
current background color of the drawing area

getPosition

public static java.awt.geom.Point2D.Double getPosition()
Gets the x and y coordinates of the current postion of the current drawing position.

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

getPenWidth

public int getPenWidth()
Get the drawing width of the Pen

Returns:
current width of the Pen

getFill

public boolean getFill()
Get the state of the fill flag

Returns:
true if in fill mode, false otherwise.

getTranslate

public boolean getTranslate()
Get the state of the translation flag

Returns:
true if in translation mode, false otherwise.

home

public static void home()
The drawing location is set to the center of the drawing area with the drawing direction set to the right.


down

public static void down()
Sets drawing mode to true.


up

public static void up()
Sets drawing mode to false (movement mode).


translateOn

public static void translateOn()
Sets the origin of the drawing area to the center of the window. This is the default mode.


translateOff

public static void translateOff()
Sets the origin of the drawing area to the lower left-hand corner of the window.


fillOn

public static void fillOn()
Sets the drawing mode so that all circle, oval and rectangle shapes are filled with the current color when drawn.


fillOff

public static void fillOff()
Sets the drawing mode so that only the outline of all circle, oval and rectangle shapes are drawn..


setDirection

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

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

setWidth

public static void setWidth(int width)
Sets the drawing width to width pixels.

Parameters:
width - drawing width in pixels
Throws:
java.lang.RuntimeException - if width is < 1

clear

public static void clear()
Clears the background to the current color


clear

public static void clear(java.awt.Color bg)
Clears the background to specified Color

Parameters:
bg - Color of the resulting cleared background

clear

public static void clear(int colorCode)
Clears the background to the color specified by the given color code

Parameters:
colorCode - code used to specify the color of the resulting cleared background
See Also:
setColor(int colorCode)

pause

public static void pause(int delay)
Pauses for delay milliseconds. Also invokes a repaint of the display area.

Parameters:
delay - number of ms to delay

setColor

public static void setColor(java.awt.Color c)
The foreground drawing color is set to c.

Parameters:
c - a valid Color object

setColorRGB

public static void setColorRGB(int r,
                               int g,
                               int b)
The foreground drawing color is set using red-green-blue.

Parameters:
r - the red component
g - the green component
b - the blue component

setColorHSB

public static void setColorHSB(int h,
                               int s,
                               int b)
The foreground drawing color is set using hue-saturation-brightness.

Parameters:
h - the hue component
s - the saturation of the color
b - the brightness of the color

setColor

public static void setColor(int colorCode)
The color of the Pen is set using the colorCode as follows:

Number
Color
Number
Color
Number
Color
Number
Color
0
black
4
red
8
brown
12
salmon
1
blue
5
magenta
9
tan
13
violet
2
green
6
yellow
10
forest
14
orange
3
cyan
7
white
11
aqua
15
grey

Parameters:
colorCode - a valid colorCode

setFont

public static void setFont(java.awt.Font font)

move

public static void move(int x,
                        int y)

move

public static void move(double x,
                        double y)
This drawing position 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

move

public static void move(double d)
This drawing position 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:
d - pixels to move from the current location

forward

public static void forward(double distance)
This drawing position is moved forward from current location 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 methods execution.

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

backward

public static void backward(double distance)
This drawing position is moved backward from current location 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 methods execution.

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

pixel

public static void pixel(int x,
                         int y)
Plots a single pixel at the point (x, y) using the current drawing color.

Parameters:
x - x coordinate of the pixel to be drawn
y - y coordinate of the pixel to be drawn

fillRect

public static void fillRect(double w,
                            double h)
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.Degenerates to single pixel if the rectangle is too small.

Parameters:
w - width of the rectangle to be filled
h - height of the rectangle to be filled

drawRect

public static void drawRect(double w,
                            double h)
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. Degenerates to single pixel if the rectangle is too small.

Parameters:
w - width of the rectangle to be drawn
h - height of the rectangle to be drawn

fillCircle

public static void fillCircle(double r)
Fills the specified circle. The result is a filled circle of radius radiuscentered around the current drawing position The circle is drawn using the DrawingTool's current color. Degenerates to single pixel if the circle is too small.

Parameters:
r - radius of circle to be drawn

drawCircle

public static void drawCircle(double r)
If the object is in drawing mode, Draws a circle of radius radius around the current location using the current width and color. Degenerates to single pixel if the circle is too small.

Parameters:
r - radius of circle to be drawn

fillOval

public static void fillOval(double w,
                            double h)
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. Degenerates to single pixel if the oval is too small.

Parameters:
w - width of the oval to be filled
h - height of the oval to be filled

drawOval

public static void drawOval(double w,
                            double h)
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. Degenerates to single pixel if the oval is too small.

Parameters:
w - width of the oval to be drawn
h - height of the oval to be drawn

drawImage

public static void drawImage(java.lang.String fileName)
Loads and draws the image specified by the file name fileName. The image is centered at the current drawing location.

Parameters:
fileName - file name of image to load and draw

drawImage

public static void drawImage(java.lang.String fileName,
                             double w,
                             double h)
Loads and draws the image specified by the file name fileName. The image is centered at the current drawing location and scaled of size w-by-h.

Parameters:
fileName - file name of image to load and draw
w - width of bounding box in which the image is drawn
h - height of bounding box in which the image is drawn

turn

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

Parameters:
angle - degrees counterclockwise from the current direction

turnLeft

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

Parameters:
degrees - degrees counterclockwise from the current direction

turnRight

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

Parameters:
degrees - degrees clockwise from the current direction

play

public static void play(java.lang.String fileName)
Plays a wav or midi sound.

Parameters:
fileName - name of the sound file

drawString

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

Parameters:
s - string to be drawn

render

public static void render()
Repaint the drawing area.


save

public static void save(java.lang.String fileName)
Write the display area to a file. The suffix of the file name can be png, jpg, or gif and will determine the format of the graphics file that is written.

Parameters:
fileName - Name of file to be written

print

public static void print()
Prints the SketchPad panel area confirming from user with a print dialog