cs015.SketchySupport
Class BoundingBox

java.lang.Object
  extended byNGP.Graphics.Shape
      extended byNGP.Graphics.RectangularShape
          extended byNGP.Graphics.FramedRectangularShape
              extended byNGP.Graphics.FramedRectangle
                  extended bycs015.SketchySupport.BoundingBox
All Implemented Interfaces:
Colorable, java.util.EventListener, Graphic, Locatable, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, Reactor, Rotatable, Sizeable

public class BoundingBox
extends FramedRectangle

This class models a bounding box to be used with a SketchyShape. It has handles that can be dragged with the mouse to adjust the size of the shape that it's attached to.

Author:
Brent Shields (bshields)

Field Summary
protected  SketchyShape _shape
           
 
Fields inherited from class NGP.Graphics.Shape
_awtShape, _dpanel
 
Fields inherited from interface NGP.Colorable
DEFAULT_GRAY
 
Constructor Summary
BoundingBox(DrawingPanel dp)
          Creates a bounding box.
 
Method Summary
 void attachShape(SketchyShape shape)
          Sets the SketchyShape that the bounding box will be acting on.
 double degToRad(int deg)
          Converts an angle in degrees to an angle in radians.
 java.awt.Point getCenterLocation()
          Gets the location of the CENTER of the bounding box.
 java.awt.Point getLocation()
          Gets the location of the CENTER of the bounding box.
 void handleDragged(java.awt.event.MouseEvent e, Handle handle)
          This method is called whenever one of the BoundingBox's Handles are dragged.
 void handlePressed(java.awt.event.MouseEvent e, Handle handle)
          This method is called whenever one of the Handles is pressed.
 void handleReleased(java.awt.event.MouseEvent e, Handle handle)
          This method is called whenever one of the Handles is released.
 void hide()
          Hide the bounding box.
 int radToDeg(double rad)
          Converts an angle in radians to an angle in degrees.
 void removeShape()
          This method makes the bounding box lose its reference to its attached shape and then hides the box.
 void resize(java.awt.Point oldLoc, java.awt.Point newLoc, Handle handle)
          This method should be called when a Handle is dragged in an attempt to resize the BoundingBox and attached Shape.
 void rotateClockwise(int degrees)
          Rotates the BoundingBox and its attached shape by the given number of degrees in the clockwise direction.
 java.awt.Point rotatePoint(java.awt.Point p, int degrees)
          A math function that takes a Point p and an angle in degrees and returns p after a clockwise rotation through degrees around the origin.
 void setCenterLocation(java.awt.Point p)
          Change the location of the bounding box and its attached shape.
 void setDimension(java.awt.Dimension d)
          Sets the dimension of the BoundingBox and its attached SketchyShape.
 void setLocation(java.awt.Point p)
          Change the location of the bounding box and its attached shape.
 void setRotation(int rotation)
          Sets the bounding box and its attached shape's rotation.
 void show()
          Show the bounding box.
 void vectorRotate(java.awt.Point tail, java.awt.Point head)
          This method takes two points, finds the angle between vectors from the center of the bounding box to each of the points, and then rotates the bounding box and its attached shape by this angle.
 
Methods inherited from class NGP.Graphics.FramedRectangularShape
actualPaint, getBounds, getThickness, setThickness
 
Methods inherited from class NGP.Graphics.RectangularShape
getDimension, unwrap, wrap
 
Methods inherited from class NGP.Graphics.Shape
contains, drag, getColor, getDrawingPanel, getRotation, intersects, mouseClicked, mouseDragged, mouseEntered, mouseExited, mouseMoved, mousePressed, mouseReleased, paint, react, setColor, setDrawingPanel
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_shape

protected SketchyShape _shape
Constructor Detail

BoundingBox

public BoundingBox(DrawingPanel dp)
Creates a bounding box. It is hidden by default.

Parameters:
dp - the DrawingPanel where the bounding box will be drawn.
Method Detail

attachShape

public void attachShape(SketchyShape shape)
Sets the SketchyShape that the bounding box will be acting on. This method makes the bounding box assume the shape's location, dimension and rotation. After calling this method, all calls to resize(), vectorRotate(), and setLocation() will all effect the attached shape as well as the BoundingBox.

Parameters:
shape - The shape which the bounding box will manipulate.

removeShape

