CSLib
Class DrawingBox

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--java.awt.Window
                    |
                    +--java.awt.Frame
                          |
                          +--CSLib.ClosableFrame
                                |
                                +--CSLib.DrawingBox

public class DrawingBox
extends 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.

Author:
M. Dennis Mickunas
See Also: ClosableFrame, Graphics

Inner Class Summary
 public classDrawingBox.DBMouseAdapter
          DBMouseAdapter is an inner class that implements
the Mouse listener for DrawingBox.
 public classDrawingBox.DBMouseMotionAdapter
          DBMouseMotionAdapter is an inner class that implements
the Mouse motion listener for DrawingBox.

Field Summary
 (package private) DimensionscreenSize
          the size of the user's screen

Fields inherited from class java.awt.Frame
DEFAULT_CURSOR, CROSSHAIR_CURSOR, TEXT_CURSOR, WAIT_CURSOR, SW_RESIZE_CURSOR, SE_RESIZE_CURSOR, NW_RESIZE_CURSOR, NE_RESIZE_CURSOR, N_RESIZE_CURSOR, S_RESIZE_CURSOR, W_RESIZE_CURSOR, E_RESIZE_CURSOR, HAND_CURSOR, MOVE_CURSOR, NORMAL, ICONIFIED

Fields inherited from class java.awt.Component
TOP_ALIGNMENT, CENTER_ALIGNMENT, BOTTOM_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT

Constructor Summary
DrawingBox()
          Constructs a DrawingBox with the default title.
DrawingBox(String title)
          Constructs a DrawingBox with a specific title.

Method Summary
 voidclear()
          Clears the entire drawing box.
 voidclearRect(int x, int y, int width, int height)
          Clears the specified rectangle by filling it with the background
