legos
Class LegoPiece

java.lang.Object
  extended by objectdraw.ObjectDrawObject
      extended by objectdraw.ObjectDrawShape
          extended by legos.LegoPiece
All Implemented Interfaces:
Dependent, DrawableInterface, LocatableInterface
Direct Known Subclasses:
Arm, Body, Hand, Head, Leg, Legs

public abstract class LegoPiece
extends ObjectDrawShape
implements LocatableInterface

A LegoPiece represents some kind of object created by connecting together smaller pieces. Each LegoPiece has a shape of its own, but is also able to have more LegoPieces of specific types attached to it at particular points. So, for example, a LegoPiece representing the body of a lego man only draws the body when it is by itself, but can have a head, legs, and arms attached to it. The LegoPiece class contains methods for accessing the attached pieces (subunits) by simply specifying the number of the subunit. More often you will use methods in a particular subclass that retrieve a particular subunit. So, for example, I could get the head of a lego person with body.getSubunit(0), but it would make more sense to use the getHead() method in the Body class.

Author:
Russell Zahniser (russell@zahniser.net)

Method Summary
 void addToCanvas(DrawingCanvas c)
          Place this object on the specified canvas.
 boolean canAttach(int index, LegoPiece subunit)
          Check whether the given piece can be attached at the given index.
 boolean contains(Location point)
          Return true if the given location is inside this object, false otherwise.
 void detach()
          Remove this piece from the piece it is attached to.
 Location getAttachmentPoint(int index)
          Return the point at which the given subunit attaches.
 DrawingCanvas getCanvas()
          Return the canvas that this object is on.
 Color getColor()
          Return the color of this object
 double getDoubleX()
          Get this object's x coordinate, in double precision.
 double getDoubleY()
          Get this object's y coordinate, in double precision.
 Location getLocation()
          Get this object's location.
 LegoPiece getOwner()
           
 LegoPiece getSubunit(int index)
          Retrieve one of the subunits of this piece
 int getSubunitCount()
          Return the number of subunits that can be attached to this piece.
 int getX()
          Get this object's x coordinate.
 int getY()
          Get this object's y coordinate.
 void hide()
          Make this object invisible.
 boolean isHidden()
          Return true if this object has been rendered invisible with a call to its hide() method, false if it is still being drawn.
 void move(double dx, double dy)
          Shift (translate) this object left or right by an amount dx, and up or down by an amount dy.
 void moveTo(double x, double y)
          Move the reference point of this object to the given location.
 void moveTo(Location point)
          Move the reference point of this object to the given location.
 void removeFromCanvas()
          Remove this object from its canvas.
 void removeSubunit(int index)
          Remove the given subunit from this piece.
 void removeSubunit(LegoPiece subunit)
          Remove the given subunit from this piece.
 void sendBackward()
          Move this object one step backward in the draw order, causing it to be drawn underneath the objects that are in front of it.
 void sendForward()
          Move this object one step forward in the draw order, causing it to be drawn on top of the objects that are behind it.
 void sendToBack()
          Move this object to the very back of the draw order, causing it to be drawn underneath all the other objects.
 void sendToFront()
          Move this object to the very front of the draw order, causing it to be drawn on top of all the other objects.
 void setColor(Color c)
          Change the color of this object.
 void setSubunit(int index, LegoPiece subunit)
          Attach the given piece at the given index.
 void show()
          Make an object visible again after it had been hidden.
 String toString()
          Return a string describing how this object might be constructed.
 
Methods inherited from class objectdraw.ObjectDrawShape
clearCanvas, draw, draw, getDrawables, getShape, makeShape, removeCanvas, setMyShape, update
 
Methods inherited from class objectdraw.ObjectDrawObject
deferUpdates, depend, runUpdates, undepend
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface objectdraw.DrawableInterface
draw, getShape
 

Method Detail

addToCanvas

public void addToCanvas(DrawingCanvas c)
Place this object on the specified canvas. The object will be removed from its current canvas, if it is on a canvas that is not the one it is being moved to. The object will be at the front of the canvas, as if it had just been drawn. If the given canvas is null the object is simply removed from its current canvas.

Specified by:
addToCanvas in interface DrawableInterface
Overrides:
addToCanvas in class ObjectDrawShape
Parameters:
c - DrawingCanvas to place this object on

canAttach

public boolean canAttach(int index,
                         LegoPiece subunit)
Check whether the given piece can be attached at the given index. If this returns true, you can safely call setSubunit(index, subunit) with the same index and subunit; if it returns false, attaching that subunit would be illegal.

Parameters:
index - Index to attach subunit at, between 0 and getSubunitCount() - 1.
subunit - Subunit that will be checked for whether it can be attached there.
Returns:
true if the given piece can be attached at the given index, false otherwise.

contains

public boolean contains(Location point)
Return true if the given location is inside this object, false otherwise. Because Lines and FramedArcs have no "inside", a point is considered to be contained in one of them as long as it is within a few pixels of the line.

Specified by:
contains in interface DrawableInterface
Parameters:
point - Location to test
Returns:
whether the given location is inside this shape

detach

public void detach()
Remove this piece from the piece it is attached to. It will appear on the canvas a short distance away.


getAttachmentPoint

public Location getAttachmentPoint(int index)
Return the point at which the given subunit attaches. This will give you the same location that you would get with getSubunit(index).getLocation(), except that it will return a valid Location even if there is no subunit currently attached there.

Parameters:
index - Index of subunit, between 0 and getSubunitCount() - 1.
Returns:
Location at which the given subunit would be placed if it were attached.

getCanvas

