CSLib
Class DrawingBox

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--java.awt.Window
                    |
                    +--java.awt.Frame
                          |
                          +--CSLib.ClosableFrame
                                |
                                +--CSLib.DrawingBox
All Implemented Interfaces:
javax.accessibility.Accessible, java.util.EventListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.awt.event.WindowListener

public class DrawingBox
extends CSLib.ClosableFrame

DrawingBox is a frame upon which many of the java.awt.Graphics operations can be performed. As a frame, it exists as a separate window, and the operations on it may be performed from anywhere -- not just from within a paint method (as is normal with a frame's Graphics object). (However, to be used effectively as a teaching aid, operations on a DrawingBox should be performed from within a single method, which later can be converted to a paint method.)

Care must be taken that when this frame is obscured, that subsequently it can repaint itself. An off-screen Graphics context keeps an up-to-date copy of this frame's Graphics at all times. The origin of the coordinate system is translated so that it is in the upper left of the drawable portion of the frame. Consequently, DrawingBox behaves more like a Canvas object that occupies the drawable portion of a frame.

In order to distinguish the Frame from its drawable portion, methods getDrawableWidth and getDrawableHeight are provided.

DrawingBox uses inner classes DBMouseAdapter (extending java.awt.event.MouseAdapter) to implement mouse listeners, and DBMouseMotionAdapter (extending java.awt.event.MouseMotionAdapter) to implement mouse motion listeners.

See Also:
ClosableFrame, Graphics, Serialized Form

Nested Class Summary
 class DrawingBox.DBMouseAdapter
          DBMouseAdapter is an inner class that implements the Mouse listener for DrawingBox.
 class DrawingBox.DBMouseMotionAdapter
          DBMouseMotionAdapter is an inner class that implements the Mouse motion listener for DrawingBox.
 
Nested classes inherited from class java.awt.Frame
java.awt.Frame.AccessibleAWTFrame
 
Nested classes inherited from class java.awt.Window
java.awt.Window.AccessibleAWTWindow
 
Nested classes inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
 
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 java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
DrawingBox()
          Constructs a DrawingBox with the default title.
DrawingBox(java.lang.String title)
          Constructs a DrawingBox with a specific title.
 
Method Summary
 void clear()
          Clears the entire drawing box.
 void clearRect(int x, int y, int width, int height)
          Clears the specified rectangle by filling it with the background color of the current drawing box.
 void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
          Draws the outline of a circular or elliptical arc covering the specified rectangle.
 void drawCircle(int x, int y, int radius)
          Draws the outline of a circle.
 void drawCircle(java.awt.Point p, int radius)
          Draws the outline of a circle.
 void drawImage(java.awt.Image img, int x, int y)
          Draws the specified image with its top-left corner at (x,y) in this drawing box's coordinate system.
 void drawLine(int x1, int x2, int y1, int y2)
          Draws a line, using the current color, between the points (x1,y1) and (x2,y2) in this drawing box's coordinate system.
 void drawOval(int x1, int y1, int width, int height)
          Draws the outline of an oval.
 void drawRect(int x, int y, int width, int height)
          Draws the outline of the specified rectangle.
 void drawString(java.lang.String str, int x, int y)
          Draws the text given by the specified string, using this drawing box's current font and color.
 void fillCircle(int x, int y, int radius)
          Fills a circle with center at (x,y) and with a given radius using this drawing box's current color.
 void fillCircle(java.awt.Point p, int radius)
          Fills a circle with center at point p and with a given radius, using this drawing box's current color.
 void fillOval(int x1, int y1, int width, int height)
          Fills an oval bounded by the specified rectangle with this drawing box's current color.
 void fillRect(int x, int y, int width, int height)
          Fills the specified rectangle.
 java.awt.Color getColor()
          Gets this drawing box's current color.
 int getDrawableHeight()
          Returns the current drawable height (less insets) of this drawing box.
 int getDrawableWidth()
          Returns the current drawable width (less insets) of this drawing box.
 void paint(java.awt.Graphics g)
          Paints the drawing box.
 void setColor(java.awt.Color c)
          Sets this drawing box's color.
 void setDrawableSize(int width, int height)
          Sets the drawable size of this drawing box by setting a smaller size, then increasing it by the amount of the insets.
 
Methods inherited from class CSLib.ClosableFrame
windowActivated, windowClosed, windowClosing, windowDeactivated, windowDeiconified, windowIconified, windowOpened
 
Methods inherited from class java.awt.Frame
addNotify, finalize, getAccessibleContext, getCursorType, getExtendedState, getFrames, getIconImage, getMaximizedBounds, getMenuBar, getState, getTitle, isResizable, isUndecorated, paramString, remove, removeNotify, setCursor, setExtendedState, setIconImage, 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, isFocusableWindow, isFocusCycleRoot, isFocused, isShowing, pack, postEvent, processEvent, processWindowEvent, processWindowFocusEvent, processWindowStateEvent, removeWindowFocusListener, removeWindowListener, removeWindowStateListener, setCursor, setFocusableWindowState, setFocusCycleRoot, setLocationRelativeTo, show, toBack, toFront
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, print, printComponents, processContainerEvent, remove, remove, removeAll, removeContainerListener, setFocusTraversalKeys, setFocusTraversalPolicy, setFont, setLayout, transferFocusBackward, transferFocusDownCycle, update, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isOpaque, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.awt.MenuContainer
getFont, postEvent
 

Constructor Detail

DrawingBox

public DrawingBox()
Constructs a DrawingBox with the default title.


DrawingBox

public DrawingBox(java.lang.String title)
Constructs a DrawingBox with a specific title.

Parameters:
title - the string to use as this DrawingBox title.
See Also:
Graphics.translate(int, int), Container.getInsets()
Method Detail

paint

public void paint(java.awt.Graphics g)
Paints the drawing box. This will be called when the drawing box must be restored. The off-screen image in buffer has a true copy of what was drawn in the drawing box.

Overrides:
paint in class java.awt.Container
See Also:
Container.getInsets()

getDrawableWidth

public int getDrawableWidth()
Returns the current drawable width (less insets) of this drawing box.

Returns:
the current drawable width of this drawing box.
See Also:
Container.getInsets()

getDrawableHeight

public int getDrawableHeight()
Returns the current drawable height (less insets) of this drawing box.

Returns:
the current drawable height of this drawing box.
See Also:
Container.getInsets()

setDrawableSize

public void setDrawableSize(int width,
                            int height)
Sets the drawable size of this drawing box by setting a smaller size, then increasing it by the amount of the insets.

Parameters:
width - the desired drawable width
height - the desired drawable height
See Also:
Container.getInsets()

getColor

public java.awt.Color getColor()
Gets this drawing box's current color.

Returns:
this drawing box's current color.
See Also:
Color, Graphics.getColor()

setColor

public void setColor(java.awt.Color c)
Sets this drawing box's color.

Parameters:
c - the desired drawing box's color.
See Also:
Color, Graphics.setColor(java.awt.Color)

drawArc

public void drawArc(int x,
                    int y,
                    int width,
                    int height,
                    int startAngle,
                    int arcAngle)
Draws the outline of a circular or elliptical arc covering the specified rectangle.

The resulting arc begins at startAngle and extends for arcAngle degrees, using the current color. Angles are interpreted such that 0 degrees is at the 3 o'clock position. A positive value indicates a counter-clockwise rotation while a negative value indicates a clockwise rotation.

The center of the arc is the center of the rectangle whose upper left hand corner is located at (x, y) and whose size is specified by the width and height arguments.

The resulting arc covers an area width + 1 pixels wide by height + 1 pixels tall.

The angles are specified relative to the non-square extents of the bounding rectangle such that 45 degrees always falls on the line from the center of the ellipse to the upper right corner of the bounding rectangle. As a result, if the bounding rectangle is noticeably longer in one axis than the other, the angles to the start and end of the arc segment will be skewed farther along the longer axis of the bounds. Parameters:

Parameters:
x - - the x coordinate of the upper-left corner of the arc to be drawn.
y - - the y coordinate of the upper-left corner of the arc to be drawn.
width - - the width of the arc to be drawn.
height - - the height of the arc to be drawn.
startAngle - - the beginning angle.
arcAngle - - the angular extent of the arc, relative to the start angle.

drawLine

public void drawLine(int x1,
                     int x2,
                     int y1,
                     int y2)
Draws a line, using the current color, between the points (x1,y1) and (x2,y2) in this drawing box's coordinate system.

Parameters:
x1 - the first point's x coordinate.
y1 - the first point's y coordinate.
x2 - the second point's x coordinate.
y2 - the second point's y coordinate.
See Also:
Graphics.drawLine(int, int, int, int)

drawRect

public void drawRect(int x,
                     int y,
                     int width,
                     int height)
Draws the outline of the specified rectangle. The left and right edges of the rectangle are at x and x+width. The top and bottom edges are at y and y+height. The rectangle is drawn using this drawing box's current color.

Parameters:
x - the x coordinate of the rectangle to be drawn.
y - the y coordinate of the rectangle to be drawn.
width - the width of the rectangle to be drawn.
height - the height of the rectangle to be drawn.
See Also:
Graphics.drawRect(int, int, int, int)

fillRect

public void fillRect(int x,
                     int y,
                     int width,
                     int height)
Fills the specified rectangle. The left and right edges of the rectangle are at x and x+width-1. The top and bottom edges are at y and y+height-1. The resulting rectangle covers an area width pixels wide by height pixels tall. The rectangle is filled using this drawing box's current color.

Parameters:
x - the x coordinate of the rectangle to be filled.
y - the y coordinate of the rectangle to be filled.
width - the width of the rectangle to be filled.
height - the height of the rectangle to be filled.
See Also:
Graphics.fillRect(int, int, int, int)

drawCircle

public void drawCircle(int x,
                       int y,
                       int radius)
Draws the outline of a circle. The result is a circle with center at (x,y) and a given radius.

Parameters:
x - the x coordinate of the center of the circle to be drawn.
y - the y coordinate of the center of the circle to be drawn.
radius - the radius of the circle to be drawn.
See Also:
Graphics.drawOval(int, int, int, int)

drawCircle

public void drawCircle(java.awt.Point p,
                       int radius)
Draws the outline of a circle. The result is a circle with center at p and a given radius.

Parameters:
p - the point giving the center of the circle to be drawn.
radius - the radius of the circle to be drawn.
See Also:
CSLib#drawCircle(int, int, int)

fillCircle

public void fillCircle(int x,
                       int y,
                       int radius)
Fills a circle with center at (x,y) and with a given radius using this drawing box's current color.

Parameters:
x - the x coordinate of the center of the circle to be filled.
y - the y coordinate of the center of the circle to be filled.
radius - the radius of the circle to be filled.
See Also:
Graphics.fillOval(int, int, int, int)

fillCircle

public void fillCircle(java.awt.Point p,
                       int radius)
Fills a circle with center at point p and with a given radius, using this drawing box's current color.

Parameters:
p - the point giving the center of the circle to be filled.
radius - the radius of the circle to be filled.
See Also:
CSLib#fillCircle(int, int, int)

drawString

public void drawString(java.lang.String str,
                       int x,
                       int y)
Draws the text given by the specified string, using this drawing box's current font and color. The baseline of the leftmost character is at position (x,y) in this drawing box's translated coordinate system.

Parameters:
str - the string to be drawn.
x - the x coordinate.
y - the y coordinate.
See Also:
Graphics.drawString(java.lang.String, int, int)

fillOval

public void fillOval(int x1,
                     int y1,
                     int width,
                     int height)
Fills an oval bounded by the specified rectangle with this drawing box's current color.

Parameters:
width - the width of the oval to be filled.
height - the height of the oval to be filled.
See Also:
Graphics.fillOval(int, int, int, int)

drawOval

public void drawOval(int x1,
                     int y1,
                     int width,
                     int height)
Draws the outline of an oval. The result is an ellipse that fits within the rectangle specified by the x, y, width, and height arguments.

Parameters:
width - the width of the oval to be drawn.
height - the height of the oval to be drawn.
See Also:
Graphics.drawOval(int, int, int, int)

clearRect

public void clearRect(int x,
                      int y,
                      int width,
                      int height)
Clears the specified rectangle by filling it with the background color of the current drawing box.

Parameters:
x - the x coordinate of the rectangle to clear.
y - the y coordinate of the rectangle to clear.
width - the width of the rectangle to clear.
height - the height of the rectangle to clear.
See Also:
Graphics.setColor(java.awt.Color), Graphics.fillRect(int, int, int, int), Graphics.clearRect(int, int, int, int)

clear

public void clear()
Clears the entire drawing box.

See Also:
Graphics.setColor(java.awt.Color), Graphics.fillRect(int, int, int, int), Graphics.clearRect(int, int, int, int)

drawImage

public void drawImage(java.awt.Image img,
                      int x,
                      int y)
Draws the specified image with its top-left corner at (x,y) in this drawing box's coordinate system.

This method waits for the image to be fully loaded.

Parameters:
img - the specified image to be drawn.
x - the x coordinate.
y - the y coordinate.
See Also:
Graphics.drawImage(java.awt.Image, int, int, java.awt.image.ImageObserver), Image, MediaTracker