This page is optimized for mobile devices, if you would prefer the desktop version just click here

3.2 Java3002: creating and manipulating turtles and pictures in a  (Page 10/12)

Then the code in Listing 6 calls the getter method named getPicture to gin access to the Picture object encapsulated in the World object.

Call the addMessage method

Having gained access to the Picture object, Listing 6 calls the addMessage method on that object passing my name as a String object along with a pair of coordinate values that specify a location near the upper-left corner of the image. The result is that my name appears in the world as shown in Figure 1 .

Methods encapsulated in the Turtle object

The Turtle class extends the SimpleTurtle class, which in turn extends the Object class. Therefore, an object of the Turtle class encapsulates all of the methods defined in all three classes.

Manipulate the turtle referred to by the variable named joe

A Turtle object encapsulates many methods that can be used to manipulate the turtle in a variety of different ways. This is illustrated by the series of statements in Listing 7 .

Listing 7 . Manipulate the turtle named joe.
joe.setName("joe");joe.setBodyColor(Color.RED); joe.setShellColor(Color.BLUE);joe.setPenColor(Color.YELLOW);joe.setPenWidth(3);joe.forward();joe.turn(-135); joe.setPenColor(Color.BLUE);joe.forward(150);

Initial (default) state of a Turtle object

When a new Turtle object is instantiated and added to a World object (using the constructor shown in Listing 2 ) , it doesn't have a name property.

The turtle initially appears in the center of the world, facing northwith a default color.

Every Turtle object has a pen attached to its belly that can draw a line with a default width of one pixel in a default color when the turtle moves.

The pen can be raised so that it won't draw a line or lowered so that it will draw a line. Initially it is down and will draw a line by default.

Set the name property to "joe"

Listing 7 begins by setting the name property of one of the turtles to the string value "joe."

Note that this is completely independent of the fact that a reference to this turtle is stored in a variable named joe . The name property could have been set to "Tom", "Dick", "Harry", or any other string value. Itis the value of the name property and not the name of the variable that determines the text output shown in Figure 2 .

Set the turtle's body and shell color

Listing 7 continues by calling two setter methods on the turtle object to set the body color (head and feet) to red and the color of the shell to blue. You can see the effect of this in Figure 1 .

Set the pen color and width

Then Listing 7 calls two setter methods that set the turtle's pen color to yellow and the pen width to three pixels. You can alsosee the result of this in Figure 1 .

Make the turtle move forward

After that, Listing 7 calls the forward method (with no parameters) to cause the turtle to move forward by a default distance of 100 pixels.

Recall that the turtle initially faces north. In this case, the forward method causes the turtle to move from the center of the world to alocation that is 100 pixels due north of the center of the world, drawing a wide yellow line along the way.

<< Chapter < Page Page > Chapter >>

Read also:

OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.