color of the current drawing box.
 voiddrawCircle(int x, int y, int radius)
          Draws the outline of a circle.
 voiddrawCircle(Point p, int radius)
          Draws the outline of a circle.
 voiddrawImage(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.
 voiddrawLine(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.
 voiddrawOval(int x1, int y1, int width, int height)
          Draws the outline of an oval.
 voiddrawRect(int x, int y, int width, int height)
          Draws the outline of the specified rectangle.
 voiddrawRect(Rectangle r)
          Draws the outline of the specified rectangle given a
Rectangle object.
 voiddrawString(String str, int x, int y)
          Draws the text given by the specified string, using this
drawing box's current font and color.
 voidfillCircle(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.
 voidfillCircle(Point p, int radius)
          Fills a circle with center at point p
and with a given radius, using this drawing box's current color.
 voidfillOval(int x1, int y1, int width, int height)
          Fills an oval bounded by the specified rectangle with this
drawing box's current color.
 voidfillRect(int x, int y, int width, int height)
          Fills the specified rectangle.
 voidfillRect(Rectangle r)
          Fills the specified rectangle given a Rectangle object.
 ColorgetColor()
          Gets this drawing box's current color.
 intgetDrawableHeight()
          Returns the current drawable height (less insets) of this drawing box.
 intgetDrawableWidth()
          Returns the current drawable width (less insets) of this drawing box.
 voidpaint(Graphics g)
          Paints the drawing box.
 voidsetColor(Color c)
          Sets this drawing box's color.
 voidsetDrawableSize(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, getTitle, setTitle, getIconImage, setIconImage, getMenuBar, setMenuBar, isResizable, setResizable, setState, getState, remove, removeNotify, setCursor, getCursorType, getFrames, getAccessibleContext

Methods inherited from class java.awt.Window
pack, show, hide, dispose, toFront, toBack, getToolkit, getWarningString, getLocale, getInputContext, setCursor, getOwner, getOwnedWindows, addWindowListener, removeWindowListener, getListeners, getFocusOwner, postEvent, isShowing, applyResourceBundle, applyResourceBundle, getGraphicsConfiguration

Methods inherited from class java.awt.Container
getComponentCount, countComponents, getComponent, getComponents, getInsets, insets, add, add, add, add, add, remove, remove, removeAll, getLayout, setLayout, doLayout, layout, invalidate, validate, setFont, getPreferredSize, preferredSize, getMinimumSize, minimumSize, getMaximumSize, getAlignmentX, getAlignmentY, update, print, paintComponents, printComponents, addContainerListener, removeContainerListener, deliverEvent, getComponentAt, locate, getComponentAt, findComponentAt, findComponentAt, isAncestorOf, list, list

Methods inherited from class java.awt.Component
getName, setName, getParent, getPeer, setDropTarget, getDropTarget, getTreeLock, isValid, isDisplayable, isVisible, isEnabled, setEnabled, enable, enable, disable, isDoubleBuffered, enableInputMethods, setVisible, show, getForeground, setForeground, getBackground, setBackground, getFont, setLocale, getColorModel, getLocation, getLocationOnScreen, location, setLocation, move, setLocation, getSize, size, setSize, resize, setSize, resize, getBounds, bounds, setBounds, reshape, setBounds, getX, getY, getWidth, getHeight, getBounds, getSize, getLocation, isOpaque, isLightweight, getGraphics, getFontMetrics, getCursor, paintAll, repaint, repaint, repaint, repaint, printAll, imageUpdate, createImage, createImage, prepareImage, prepareImage, checkImage, checkImage, contains, inside, contains, dispatchEvent, addComponentListener, removeComponentListener, addFocusListener, removeFocusListener, addHierarchyListener, removeHierarchyListener, addHierarchyBoundsListener, removeHierarchyBoundsListener, addKeyListener, removeKeyListener, addMouseListener, removeMouseListener, addMouseMotionListener, removeMouseMotionListener, addInputMethodListener, removeInputMethodListener, getInputMethodRequests, handleEvent, mouseDown, mouseDrag, mouseUp, mouseMove, mouseEnter, mouseExit, keyDown, keyUp, action, gotFocus, lostFocus, isFocusTraversable, requestFocus, transferFocus, nextFocus, hasFocus, add, toString, list, list, list, addPropertyChangeListener, removePropertyChangeListener, addPropertyChangeListener, removePropertyChangeListener, setComponentOrientation, getComponentOrientation

Methods inherited from class java.lang.Object
getClass, hashCode, equals, notify, notifyAll, wait, wait, wait

Field Detail

screenSize

Dimension screenSize
the size of the user's screen
Constructor Detail

DrawingBox

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

DrawingBox

public DrawingBox(String title)
Constructs a DrawingBox with a specific title.
Parameters:
title the string to use as this DrawingBox title.
See Also:
java.awt.Graphics.translate(int,int), java.awt.Container.getInsets()
Method Detail

clear

public void clear()
Clears the entire drawing box.
See Also:
java.awt.Graphics.setColor(java.awt.Color), java.awt.Graphics.fillRect(int,int,int,int), java.awt.Graphics.clearRect(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:
java.awt.Graphics.setColor(java.awt.Color), java.awt.Graphics.fillRect(int,int,int,int), java.awt.Graphics.clearRect(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:
java.awt.Graphics.drawOval(int,int,int,int)

drawCircle

public void drawCircle(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)

drawImage

public void drawImage(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:
java.awt.Graphics.drawImage(java.awt.Image,int,int,java.awt.image.ImageObserver), Image, MediaTracker

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:
java.awt.Graphics.drawLine(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:
x the x coordinate of the upper left
corner of the oval to be drawn.
y the y coordinate of the upper left
corner of the oval to be drawn.
width the width of the oval to be drawn.
height the height of the oval to be drawn.
See Also:
java.awt.Graphics.drawOval(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:
java.awt.Graphics.drawRect(int,int,int,int)

drawRect

public void drawRect(Rectangle r)
Draws the outline of the specified rectangle given a
Rectangle object.
The rectangle is drawn using this drawing box's current color.
Parameters:
r the rectangle to be drawn.
See Also:
Rectangle

drawString

public void drawString(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:
java.awt.Graphics.drawString(java.lang.String,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:
java.awt.Graphics.fillOval(int,int,int,int)

fillCircle

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

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:
x the x coordinate of the upper left corner
of the oval to be filled.
y the y coordinate of the upper left corner
of the oval to be filled.
width the width of the oval to be filled.
height the height of the oval to be filled.
See Also:
java.awt.Graphics.fillOval(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:
java.awt.Graphics.fillRect(int,int,int,int)

fillRect

public void fillRect(Rectangle r)
Fills the specified rectangle given a Rectangle object.
The rectangle is filled using this drawing box's current color.
Parameters:
r the rectangle to be drawn.
See Also:
Rectangle

getColor

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

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:
java.awt.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:
java.awt.Container.getInsets()

paint

public void paint(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.
See Also:
java.awt.Container.getInsets()

setColor

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

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:
java.awt.Container.getInsets()

Association Links

to Class java.awt.Graphics

the Graphics object affiliated with this Frame

to Class java.awt.Dimension

the size of the user's screen

to Class java.awt.Image

an off-screen image of everything that is done to g

to Class java.awt.Graphics

the Graphics affiliated with the off-screen image