gpdraw
Class DrawingTool

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by java.awt.Window
              extended by java.awt.Frame
                  extended by javax.swing.JFrame
                      extended by gpdraw.DrawingTool
All Implemented Interfaces:
SketchPadListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.accessibility.Accessible, javax.swing.RootPaneContainer, javax.swing.WindowConstants

public class DrawingTool
extends javax.swing.JFrame
implements SketchPadListener

DrawingTool is 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(75); pencil.forward(75);

Since:
JDK1.4
Version:
1.11, 08/12/06
Author:
George Peck
See Also:
Serialized Form

Field Summary
 
Fields inherited from class javax.swing.JFrame
EXIT_ON_CLOSE
 
Fields inherited from class java.awt.Frame
CROSSHAIR_CURSOR, DEFAULT_CURSOR, E_RESIZE_CURSOR, HAND_CURSOR, ICONIFIED, MAXIMIZED_BOTH, MAXIMIZED_HORIZ, MAXIMIZED_VERT, MOVE_CURSOR, N_RESIZE_CURSOR, NE_RESIZE_CURSOR, NORMAL, NW_RESIZE_CURSOR, S_RESIZE_CURSOR, SE_RESIZE_CURSOR, SW_RESIZE_CURSOR, TEXT_CURSOR, W_RESIZE_CURSOR, WAIT_CURSOR
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface javax.swing.WindowConstants
DISPOSE_ON_CLOSE, DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
DrawingTool()
          Constructor.
DrawingTool(int width, int height)
          Constructor.
DrawingTool(int width, int height, int delay)
          Constructor.
DrawingTool(SketchPad sPad)
          Constructor.
DrawingTool(SketchPad sPad, int delay)
           
 
Method Summary
 void backward(double distance)
          This DrawingTool object is moved backward from current direction by distance pixels from the old (previous) location.
 void clear()
          Clears the background to the current color
 void clear(java.awt.Color bg)
          Clears the background to specified Color
 void clear(int colorCode)
          Clears the background to the color specified by the given color code
 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.
 void drawImage(java.lang.String fileName)
          Loads and draws the image specified by the file name fileName.
 void drawImage(java.lang.String fileName, double w, double h)
          Loads and draws the image specified by the file name fileName.
 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 and font.
 void fillCircle(double radius)
          Fills the specified circle.
 void fillOff()
          Sets the drawing mode so that only the outline of all circle, oval and rectangle shapes are drawn..
 void fillOn()
          Sets the drawing mode so that all circle, oval and rectangle shapes are filled with the current color when drawn.
 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 getBackgroundColor()
          Gets the background color of the DrawingTool window
 java.awt.Color getColor()
          Gets the color of the DrawingTool
 double getDirection()
          Gets the direction of this DrawingTool .
 int getHeight()
          Get the height of the DrawingTool SketchPad window
 int getPenWidth()
          Get the drawing width of the DrawingTool "pen"
 java.awt.geom.Point2D.Double getPosition()
          Gets the x and y coordinates of the current postion of this DrawingTool.
 int getWidth()
          Get the width of the DrawingTool SketchPad window
 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.
 boolean isDown()
          Get the drawing width of the DrawingTool
 void keyPressed(int keyCode)
          Invoked when a key has been pressed
 void keyTyped(char ch)
          Invoked when a key has been typed
 void mouseClicked(double x, double y)
          Invoked when the mouse button has been clicked (pressed and released).
 void mouseDragged(double x, double y)
          Invoked when a mouse button is pressed on a component and then dragged.
 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 move(int x, int y)
           
 void pause(int delay)
          Pauses for delay milliseconds.
 void pixel(int x, int y)
          Plots a single pixel at the point (x, y) using the DrawingTool's current color.
 void play(java.lang.String fileName)
          Plays a wav or midi sound.
 void print()
          Prints the DrawingTool SketchPad panel area confirming from user with a print dialog
 void render()
          Repaint the DrawingTool SketchPad panel.
 void save(java.lang.String fileName)
          Write the DrawingTool SketchPad panel to a file.
 void setColor(java.awt.Color c)
          The color of the DrawingTool object is set to c.
 void setColor(int colorCode)
          The color of the DrawingTool object is set using the colorCode as follows:
 void setColorHSB(int h, int s, int b)
          The color of the DrawingTool object is set using hue-saturation-brightness.
 void setColorRGB(int r, int g, int b)
          The color of the DrawingTool object is set using red-green-blue.
 void setDirection(double d)
          Sets the direction to d degrees.
 void setFont(java.awt.Font font)
           
 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 translateOff()
          Sets the origin of the DrawingTool SketchPad to the lower left-hand corner of the window.
 void translateOn()
          Sets the origin of the DrawingTool SketchPad to the center of the window.
 void turn(double angle)
          Changes the current direction counterclockwise by degrees degrees from the current direction.
 void turnLeft(double degrees)
          Changes the current direction counterclockwise by degrees 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 javax.swing.JFrame
