Background
jLogo Programming
- Commanding a Turtle
- Pseudocode
- Adding New Commands
- Iteration & Animation
- Hierarchical Structure
- Procedure Inputs
- Operators & Expressions
- Defining Operators
- Words & Sentences
- User Interface Events
- What If? (Predicates)
- Recursion
- Local Variables
- Global Variables
- Word/Sentence Iteration
- Mastermind Project
- Turtles As Actors
- Arrays
- File Input/Output
Java
- A Java Program
- What's a Class?
- Extending Existing Classes
- Types
- Turtle Graphics
- Control Flow
- User Interface Events
Appendices
- Jargon
- What Is TG?
- TG Directives
- jLogo Primitives
- TG Editor
- Java Tables
- Example Programs
- *** New ***:
Installation Notes
Updates
- December 13, 2008
- January 6, 2012
- March 15, 2013
- January 20, 2014
- February 13, 2014
- July 29, 2014
- January 18, 2016
- January 29, 2016
- August 19, 2016
Lastly
Java Turtle Graphics
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
Go to the Table of Contents
On to Control Flow