public void removeShape()
This method makes the bounding box lose its reference to its attached shape and then hides the box. The attached shape reference is null after this method is called.


hide

public void hide()
Hide the bounding box. Note that this method has no effect on the shape that is currently attached to the BoundingBox.


show

public void show()
Show the bounding box. Note that this method has no effect on the shape that is currently attached to the BoundingBox.


radToDeg

public int radToDeg(double rad)
Converts an angle in radians to an angle in degrees.

Parameters:
rad - radian value
Returns:
the number of degrees corresponding to rad.

degToRad

public double degToRad(int deg)
Converts an angle in degrees to an angle in radians.

Parameters:
deg - value to convert to radians
Returns:
the radian value of deg

setDimension

public void setDimension(java.awt.Dimension d)
Sets the dimension of the BoundingBox and its attached SketchyShape. Normal users probably do not need to call this method because it is used by the resize() method.

Parameters:
d - the new dimension of the BoundingBox and its attached shape.

setRotation

public void setRotation(int rotation)
Sets the bounding box and its attached shape's rotation. Normal users probably don't need to call this method because it is used by the vectorRotate() method.


setLocation

public void setLocation(java.awt.Point p)
Change the location of the bounding box and its attached shape.

Parameters:
p - the new position of the CENTER of the bounding box.

getLocation

public java.awt.Point getLocation()
Gets the location of the CENTER of the bounding box.


setCenterLocation

public void setCenterLocation(java.awt.Point p)
Change the location of the bounding box and its attached shape.

Parameters:
p - the new position of the CENTER of the bounding box.

getCenterLocation

public java.awt.Point getCenterLocation()
Gets the location of the CENTER of the bounding box.


rotateClockwise

public void rotateClockwise(int degrees)
Rotates the BoundingBox and its attached shape by the given number of degrees in the clockwise direction. Notice that the absolute rotation on the box and shape will be current rotation + degrees, not just degrees.

Parameters:
degrees - the number of degrees to rotate by

rotatePoint

public java.awt.Point rotatePoint(java.awt.Point p,
                                  int degrees)
A math function that takes a Point p and an angle in degrees and returns p after a clockwise rotation through degrees around the origin.

Parameters:
p - the point to be rotated
degrees - the angle through which to rotate p

resize

public void resize(java.awt.Point oldLoc,
                   java.awt.Point newLoc,
                   Handle handle)
This method should be called when a Handle is dragged in an attempt to resize the BoundingBox and attached Shape. The parameters are the old location of the Handle being dragged, and the new location, or where the handle is being dragged to. These locations must be relative to the box. The coordinates for the points should be in an un-rotated, BoundingBox-centered coordinate system.

Parameters:
oldLoc - the old location of the handle in box-relative coordinates
newLoc - what the new location of the handle should be in box- relative coordinates.
handle - the Handle that is being dragged.

vectorRotate

public void vectorRotate(java.awt.Point tail,
                         java.awt.Point head)
This method takes two points, finds the angle between vectors from the center of the bounding box to each of the points, and then rotates the bounding box and its attached shape by this angle. The points passed in must be relative to the bounding box. The coordinates for the points should be in an un-rotated, BoundingBox-centered coordinate system.

Parameters:
tail - the tail of the rotation vector. If the box is being rotated by dragging a Handle, then this would correspond to the location of that Handle.
head - the head of the rotation vector. If the box is being rotated by dragging a Handle, then this would correspond to the location of the mouse after the drag event.

handleDragged

public void handleDragged(java.awt.event.MouseEvent e,
                          Handle handle)
This method is called whenever one of the BoundingBox's Handles are dragged. It currently does nothing. It should be overriden in a subclass to do something useful.

Parameters:
e - the mouse event generated by the dragging.
handle - the Handle being dragged.

handlePressed

public void handlePressed(java.awt.event.MouseEvent e,
                          Handle handle)
This method is called whenever one of the Handles is pressed. It currently does nothing. It should be overriden in a subclass to do something useful.

Parameters:
e - the MouseEvent generated by the press.
handle - the Handle being dragged.

handleReleased

public void handleReleased(java.awt.event.MouseEvent e,
                           Handle handle)
This method is called whenever one of the Handles is released. It currently does nothing. It should be overriden in a subclass to do something useful.

Parameters:
e - the MouseEvent generated by the release.
handle - the Handle being dragged.