turtle_graphics
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
turtle_graphics [2022/08/27 11:59] – frchris | turtle_graphics [2024/09/12 14:27] (current) – [Make Your own Picture] frchris | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Turtle Graphics ====== | ====== Turtle Graphics ====== | ||
- | {{ :: | ||
===== History ===== | ===== 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 | + | 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. |
{{:: | {{:: | ||
Line 12: | Line 11: | ||
{{:: | {{:: | ||
+ | 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. | ||
+ | [[https:// | ||
+ | ===== The CSAwesome Turtle ===== | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | Here is the class diagram of '' | ||
+ | {{:: | ||
+ | |||
+ | |||
+ | The class Main has the main method | ||
+ | <code java> | ||
+ | public class Main { | ||
+ | public static void main(String[] args) { | ||
+ | World world = new World(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 " | ||
+ | An animated version from Berne, Switzerland has so many classes, they put them all in a Library. | ||
+ | |||
+ | The [[http:// | ||
+ | |||
+ | 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): | ||
+ | |||
+ | {{ :: | ||
+ | |||
+ | ==== Make Your own Picture==== | ||
+ | Here is an example of a Turtle instance that can make a hexagon, fill it in with a color, and " | ||
+ | |||
+ | <code java> | ||
+ | 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, | ||
+ | |||
+ | for (int i=0; i< 6; i++) { | ||
+ | hexter.forward(100); | ||
+ | hexter.right(-60); | ||
+ | } | ||
+ | | ||
+ | hexter.setPos(0, | ||
+ | hexter.setFillColor(Color.BLUE); | ||
+ | hexter.fill(); | ||
+ | | ||
+ | Turtle t = new Turtle(hexter); | ||
+ | t.setPenColor(Color.RED); | ||
+ | t.setPos(-200, | ||
+ | t.hideTurtle(); | ||
+ | t.label( "by My name" | ||
+ | | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | Here is the [[http:// | ||
+ | |||
+ | 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: | ||
+ | Heading: north | ||
+ | Speed: 200 (coordinates per seconds) | ||
+ | |||
+ | Lab Scoring: | ||
+ | - Adds the [[https:// | ||
+ | - 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: | ||
+ | |||
+ | <code java> | ||
+ | import ch.aplu.turtle.*; | ||
+ | import java.awt.Color; | ||
+ | |||
+ | public class Example | ||
+ | { | ||
+ | public static void main (String[] args) { | ||
+ | Turtle joe = new Turtle(Color.green); | ||
+ | //own window. | ||
+ | joe.setPos(-100, | ||
+ | for (int i=0; i < 4; i++) { | ||
+ | joe.right(90); | ||
+ | joe.forward(200); | ||
+ | // move forward 200 pixels. | ||
+ | } | ||
+ | joe.setPenColor(Color.red); | ||
+ | joe.penUp(); | ||
+ | joe.back(50); | ||
+ | joe.left(90); | ||
+ | joe.back(50); | ||
+ | joe.right(90); | ||
+ | joe.penDown(); | ||
+ | for (int i=0; i < 4; i++) { | ||
+ | joe.right(90); | ||
+ | joe.forward(100); | ||
+ | } | ||
+ | Turtle anne = new Turtle(joe); | ||
+ | // in the same window as joe | ||
+ | anne.speed(1000);// | ||
+ | anne.forward(150); | ||
+ | anne.left(90); | ||
+ | // then do some moves. | ||
+ | anne.hideTurtle();// | ||
+ | anne.forward(150); | ||
+ | anne.left(90); | ||
+ | anne.stampTurtle(); | ||
+ | anne.forward(300); | ||
+ | anne.showTurtle(); | ||
+ | // screen | ||
+ | anne.wrap(); | ||
+ | anne.setPos(200, | ||
+ | for (int i=0; i < 4; i++) { | ||
+ | anne.right(90); | ||
+ | anne.forward(400); | ||
+ | } | ||
+ | anne.reinit(); | ||
+ | // e.g. home position, | ||
+ | Turtle filly = new Turtle(joe, Color.yellow); | ||
+ | // Playground. | ||
+ | filly.setPos(75, | ||
+ | filly.setFillColor(Color.black); | ||
+ | filly.fill(); | ||
+ | // non-background colored pixel, | ||
+ | // containing filly' | ||
+ | filly.setPos(175, | ||
+ | filly.setFillColor(Color.orange); | ||
+ | filly.fill(); | ||
+ | |||
+ | Turtle texter = new Turtle(joe, Color.magenta); | ||
+ | texter.hideTurtle().label(" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | [[https:// | ||
turtle_graphics.1661615951.txt.gz · Last modified: 2022/08/27 11:59 by frchris