|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JPanel
gpdraw.SketchPad
public class SketchPad
A SketchPad is graphics componet that provides methods for a programmer to draw pictures by moving a drawing object (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.
Nested Class Summary |
---|
Nested classes/interfaces inherited from class javax.swing.JComponent |
---|
javax.swing.JComponent.AccessibleJComponent |
Field Summary |
---|
Fields inherited from class javax.swing.JComponent |
---|
TOOL_TIP_TEXT_KEY, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW |
Fields inherited from class java.awt.Component |
---|
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT |
Fields inherited from interface java.awt.print.Printable |
---|
NO_SUCH_PAGE, PAGE_EXISTS |
Fields inherited from interface java.awt.image.ImageObserver |
---|
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH |
Constructor Summary | |
---|---|
SketchPad(int width,
int height)
Constructor. |
Method Summary | |
---|---|
void |
addNotify()
Called after native screen peer object exists |
void |
addSketchPadListener(SketchPadListener listener)
Adds the specified SketchPad listener to receive mouse events, mouse motion events, and key events from this component. |
void |
backward(double distance)
This drawing position is moved backward from current location 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 |
java.awt.Color |
codeToColor(int colorCode)
Returns the Color indicated by the color code
colorCode . |
void |
down()
Sets drawing mode to true. |
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. |
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 w,
double h)
Draws the outline of an oval. |
void |
drawRect(double w,
double h)
Draws the outline of the specified rectangle. |
void |
drawString(java.lang.String s)
Draw the string text at the current location using the
current color and font. |
void |
fillCircle(double r)
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 w,
double h)
Fills the specified oval. |
void |
fillRect(double w,
double h)
Fills the specified rectangle. |
void |
forward(double distance)
This drawing position is moved forward from current location by distance pixels from the old (previous) location. |
java.awt.Color |
getBackgroundColor()
Gets the background color of the SketchPad panel |
java.awt.Color |
getColor()
Gets the color of the SketchPad foreground color |
double |
getDirection()
Gets the current drawing orientation. |
boolean |
getFill()
Get the state of the fill flag |
int |
getHeight()
Get the height of the SketchPad panel |
SketchPad |
getPadPanel()
Returns this SketchPadPanel object. |
int |
getPenWidth()
Get the drawing width of the "pen" |
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 |
int |
getWidth()
Get the width of the SketchPad panel |
double |
getXPos()
Gets the x coordinate of the current drawing postion |
double |
getYPos()
Gets the y coordinate of the current drawing postion |
void |
home()
The drawing location is set to the center of the SketchPad panel with the drawing direction set in the positive x-direction. |
boolean |
isDown()
Get the drawing state of the SketchPad panel |
void |
keyPressed(java.awt.event.KeyEvent e)
|
void |
keyReleased(java.awt.event.KeyEvent e)
|
void |
keyTyped(java.awt.event.KeyEvent e)
|
void |
mouseClicked(java.awt.event.MouseEvent e)
|
void |
mouseDragged(java.awt.event.MouseEvent e)
|
void |
mouseEntered(java.awt.event.MouseEvent e)
|
void |
mouseExited(java.awt.event.MouseEvent e)
|
void |
mouseMoved(java.awt.event.MouseEvent e)
|
void |
mousePressed(java.awt.event.MouseEvent e)
|
void |
mouseReleased(java.awt.event.MouseEvent e)
|
void |
move(double d)
This drawing position is moved in the current direction by distance pixels from the old (previous) location. |
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. |
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 current drawing color. |
void |
play(java.lang.String fileName)
Plays a wav or midi sound. |
void |
print()
Prints the SketchPad panel area confirming from
user with a print dialog |
int |
print(java.awt.Graphics g,
java.awt.print.PageFormat pf,
int pi)
|
void |
rotate(double angle)
Changes the current direction counterclockwise by angle
degrees from the current direction |
void |
save(java.lang.String fileName)
Write the SketchPad panel to a file. |
void |
setColor(java.awt.Color c)
The foreground drawing color is set to c . |
void |
setColor(int colorCode)
The foreground drawing color is set to the Color indicated by the colorCode . |
void |
setColorHSB(int h,
int s,
int b)
The foreground drawing color is set using hue-saturation-brightness. |
void |
setColorRGB(int r,
int g,
int b)
The foreground drawing color is set using red-green-blue. |
void |
setDirection(double d)
Sets the direction to d degrees. |
void |
setWidth(int width)
Sets the drawing width to width pixels. |
java.lang.String |
toString()
Returns a string representation of this object. |
void |
translateOff()
Sets the origin of the SketchPad panel to the lower
left-hand corner of the window. |
void |
translateOn()
Sets the origin of the SketchPad panel to the center
of the window. |
void |
turn(double angle)
Changes the current direction counterclockwise by angle
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()
Sets drawing mode to false (movement mode). |
Methods inherited from class javax.swing.JPanel |
---|
getAccessibleContext, getUI, getUIClassID, setUI, updateUI |
Methods inherited from class javax.swing.JComponent |
---|
addAncestorListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintImmediately, paintImmediately, print, printAll, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update |
Methods inherited from class java.awt.Container |
---|
add, add, add, add, add, addContainerListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, 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, createImage, createImage, createVolatileImage, createVolatileImage, dispatchEvent, enable, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, nextFocus, paintAll, postEvent, prepareImage, prepareImage, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, transferFocus, transferFocusUpCycle |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public SketchPad(int width, int height)
SketchPad
graphics panel of
given dimensions
width
- width of this SketchPad
height
- height of this SketchPad
Method Detail |
---|
public void addNotify()
addNotify
in class javax.swing.JComponent
public double getXPos()
x
coordinate of the current drawing postion
x
coordinate of the current drawing postionpublic double getYPos()
y
coordinate of the current drawing postion
y
coordinate of the current drawing postionpublic double getDirection()
public boolean isDown()
SketchPad
panel
true
if in drawing mode (pen is down);
false
otherwisepublic java.awt.Color getColor()
SketchPad
foreground color
SketchPad
foreground colorpublic java.awt.Color getBackgroundColor()
SketchPad
panel
SketchPad
panelpublic java.awt.geom.Point2D.Double getPosition()
x
and y
coordinates of the current
postion of the current drawing position.
x
and y
coordinates of the current
postion as a Point2D.Double
.public int getPenWidth()
public int getWidth()
SketchPad
panel
getWidth
in class javax.swing.JComponent
DrawingTool SketchPad
panelpublic int getHeight()
SketchPad
panel
getHeight
in class javax.swing.JComponent
SketchPad
panelpublic boolean getFill()
true
if in fill mode, false
otherwise.public boolean getTranslate()
true
if in translation mode, false
otherwise.public void home()
public void down()
public void up()
public void translateOn()
SketchPad
panel to the center
of the window. This is the default mode.
public void translateOff()
SketchPad
panel to the lower
left-hand corner of the window.
public void fillOn()
public void fillOff()
public void setDirection(double d)
d
degrees counterclockwise from the positive x-axis
d
- degrees counterclockwise from the positive x-axispublic void setWidth(int width)
width
pixels.
width
- drawing width in pixels
java.lang.RuntimeException
- if width is < 1public void clear()
public void clear(java.awt.Color bg)
Color
bg
- Color
of the resulting cleared backgroundpublic void clear(int colorCode)
colorCode
- code used to specify the color of the resulting cleared
backgroundcodeToColor(int colorCode)
public void setColor(java.awt.Color c)
c
.
c
- a valid Color
objectpublic void setColorRGB(int r, int g, int b)
r
- the red componentg
- the green componentb
- the blue componentpublic void setColorHSB(int h, int s, int b)
h
- the hue components
- the saturation of the colorb
- the brightness of the colorpublic java.awt.Color codeToColor(int colorCode)
Color
indicated by the color code
colorCode
.
colorCode
- a valid colorCode
Color
corresponding to the colorCode
public void setColor(int colorCode)
colorCode
.
colorCode
- a valid colorCode
codeToColor(int colorCode)
public void move(int x, int y)
move
in class java.awt.Component
public void move(double x, double y)
x
- x coordinate of destinationy
- y coordinate of destinationpublic void move(double d)
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.
d
- pixels to move from the current locationpublic void forward(double distance)
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.
distance
- pixels to move forwards from the current locationpublic void backward(double distance)
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.
distance
- pixels to move backwards from the current locationpublic void pixel(int x, int y)
x
- x coordinate of the pixel to be drawny
- y coordinate of the pixel to be drawnpublic void fillRect(double w, double h)
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.
w
- width of the rectangle to be filledh
- height of the rectangle to be filledpublic void drawRect(double w, double h)
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.
w
- width of the rectangle to be drawnh
- height of the rectangle to be drawnpublic void fillCircle(double r)
radius
centered 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.
r
- radius of circle to be drawnpublic void drawCircle(double r)
radius
around the current location using the current width and color. Degenerates
to single pixel if the circle is too small.
r
- radius of circle to be drawnpublic void fillOval(double w, double h)
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.
w
- width of the oval to be filledh
- height of the oval to be filledpublic void drawOval(double w, double h)
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.
w
- width of the oval to be drawnh
- height of the oval to be drawnpublic void drawImage(java.lang.String fileName)
fileName
.
The image is centered at the current drawing location.
fileName
- file name of image to load and drawpublic void drawImage(java.lang.String fileName, double w, double h)
fileName
.
The image is centered at the current drawing location and scaled of size w-by-h.
fileName
- file name of image to load and draww
- width of bounding box in which the image is drawnh
- height of bounding box in which the image is drawnpublic void rotate(double angle)
angle
degrees from the current direction
angle
- degrees counterclockwise from the current directionpublic void turn(double angle)
angle
degrees from the current direction
angle
- degrees counterclockwise from the current directionpublic void turnLeft(double degrees)
degrees
degrees from the current direction
degrees
- degrees counterclockwise from the current directionpublic void turnRight(double degrees)
degrees
degrees from the current direction
degrees
- degrees clockwise from the current directionpublic java.lang.String toString()
toString
in class java.awt.Component
public void drawString(java.lang.String s)
text
at the current location using the
current color and font.
s
- string to be drawnpublic void pause(int delay)
delay
milliseconds. Also invokes a repaint
of the display area.
delay
- number of ms to delaypublic SketchPad getPadPanel()
SketchPadPanel
object.
SketchPadPanel
propertypublic void play(java.lang.String fileName)
fileName
- name of the sound filepublic void save(java.lang.String fileName)
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.
fileName
- Name of file to be writtenpublic int print(java.awt.Graphics g, java.awt.print.PageFormat pf, int pi) throws java.awt.print.PrinterException
print
in interface java.awt.print.Printable
java.awt.print.PrinterException
public void print()
SketchPad
panel area confirming from
user with a print dialog
public void addSketchPadListener(SketchPadListener listener)
listener
- the SketchPad listenerpublic void keyPressed(java.awt.event.KeyEvent e)
keyPressed
in interface java.awt.event.KeyListener
public void keyReleased(java.awt.event.KeyEvent e)
keyReleased
in interface java.awt.event.KeyListener
public void keyTyped(java.awt.event.KeyEvent e)
keyTyped
in interface java.awt.event.KeyListener
public void mouseClicked(java.awt.event.MouseEvent e)
mouseClicked
in interface java.awt.event.MouseListener
public void mouseDragged(java.awt.event.MouseEvent e)
mouseDragged
in interface java.awt.event.MouseMotionListener
public void mouseMoved(java.awt.event.MouseEvent e)
mouseMoved
in interface java.awt.event.MouseMotionListener
public void mouseEntered(java.awt.event.MouseEvent e)
mouseEntered
in interface java.awt.event.MouseListener
public void mouseExited(java.awt.event.MouseEvent e)
mouseExited
in interface java.awt.event.MouseListener
public void mousePressed(java.awt.event.MouseEvent e)
mousePressed
in interface java.awt.event.MouseListener
public void mouseReleased(java.awt.event.MouseEvent e)
mouseReleased
in interface java.awt.event.MouseListener
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |