BFOIT - Introduction to Computer Programming

Java Turtle Graphics

Polish Translation provided by Valeria Aleksandrova.
http://www.pkwteile.de/wissen/java-zolw-graficzne(Java Zólw graficzne)


Introduction

In this lesson you will play with a TurtleGraphicsWindow object.  It has methods that correspond to graphics procedures in the Logo programming language.  This will help you see and compare programs that you started out writing in Logo with similar programs written in Java.

Turtle Graphics in a Java Program

Like the approach I took in Extending Existing Classes, I've written a class (TurtleGraphicsWindow) which sits on top of TG's graphics support code.  You can use this class to do turtle graphics in Java.  You will extend TurtleGraphicsWindow.  When you extend it, you inherit all of the methods it provides.  It gets you all of the graphics-oriented commands and operators you've been using in Logo, along with a bit of its user interface stuff.

  *** Note ***
  This does not get you access to the full Logo language, only the
  graphics stuff and the mouse and keyboard event stuff.  If you
  look at  Appendix C, jLogo Primitives, you will see that there is
  a lot more to Logo than its Graphics Procedures.  

To use TurtleGraphicsWindow, you will need its Java source code and the TG source code files that it sits on top of.  You must install them on your computer and compile them.  I've put all of the source code into a .jar file and into a .zip file and placed them on a website's ftp directory.  I've documented how to get stuff from this site in Appendix H (Installation of TG).

Cross.java

To get you started, here is a very simple program to draw an X axis and Y axis.  Copy it and paste it into a file named Cross.java, then compile and run it.


  class Cross extends TurtleGraphicsWindow
  {
      public void myTurtleCmds( )
      {
          forward( 100 );
          back( 200 );
          forward( 100 );
          right( 90 );
          forward( 100 );
          back( 200 );
      }

      // program starts here
      public static void main ( String[ ] args )  
      {
          Cross obj = new Cross( );
          obj.myTurtleCmds( );
      }
  } // end class Cross
                        

Additional Example Programs

Moving on, included in the TGW_Src.jar and TurtleGraphics.zip files are a few example programs, named TGWexample1, TGWexample2, ... TGWexample7. Each demonstrates some turtle graphics functionality. You should look at the source code files, find the graphics methods and see how they are used. Here is the JavaDoc documentation of the methods that are available.

The first three example programs are shown in the Java TurtleGraphics Update, dated February 13, 2014.

The last four example programs are shown in the Java TurtleGraphics Update, dated January 29, 2016.

Compile the programs and run them.

Summary


Back to Types of Arguments
Go to the Table of Contents
On to Control Flow

Public Domain Mark
This work (BFOIT: Introduction to Computer Programming, by Guy M. Haas),
identified by Berkeley Foundation for Opportunities in IT (BFOIT),
is free of known copyright restrictions.