public DrawingCanvas getCanvas()
Return the canvas that this object is on. If it has been removed from its canvas, this method will return null.

Specified by:
getCanvas in interface DrawableInterface
Overrides:
getCanvas in class ObjectDrawShape
Returns:
the DrawingCanvas that this object is on.

getColor

public Color getColor()
Return the color of this object

Specified by:
getColor in interface DrawableInterface
Overrides:
getColor in class ObjectDrawShape
Returns:
the Color of this object

getDoubleX

public double getDoubleX()
Get this object's x coordinate, in double precision.

Specified by:
getDoubleX in interface LocatableInterface
Returns:
this object's x coordinate, in double precision

getDoubleY

public double getDoubleY()
Get this object's y coordinate, in double precision.

Specified by:
getDoubleY in interface LocatableInterface
Returns:
this object's y coordinate, in double precision

getLocation

public Location getLocation()
Get this object's location.

Specified by:
getLocation in interface LocatableInterface
Returns:
the Location of this object.

getOwner

public LegoPiece getOwner()

getSubunit

public LegoPiece getSubunit(int index)
Retrieve one of the subunits of this piece

Parameters:
index - Index of subunit, between 0 and getSubunitCount() - 1.
Returns:
The LegoPiece at that index, or null if there is nothing attached there.

getSubunitCount

public int getSubunitCount()
Return the number of subunits that can be attached to this piece. So, for example, a Legs will return 2, because there are two places where a Leg can be attached to it.

Returns:
the number of subunits that can be attached to this piece.

getX

public int getX()
Get this object's x coordinate.

Specified by:
getX in interface LocatableInterface
Returns:
this object's x coordinate

getY

public int getY()
Get this object's y coordinate.

Specified by:
getY in interface LocatableInterface
Returns:
this object's y coordinate

hide

public void hide()
Make this object invisible. It stays in position on its canvas; it is simply not drawn. This method has no effect if the object was already hidden.

Specified by:
hide in interface DrawableInterface
Overrides:
hide in class ObjectDrawShape

isHidden

public boolean isHidden()
Return true if this object has been rendered invisible with a call to its hide() method, false if it is still being drawn.

Specified by:
isHidden in interface DrawableInterface
Overrides:
isHidden in class ObjectDrawShape
Returns:
whether this object is hidden

move

public void move(double dx,
                 double dy)
Shift (translate) this object left or right by an amount dx, and up or down by an amount dy. A negative dx indicates a shift to the left; a negative dy indicates a shift up

Specified by:
move in interface DrawableInterface
Overrides:
move in class ObjectDrawShape
Parameters:
dx - amount of x translation
dy - amount of y translation

moveTo

public void moveTo(double x,
                   double y)
Move the reference point of this object to the given location. For a Drawable2D object, the reference point is the top left corner; for a line, it is the start point.

Specified by:
moveTo in interface DrawableInterface
Overrides:
moveTo in class ObjectDrawShape
Parameters:
x - x coordinate to move to
y - y coordinate to move to

moveTo

public void moveTo(Location point)
Move the reference point of this object to the given location. For a Drawable2D object, the reference point is the top left corner; for a line, it is the start point.

Specified by:
moveTo in interface DrawableInterface
Overrides:
moveTo in class ObjectDrawShape
Parameters:
point - Location to move to

removeFromCanvas

public void removeFromCanvas()
Remove this object from its canvas.

Specified by:
removeFromCanvas in interface DrawableInterface
Overrides:
removeFromCanvas in class ObjectDrawShape

removeSubunit

public void removeSubunit(int index)
Remove the given subunit from this piece.

Parameters:
index - of subunit o remove

removeSubunit

public void removeSubunit(LegoPiece subunit)
Remove the given subunit from this piece.

Parameters:
subunit - Piece to remove

sendBackward

public void sendBackward()
Move this object one step backward in the draw order, causing it to be drawn underneath the objects that are in front of it.

Specified by:
sendBackward in interface DrawableInterface
Overrides:
sendBackward in class ObjectDrawShape

sendForward

public void sendForward()
Move this object one step forward in the draw order, causing it to be drawn on top of the objects that are behind it.

Specified by:
sendForward in interface DrawableInterface
Overrides:
sendForward in class ObjectDrawShape

sendToBack

public void sendToBack()
Move this object to the very back of the draw order, causing it to be drawn underneath all the other objects.

Specified by:
sendToBack in interface DrawableInterface
Overrides:
sendToBack in class ObjectDrawShape

sendToFront

public void sendToFront()
Move this object to the very front of the draw order, causing it to be drawn on top of all the other objects.

Specified by:
sendToFront in interface DrawableInterface
Overrides:
sendToFront in class ObjectDrawShape

setColor

public void setColor(Color c)
Change the color of this object.

Specified by:
setColor in interface DrawableInterface
Overrides:
setColor in class ObjectDrawShape
Parameters:
c - new Color for this object

setSubunit

public void setSubunit(int index,
                       LegoPiece subunit)
Attach the given piece at the given index. If there is already a piece there, it will be removed from the canvas. If the piece being added is already a subunit of another piece, it will be detached from that piece before being attached to this one. The piece to be attached will also be automatically moved to the correct position. An error will occur if the piece is not the proper type to be attached at the given index.

Parameters:
index - Index to attach subunit at, between 0 and getSubunitCount() - 1.
subunit - Subunit that will be attached there.

show

public void show()
Make an object visible again after it had been hidden. This method has no effect if the object was already visible.

Specified by:
show in interface DrawableInterface
Overrides:
show in class ObjectDrawShape

toString

public String toString()
Return a string describing how this object might be constructed.

Overrides:
toString in class ObjectDrawObject