Table of Contents
Turtle Graphics
History
In the 1960s, an educational programming language called Logo was developed. It is best known for teaching programming with turtles! The turtles were graphical or robotic turtles that were controlled with simple commands like go forward, turn, raise and lower a pen. Moving the turtle with the pen down would draw a picture. Here’s a photo of a robot turtle from the 1960s.
The turtle had a pen attached to it. The student-programmers would steer the robot around using simple commands to create drawings with their code.
This was later used (in a simulated state) to teach newer programming languages as they get invented. Here we have two Java incarnations of Turtle Graphics.
The CSAwesome Turtle
TurtleJavaSwingCode.zip has the Sources needed to make turtles with your own code on your own machine. See Section 2.1 of the Runestone Academy Book CSAwsome.
Here is the class diagram of SimpleTurtle
the superclass of Turtle
. any subclass of SimpleTurtle
has these attributes and methods:
The class Main has the main method
public class Main { public static void main(String[] args) { World world = new World(400,400); Turtle yertle = new Turtle(world); // Add your own code in here world.setVisible(true); yertle.setColor(Color.blue); yertle.forward(); yertle.turnRight(); yertle.forward(); } }
Java "Animated" APLU Turtle
An animated version from Berne, Switzerland has so many classes, they put them all in a Library. This library is in a file called jturtle-0.1.1.jar. Click here for instructions on how to install it in BlueJ, and click here for instructions to install a .jar library in Eclipse
The APLU.ch Java Utility Package ch.aplu.turtle Description
The Java Turtle Package provides functionality for LOGO-like Java-Programs (including multiple turtles). It is written for educational purposes by Regula Hoefer-Isenegger for the AHL at the University of Berne under the supervision of Prof. Dr. Aegidius Plüss. The Java Turtle Package comes under the GNU GENERAL PUBLIC LICENSE, Version 2, June 1991 (which you can find in the COPYING file which comes along with this package) and is copyrighted by the author.
You will need download this jar file and add it to your Libraries (Java Build Path in Eclipse, Library Tab in Preferences in BlueJ):
jturtle-0.1.1.jar Library of classes to draw with a turtle. You can get the latest version 21st Century version here
Make Your own Picture
Here is an example of a Turtle instance that can make a hexagon, fill it in with a color, and “autographs” it:
import ch.aplu.turtle.*; import java.awt.Color; /** * * @author (Your Name) * @version September 0, 2024 */ public class MyPicture { public static void main(String[] args) { Turtle hexter = new Turtle(Color.GREEN); hexter.setPos(100,-50); for (int i=0; i< 6; i++) { hexter.forward(100); hexter.right(-60); } hexter.setPos(0,0); hexter.setFillColor(Color.BLUE); hexter.fill(); Turtle t = new Turtle(hexter); t.setPenColor(Color.RED); t.setPos(-200, 180); t.hideTurtle(); t.label( "by My name"); } }
Here is the Turtle API (documentation in the Javadoc style)
The default turtle coordinate system: doubles -200.0..+200.0 in both directions (zero at center; x to left, y upward). (Turtle coordinates are rounded to to 400 x 400 pixel coordinates). Unless special constructors are used, new turtles are shown in a new window.
Defaults when a turtle is created: Coordinates: (0, 0) (center of window) Heading: north Speed: 200 (coordinates per seconds)
Lab Scoring:
- Adds the jturtle-0.1.1.jar library in either BlueJ or Eclipse
- Your name as @author in a comment
- The date as @version in a comment
- Makes a Turtle that is visable
- Draws a shape
- Fills the shape with a color
- Autographs their work with a label
- (8th point: Has more than one shape with an additional Turtle instance
- (9th or 10th point: Has unique and creative work of art
Here's an example program which demonstrates how to use some of the fancy methods Turtle class from the APLU:
import ch.aplu.turtle.*; import java.awt.Color; public class Example { public static void main (String[] args) { Turtle joe = new Turtle(Color.green); // Create a green turtle in her //own window. joe.setPos(-100,100); // Place joe to the Point(-100,100). for (int i=0; i < 4; i++) { joe.right(90); joe.forward(200); // turn 90 degrees to the right, then // move forward 200 pixels. } joe.setPenColor(Color.red); // set the pen color to red. joe.penUp(); // lifts the pen off the canvas pu() - pen up joe.back(50); joe.left(90); joe.back(50); joe.right(90); joe.penDown(); // back 50, left 90.... pen down for (int i=0; i < 4; i++) { joe.right(90); joe.forward(100); } Turtle anne = new Turtle(joe); // Create a new Turtle (named anne) // in the same window as joe anne.speed(1000);// sets the speed to 1000 pixels/sec, anne.forward(150); anne.left(90); // then do some moves. anne.hideTurtle();// ht(): hide the turtle anne.forward(150); anne.left(90); anne.stampTurtle(); anne.forward(300); anne.showTurtle(); // lets the turtle reappear on the st(): show turtle // screen anne.wrap(); // Tells anne to wrap around the edges anne.setPos(200,200); for (int i=0; i < 4; i++) { anne.right(90); anne.forward(400); } anne.reinit(); // Resets anne to her standard settings, // e.g. home position,facing north Turtle filly = new Turtle(joe, Color.yellow); // yellow Turtle in joe's // Playground. filly.setPos(75,75); filly.setFillColor(Color.black); // Sets the fill color to black. filly.fill(); // fills the region bounded by any // non-background colored pixel, // containing filly's position. filly.setPos(175,175); filly.setFillColor(Color.orange); filly.fill(); Turtle texter = new Turtle(joe, Color.magenta); texter.hideTurtle().label("Hello Turtle"); } }