java.lang.Objectobjectdraw.ObjectDrawObject
objectdraw.ObjectDrawShape
objectdraw.Turtle
public class Turtle
A Turtle
object allows you to draw lines and filled-in polygons by giving
instructions to a computer creature called a "turtle" that moves around on the screen and
draws a line as it goes. The instructions are of the form "move forward this far" or "turn
right this many degrees". Often it is easier to draw graphics this way than it is to try to
figure out the coordinates of all the points involved. So, for example, I can draw a pentagon
without needing any trigonometry, because all it takes to walk out a pentagon is to move
forward, turn right 72°, and repeat that five times. If I want to fill in that pentagon,
all I need to do is to tell the turtle to beginFill()
when it starts drawing,
and then to close()
the shape and endFill()
when it is done.
Once a turtle has completed its sketch, you can move that sketch around and resize it just as
you would any object on the canvas. You can also rotate a turtle sketch. Resizing and
rotating are centered around the point at which the turtle was located when it was first
created.
Because the sketch may contain many different colors - I can change the pen color as I move,
and each fill area has its own color - the setColor()
method would not work to
recolor the sketch after it is done. Instead, there is a method recolorSketch()
that can be used to take all parts of the sketch that were originally in a particular color,
and change them to another color.
Field Summary |
---|
Fields inherited from interface objectdraw.DrawableStrokeInterface |
---|
CLICK_SIZE, DEFAULT_STROKE |
Constructor Summary | |
---|---|
Turtle(Color color,
DrawingCanvas canvas)
Construct a Turtle at the center of canvas , facing to the right,
and drawing a line in the given color . |
|
Turtle(double x,
double y,
Color color,
DrawingCanvas canvas)
Construct a Turtle at (x , y ), facing to the right,
and drawing a line in the given color . |
|
Turtle(double x,
double y,
DrawingCanvas canvas)
Construct a Turtle at (x , y ), facing to the right,
and drawing a line in black. |
|
Turtle(DrawingCanvas canvas)
Construct a Turtle at the center of canvas , facing to the right,
and drawing a line in black. |
|
Turtle(Location start,
Color color,
DrawingCanvas canvas)
Construct a Turtle at start , facing to the right,
and drawing a line in the given color . |
|
Turtle(Location start,
DrawingCanvas canvas)
Construct a Turtle at start , facing to the right,
and drawing a line in the given color . |
|
Turtle(Turtle clone,
DrawingCanvas canvas)
Construct a clone of clone and place it on the given canvas. |
Method Summary | |
---|---|
void |
addToCanvas(DrawingCanvas c)
Place this object on the specified canvas. |
void |
beginFill(Color fillColor)
Tell this turtle to begin outlining a shape that will be filled in with fillColor . |
void |
clearSketch()
Clear this turtle's sketch. |
void |
close()
Close the shape that the turtle is currently drawing by connecting a line back to the starting point. |
boolean |
contains(Location point)
Return true if the given location is inside this object, false
otherwise. |
void |
endFill()
Finish up the fill region you are currently working on, and go back to adding to the previously active fill region, if there is one. |
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. |
double |
getHeading()
Get the turtle's heading - the direction, as a degree angle, that it is facing. |
double |
getLineWidth()
Return the width of this object's lines. |
Location |
getLocation()
Get this object's location. |
double |
getSketchDoubleX()
Get the x coordinate of the origin of the sketch, in double precision. |
double |
getSketchDoubleY()
Get the y coordinate of the origin of the sketch, in double precision. |
Location |
getSketchLocation()
Get the location of the origin of the sketch. |
double |
getSketchRotation()
Get the angle of rotation of this turtle's sketch |
double |
getSketchScale()
Get the scale factor of the sketch - the degree to which it has been shrunk or expanded. |
int |
getSketchX()
Get the x coordinate of the origin of the sketch, in int precision. |
int |
getSketchY()
Get the y coordinate of the origin of the sketch, in int precision. |
BasicStroke |
getStroke()
Return the stroke used for this object's lines. |
int |
getX()
Get this object's x coordinate. |
int |
getY()
Get this object's y coordinate. |
void |
hide()
Make this object invisible. |
void |
hideTurtle()
A little triangle-shaped icon represents the turtle while it is drawing the sketch, to make it easier for you to see where the turtle is and which way it is facing when you are trying to figure out how to get it to draw a shape. |
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. |
boolean |
isPenDown()
Determine whether the turtle is leaving a line behind as it moves |
boolean |
isTurtleHidden()
Determine whether the turtle's icon is hidden |
void |
lt()
Turn 90° to the left. |
void |
lt(double angle)
Turn angle degrees to the left |
void |
move()
Move forward 40 pixels |
void |
move(double distance)
Move forward distance pixels |
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 |
moveSketch(double dx,
double dy)
Move the turtle and its entire sketch by the given amount in the x and y. |
void |
moveSketchTo(double x,
double y)
Move the origin on the sketch to ( x , y ). |
void |
moveSketchTo(Location point)
Move the origin on the sketch to the given Location . |
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 |
penDown()
Tell the turtle to resume drawing a line behind it as it moves. |
void |
penUp()
Tell the turtle to lift up its pen, so that it moves without drawing a line behind it. |
void |
recolorSketch(Color original,
Color substitute)
Replace all parts of the sketch that were drawn in the color original with
the new color substitute . |
void |
removeFromCanvas()
Remove this object from its canvas. |
void |
rotateSketch(double byAngle)
Rotate this sketch by the given angle, in degrees. |
void |
rt()
Turn 90° to the right. |
void |
rt(double angle)
Turn angle degrees to the right |
void |
scaleSketch(double byFactor)
Add a scale factor to this turtle's sketch, causing it to be shrunk or expanded. |
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 |
setHeading(double heading)
Give the turtle a new heading |
void |
setLineWidth(double width)
Set the width of this object's lines |
void |
setSketchRotation(double angle)
Set the angle, in degrees, by which this sketch should be rotated. |
void |
setSketchScale(double factor)
Set the scale factor of this turtle's sketch, causing it to be shrunk or expanded. |
void |
setStroke(BasicStroke stroke)
Set the stroke used for this object's lines |
void |
show()
Make an object visible again after it had been hidden. |
void |
showTurtle()
Show the turtle icon again after it had been hidden |
String |
toString()
Return a string describing how this object might be constructed. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public Turtle(Color color, DrawingCanvas canvas)
Turtle
at the center of canvas
, facing to the right,
and drawing a line in the given color
.
color
- Color
the turtle will be drawing incanvas
- DrawingCanvas
on which the turtle should be placedpublic Turtle(double x, double y, Color color, DrawingCanvas canvas)
Turtle
at (x
, y
), facing to the right,
and drawing a line in the given color
.
x
- x coordinate of the turtley
- y coordinate of the turtlecolor
- Color
the turtle will be drawing incanvas
- DrawingCanvas
on which the turtle should be placedpublic Turtle(double x, double y, DrawingCanvas canvas)
Turtle
at (x
, y
), facing to the right,
and drawing a line in black.
x
- x coordinate of the turtley
- y coordinate of the turtlecanvas
- DrawingCanvas
on which the turtle should be placedpublic Turtle(DrawingCanvas canvas)
Turtle
at the center of canvas
, facing to the right,
and drawing a line in black.
canvas
- DrawingCanvas
on which the turtle should be placedpublic Turtle(Location start, Color color, DrawingCanvas canvas)
Turtle
at start
, facing to the right,
and drawing a line in the given color
.
start
- the Location
at which the turtle should be placedcolor
- Color
the turtle will be drawing incanvas
- DrawingCanvas
on which the turtle should be placedpublic Turtle(Location start, DrawingCanvas canvas)
Turtle
at start
, facing to the right,
and drawing a line in the given color
.
start
- the Location
at which the turtle should be placedcanvas
- DrawingCanvas
on which the turtle should be placedpublic Turtle(Turtle clone, DrawingCanvas canvas)
clone
and place it on the given canvas. This allows you
to sketch a shape once with a single Turtle
and then use that as a template
to make more copies of that shape.
clone
- Turtle
to copycanvas
- DrawingCanvas
on which the new turtle should be placedMethod Detail |
---|
public void addToCanvas(DrawingCanvas c)
null
the object is simply removed from its current canvas.
addToCanvas
in interface DrawableInterface
addToCanvas
in class objectdraw.ObjectDrawShape
c
- DrawingCanvas
to place this object onpublic void beginFill(Color fillColor)
fillColor
. You should later call endFill()
once the shape
has been outlined. If a new fill region is started with another call to
beginFill()
before you call endFill()
, the turtle will start
adding to the new fill region instead and will make no changes to the previous fill
region until the new one is ended.
fillColor
- Color
to fill in withpublic void clearSketch()
public void close()
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 endFill()
public DrawingCanvas getCanvas()
null
.
getCanvas
in interface DrawableInterface
getCanvas
in class objectdraw.ObjectDrawShape
DrawingCanvas
that this object is on.public Color getColor()
getColor
in interface DrawableInterface
getColor
in class objectdraw.ObjectDrawShape
Color
of this objectpublic double getDoubleX()
getDoubleX
in interface LocatableInterface
public double getDoubleY()
getDoubleY
in interface LocatableInterface
public double getHeading()
public double getLineWidth()
getLineWidth
in interface DrawableStrokeInterface
public Location getLocation()
Location
object
will have the same effect as moving the object. So, the following two lines
of code are equivalent:
object.move(10, 20);
object.getLocation().translate(10, 20);
Location
offset by some
amount from the Location
of this object, without actually
moving this object, use the offset()
method in Location
,
which creates a new Location
.
getLocation
in interface LocatableInterface
Location
of this object.public double getSketchDoubleX()
double
precision. The
origin is the point on the sketch where the turtle started out.
double
precisionpublic double getSketchDoubleY()
double
precision. The
origin is the point on the sketch where the turtle started out.
double
precisionpublic Location getSketchLocation()
Location
objectpublic double getSketchRotation()
public double getSketchScale()
public int getSketchX()
int
precision. The
origin is the point on the sketch where the turtle started out.
int
precisionpublic int getSketchY()
int
precision. The
origin is the point on the sketch where the turtle started out.
int
precisionpublic BasicStroke getStroke()
getStroke
in interface DrawableStrokeInterface
public int getX()
getX
in interface LocatableInterface
public int getY()
getY
in interface LocatableInterface
public void hide()
hide
in interface DrawableInterface
hide
in class objectdraw.ObjectDrawShape
public void hideTurtle()
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 objectdraw.ObjectDrawShape
public boolean isPenDown()
public boolean isTurtleHidden()
public void lt()
public void lt(double angle)
angle
degrees to the left
angle
- number of degrees to turnpublic void move()
public void move(double distance)
distance
pixels
distance
- number of pixels to movepublic 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 objectdraw.ObjectDrawShape
dx
- amount of x translationdy
- amount of y translationpublic void moveSketch(double dx, double dy)
dx
- distance to shift the sketch in the x directiondy
- distance to shift the sketch in the y directionpublic void moveSketchTo(double x, double y)
x
, y
). The origin is the point
on the sketch where the turtle started out.
x
- x coordinate to move the origin toy
- y coordinate to move the origin topublic void moveSketchTo(Location point)
Location
. The origin is the point
on the sketch where the turtle started out.
point
- Location
to move the origin topublic void moveTo(double x, double y)
moveTo
in interface DrawableInterface
moveTo
in class objectdraw.ObjectDrawShape
x
- x coordinate to move toy
- y coordinate to move topublic void moveTo(Location point)
moveTo
in interface DrawableInterface
moveTo
in class objectdraw.ObjectDrawShape
point
- Location
to move topublic void penDown()
public void penUp()
public void recolorSketch(Color original, Color substitute)
original
with
the new color substitute
.
original
- Color
with which a portion of the sketch was originally drawnsubstitute
- Color
to replace that portion of the sketch withpublic void removeFromCanvas()
removeFromCanvas
in interface DrawableInterface
removeFromCanvas
in class objectdraw.ObjectDrawShape
public void rotateSketch(double byAngle)
byAngle
- angle to rotate by. Positive rotates left, negative rotates right.public void rt()
public void rt(double angle)
angle
degrees to the right
angle
- number of degrees to turnpublic void scaleSketch(double byFactor)
byFactor
- factor by which the sketch should be scaledpublic void sendBackward()
sendBackward
in interface DrawableInterface
sendBackward
in class objectdraw.ObjectDrawShape
public void sendForward()
sendForward
in interface DrawableInterface
sendForward
in class objectdraw.ObjectDrawShape
public void sendToBack()
sendToBack
in interface DrawableInterface
sendToBack
in class objectdraw.ObjectDrawShape
public void sendToFront()
sendToFront
in interface DrawableInterface
sendToFront
in class objectdraw.ObjectDrawShape
public void setColor(Color c)
setColor
in interface DrawableInterface
setColor
in class objectdraw.ObjectDrawShape
c
- new Color
for this objectpublic void setHeading(double heading)
heading
- the degree angle toward which you want the turtle to facepublic void setLineWidth(double width)
setLineWidth
in interface DrawableStrokeInterface
width
- the new width of this object's linespublic void setSketchRotation(double angle)
angle
- angle of rotation of the sketchpublic void setSketchScale(double factor)
factor
- factor to which the sketch should be scaledpublic void setStroke(BasicStroke stroke)
setStroke
in interface DrawableStrokeInterface
stroke
- the new stroke for this object's linespublic void show()
show
in interface DrawableInterface
show
in class objectdraw.ObjectDrawShape
public void showTurtle()
public String toString()
toString
in class objectdraw.ObjectDrawObject