getAccessibleContext, getContentPane, getDefaultCloseOperation, getGlassPane, getJMenuBar, getLayeredPane, getRootPane, isDefaultLookAndFeelDecorated, remove, setContentPane, setDefaultCloseOperation, setDefaultLookAndFeelDecorated, setGlassPane, setIconImage, setJMenuBar, setLayeredPane, setLayout, update
 
Methods inherited from class java.awt.Frame
addNotify, getCursorType, getExtendedState, getFrames, getIconImage, getMaximizedBounds, getMenuBar, getState, getTitle, isResizable, isUndecorated, remove, removeNotify, setCursor, setExtendedState, setMaximizedBounds, setMenuBar, setResizable, setState, setTitle, setUndecorated
 
Methods inherited from class java.awt.Window
addPropertyChangeListener, addPropertyChangeListener, addWindowFocusListener, addWindowListener, addWindowStateListener, applyResourceBundle, applyResourceBundle, createBufferStrategy, createBufferStrategy, dispose, getBufferStrategy, getFocusableWindowState, getFocusCycleRootAncestor, getFocusOwner, getFocusTraversalKeys, getGraphicsConfiguration, getInputContext, getListeners, getLocale, getMostRecentFocusOwner, getOwnedWindows, getOwner, getToolkit, getWarningString, getWindowFocusListeners, getWindowListeners, getWindowStateListeners, hide, isActive, isAlwaysOnTop, isFocusableWindow, isFocusCycleRoot, isFocused, isLocationByPlatform, isShowing, pack, postEvent, removeWindowFocusListener, removeWindowListener, removeWindowStateListener, setAlwaysOnTop, setBounds, setCursor, setFocusableWindowState, setFocusCycleRoot, setLocationByPlatform, setLocationRelativeTo, show, toBack, toFront
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, preferredSize, print, printComponents, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, transferFocusBackward, transferFocusDownCycle, validate
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, dispatchEvent, enable, enable, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getTreeLock, getX, getY, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, nextFocus, paintAll, prepareImage, prepareImage, printAll, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setComponentOrientation, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, size, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.awt.MenuContainer
getFont, postEvent
 

Constructor Detail

DrawingTool

public DrawingTool(SketchPad sPad,
                   int delay)

DrawingTool

public DrawingTool(SketchPad sPad)
Constructor. A new DrawingTool is created and placed in the center (0, 0) of the SketchPad. This object is set to drawing mode. The direction for this object is to the right (0 degrees). The DrawingTool color is set to black. The DrawingTool width is 1.


DrawingTool

public DrawingTool(int width,
                   int height,
                   int delay)
Constructor. A new DrawingTool window 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
delay - drawing delay (ms)

DrawingTool

public DrawingTool(int width,
                   int height)
Constructor. A new DrawingTool window that is width pixels wide by height pixels high with no drawing delay.

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

DrawingTool

