<< Chapter < Page Chapter >> Page >

Turn counter clockwise

Then Listing 7 calls the turn method causing the turtle to rotate its body by 135 degrees counter-clockwise. (A positive parameter causes a clockwise rotation and a negative parameter causes a counter clockwise rotation.)

Change the pen color and move forward again

Finally Listing 7 calls methods to change the pen color to blue and to cause the turtle to move forward by 150 pixels.

The final location

After making the turn, the turtle is facing southwest. Therefore, the forward movement causes a diagonal blue line to be drawn from theposition at the top of the yellow line down toward the southwest. As you can see in Figure 1 , the turtle comes to rest at the end of that line.

A few words about color

I have published extensively on the concept of color in Java. The best way to find that information is probably to go to Google and search for the keywords:

richard baldwin "color class" site:http://cnx.org/contents/

Google is also probably your best bet for finding information on other topics that I have published on various websites. For example, if you go to Google Images and search for the following keywords, you will find a lot of the work that I havepublished using Ericson's media library.

richard baldwin java ericson

Manipulate the turtle named sue

Listing 8 calls several methods on the object whose reference is stored in the variable named sue .

Listing 8 . Manipulate the turtle named sue.
sue.setName("sue");sue.setPenWidth(2); sue.setPenColor(Color.RED);sue.moveTo(183,170);sue.setPenDown(false);sue.moveTo(216,203); sue.setPenDown(true);sue.moveTo(250,237);}//end run method }//end class Prob01Runner

The end result

These method calls result in the turtle named sue facing north in the lower right corner of the window, having drawn the broken red line shown in Figure 1 in getting there.

The moveTo method

Listing 8 calls the moveTo method to cause the turtle to move to a new location on the basis of coordinate values instead of on the basis of a distance value.

Pen control

Listing 8 also calls the setPenDown method twice passing false and then true as the parameter to first raise and then lower the pen. This produced the gap in the red line shown in Figure 1 .

The Turtle class also provides methods named penUp and penDown that can be used to raise and lower the pen.

The end of the program

Listing 8 also signals the end of the method named run and the end of the class named Prob01Runner . As such, Listing 8 signals the end of the program.

Run the program

II encourage you to copy the code from Listing 9 , compile it and execute it. Experiment with the code, making changes, and observing the results of your changes. Make certain that you can explain why your changes behave as they do.

Summary

II explained a program that uses Java and Ericson's media library to:

  • Add a picture and two turtles to a world.
  • Manipulate the turtles, their color, and their pens.

Stated in more detail, the program:

  • Creates a Picture object and replaces the default white Picture in a World object with the new Picture object.
  • Places two Turtle objects in a World object.
  • Applies a series of operations to manipulate the two turtle objects so as to produce a specific graphic output.
  • Provides accessor methods to get references to two Turtle objects and the World object.
  • Gets information from the World and Turtle objects and displays the information on the command-line screen.
  • Displays text on a Picture in a World object.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  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.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask