java.lang.Objectobjectdraw.ObjectDrawObject
objectdraw.ObjectDrawShape
legos.LegoPiece
public abstract class LegoPiece
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
LegoPiece
s 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.
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 |
---|
public void addToCanvas(DrawingCanvas c)
null
the object is simply removed from its current canvas.
addToCanvas
in interface DrawableInterface
addToCanvas
in class ObjectDrawShape
c
- DrawingCanvas
to place this object onpublic boolean canAttach(int index, LegoPiece subunit)
true
,
you can safely call setSubunit(index, subunit)
with the same index and subunit; if it
returns false
, attaching that subunit would be illegal.
index
- Index to attach subunit at, between 0
and getSubunitCount() - 1
.subunit
- Subunit that will be checked for whether it can be attached there.
true
if the given piece can be attached at the given index, false
otherwise.public boolean contains(Location point)
true
if the given location is inside this object, false
otherwise. Because Line
s and FramedArc
s 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.
contains
in interface DrawableInterface
point
- Location
to test
public void detach()
public Location getAttachmentPoint(int index)
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.
public DrawingCanvas getCanvas()
null
.
getCanvas
in interface DrawableInterface
getCanvas
in class ObjectDrawShape
DrawingCanvas
that this object is on.public Color getColor()
getColor
in interface DrawableInterface
getColor
in class ObjectDrawShape
Color
of this objectpublic double getDoubleX()
getDoubleX
in interface LocatableInterface
public double getDoubleY()
getDoubleY
in interface LocatableInterface
public Location getLocation()
getLocation
in interface LocatableInterface
Location
of this object.public LegoPiece getOwner()
public LegoPiece getSubunit(int index)
index
- Index of subunit, between 0
and getSubunitCount() - 1
.
LegoPiece
at that index, or null
if there is nothing attached there.public int getSubunitCount()
Legs
will return 2, because there are two places where a Leg
can be attached to it.
public int getX()
getX
in interface LocatableInterface
public int getY()
getY
in interface LocatableInterface
public void hide()
hide
in interface DrawableInterface
hide
in class ObjectDrawShape
public boolean isHidden()
true
if this object has been rendered invisible with a call to its
hide()
method, false
if it is still being drawn.
isHidden
in interface DrawableInterface
isHidden
in class ObjectDrawShape
public void move(double dx, double dy)
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
move
in interface DrawableInterface
move
in class ObjectDrawShape
dx
- amount of x translationdy
- amount of y translationpublic void moveTo(double x, double y)
moveTo
in interface DrawableInterface
moveTo
in class ObjectDrawShape
x
- x coordinate to move toy
- y coordinate to move topublic void moveTo(Location point)
moveTo
in interface DrawableInterface
moveTo
in class ObjectDrawShape
point
- Location
to move topublic void removeFromCanvas()
removeFromCanvas
in interface DrawableInterface
removeFromCanvas
in class ObjectDrawShape
public void removeSubunit(int index)
index
- of subunit o removepublic void removeSubunit(LegoPiece subunit)
subunit
- Piece to removepublic void sendBackward()
sendBackward
in interface DrawableInterface
sendBackward
in class ObjectDrawShape
public void sendForward()
sendForward
in interface DrawableInterface
sendForward
in class ObjectDrawShape
public void sendToBack()
sendToBack
in interface DrawableInterface
sendToBack
in class ObjectDrawShape
public void sendToFront()
sendToFront
in interface DrawableInterface
sendToFront
in class ObjectDrawShape
public void setColor(Color c)
setColor
in interface DrawableInterface
setColor
in class ObjectDrawShape
c
- new Color
for this objectpublic void setSubunit(int index, LegoPiece subunit)
index
- Index to attach subunit at, between 0
and getSubunitCount() - 1
.subunit
- Subunit that will be attached there.public void show()
show
in interface DrawableInterface
show
in class ObjectDrawShape
public String toString()
toString
in class ObjectDrawObject