public DrawingTool()
Constructor. A new DrawingTool is created and placed in the center (0, 0) of a SketchPad window that is 500 pixels wide and 500 pixels high. This object is set to drawing mode. The direction for this object is facing to the right (0). The DrawingTool color is set to black. The DrawingTool width is 1.

Method Detail

getWidth

public int getWidth()
Get the width of the DrawingTool SketchPad window

Overrides:
getWidth in class java.awt.Component
Returns:
current width of the DrawingTool SketchPad window

getHeight

public int getHeight()
Get the height of the DrawingTool SketchPad window

Overrides:
getHeight in class java.awt.Component
Returns:
current height of the DrawingTool SketchPad window

getPenWidth

public int getPenWidth()
Get the drawing width of the DrawingTool "pen"

Returns:
current width of the DrawingTool "pen"

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

isDown

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

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

getColor

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

Returns:
current color of this DrawingTool

getBackgroundColor

public java.awt.Color getBackgroundColor()
Gets the background color of the DrawingTool window

Returns:
current background color of DrawingTool window

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.


down

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


up

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


translateOn

public void translateOn()
Sets the origin of the DrawingTool SketchPad to the center of the window.


translateOff

public void translateOff()
Sets the origin of the DrawingTool SketchPad to the lower left-hand corner of the window.


fillOn

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


fillOff

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


setDirection

public 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 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

clear

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


clear

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

Parameters:
bg - Color of the resulting cleared background

clear

public 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 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 void setColor(java.awt.Color c)
The color of the DrawingTool object is set to c.

Parameters:
c - a valid Color object

setColorRGB

public void setColorRGB(int r,
                        int g,
                        int b)
The color of the DrawingTool object is set using red-green-blue.

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

setColorHSB

public void setColorHSB(int h,
                        int s,
                        int b)
The color of the DrawingTool object 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 void setColor(int colorCode)
The color of the DrawingTool object 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 void setFont(java.awt.Font font)
Overrides:
setFont in class java.awt.Container

move

public void move(int x,
                 int y)
Overrides:
move in class java.awt.Component

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

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

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 methods 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 methods execution.

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

pixel

public void pixel(int x,
                  int y)
Plots a single pixel at the point (x, y) using the DrawingTool's current color.

Parameters:
x - x coordinate of the pixel to be drawn
y - y coordinate of the pixel 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

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

fillCircle

public void fillCircle(double radius)
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.

Parameters:
radius - radius of circle 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

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

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

drawImage

public 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 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 void turn(double angle)
Changes the current direction counterclockwise by degrees degrees from the current direction.

Parameters:
angle - 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

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

play

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

Parameters:
fileName - name of the sound file

drawString

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

Parameters:
text - string to be drawn

render

public void render()
Repaint the DrawingTool SketchPad panel.


save

public void save(java.lang.String fileName)
Write the DrawingTool SketchPad panel 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 void print()
Prints the DrawingTool SketchPad panel area confirming from user with a print dialog


toString

public java.lang.String toString()
Returns a string representation of this object.

Overrides:
toString in class java.awt.Component
Returns:
a string representation of this object

mouseClicked

public void mouseClicked(double x,
                         double y)
Description copied from interface: SketchPadListener
Invoked when the mouse button has been clicked (pressed and released).

Specified by:
mouseClicked in interface SketchPadListener

mouseDragged

public void mouseDragged(double x,
                         double y)
Description copied from interface: SketchPadListener
Invoked when a mouse button is pressed on a component and then dragged. mouseDragged events will continue to be delivered to the component where the drag originated until the mouse button is released

Specified by:
mouseDragged in interface SketchPadListener

keyPressed

public void keyPressed(int keyCode)
Description copied from interface: SketchPadListener
Invoked when a key has been pressed

Specified by:
keyPressed in interface SketchPadListener

keyTyped

public void keyTyped(char ch)
Description copied from interface: SketchPadListener
Invoked when a key has been typed

Specified by:
keyTyped in interface SketchPadListener