Sample Turtle Programs



Similar documents
Week 2 Practical Objects and Turtles

Interactive Programs and Graphics in Java

CS170 Lab 11 Abstract Data Types & Objects

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

To Begin Customize Office

Part 1 Foundations of object orientation

Introduction to Object-Oriented Programming

How To Program In Java (Ipt) With A Bean And An Animated Object In A Powerpoint (For A Powerbook)

The Darwin Game 2.0 Programming Guide

A sample script file is displayed below: DRF271.SCR snap.125 grid.5 layer make vports color blue vports. zoom all

Presents. AccuDraw. Instructor Pam Roberts

Programming with the Dev C++ IDE

HOW TO VIEW AND EDIT PICTURES

ADDING and/or DELETING PIN NUMBERS (Plus other simple programming commands) in My DK-16 or DK-26 DIGITAL KEYPAD

Python for Rookies. Example Examination Paper

Electric Landing Gear controllers and sequencer LGC12 / LGC 13C

Home Phone Call Forward Guide

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

Image Processing. In this chapter: ImageObserver ColorModel ImageProducer ImageConsumer ImageFilter

Creating and Using Links and Bookmarks in PDF Documents

Instant Interactive SAS Log Window Analyzer

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

CS1002: COMPUTER SCIENCE OO MODELLING & DESIGN: WEEK 5

LAYOUT MANAGERS. Layout Managers Page 1. java.lang.object. java.awt.component. java.awt.container. java.awt.window. java.awt.panel

Xylophone. What You ll Build

Inside the Java Virtual Machine

Using Your Polyvision Digital Whiteboard and Walk-and-Talk

There are 2 approaches to starting a session: the mutation scope can contain a whole codebase or in a single chosen method.

Lesson 4. Temporal Management of Layers

Tutorial Reference Manual. Java WireFusion 4.1

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

How To Write A Program For The Web In Java (Java)

Lesson 7 - Creating Animation II

Omatics User s Guide

DCAD Website Instruction Manual

Adobe Illustrator CS5 Part 1: Introduction to Illustrator

Adobe InDesign Creative Cloud

First Steps with CoDeSys. Last update:

10 Listing data and basic command syntax

SellerDeck 2014 Responsive Design Guide

The following is an overview of lessons included in the tutorial.

Graphics Module Reference

Search and Replace in SAS Data Sets thru GUI

6. Control Structures

PowerPoint: Graphics and SmartArt

Employee Time Clock Training elearning Course Notes

Part 3: GridWorld Classes and Interfaces

Configuration app and Bluetooth interface

AP Computer Science Java Mr. Clausen Program 9A, 9B

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Introducing the hard disc recorder and Beo4, Daily use, Advanced operation, Preference settings, Set up the hard disc recorder, On-screen menus,

Section 8 Scheduler. Alcatel-Lucent OmniVista 4760 Network Management System

Programming with Java GUI components

Adobe Acrobat Professional DC Tutorial

Bitrix Site Manager 4.1. User Guide

Debugging Java Applications

Chapter 9: Building Bigger Programs

Colored Hats and Logic Puzzles

NIS-Elements Viewer. User's Guide

Source Code Translation

MULTIMEDIA INSTALLING THE MULTIMEDIA UPGRADE

Lesson 10: Video-Out Interface

Mobile Money Manager

Dell SonicWALL SRA 7.5 Secure Virtual Meeting and Secure Virtual Assist

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Select the Crow s Foot entity relationship diagram (ERD) option. Create the entities and define their components.

Goal: Practice writing pseudocode and understand how pseudocode translates to real code.

vcenter Orchestrator Developer's Guide

Using Microsoft Project 2000

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

cs281: Introduction to Computer Systems Lab08 Interrupt Handling and Stepper Motor Controller

OneDrive for Business User Guide

Licensed for viewing only. Printing is prohibited. For hard copies, please purchase from

Java Program Coding Standards Programming for Information Technology

University of Arkansas Libraries ArcGIS Desktop Tutorial. Section 2: Manipulating Display Parameters in ArcMap. Symbolizing Features and Rasters:

1. a procedure that you perform frequently. 2. Create a command. 3. Create a new. 4. Create custom for Excel.

3D Input Format Requirements for DLP Projectors using the new DDP4421/DDP4422 System Controller ASIC. Version 1.3, March 2 nd 2012

Creating a Project with PSoC Designer

Welcome to CSU The Software Used To Data Conference.

LOCAL FLEET TRACKING. GPS Fleet Tracking Help Guide

3D Animation of Java Program Execution for Teaching Object Oriented Concepts

14 Model Validation and Verification

AEI RAIL & ROAD MANAGER

13 Classes & Objects with Constructors/Destructors

Installing Java. Table of contents

Lou Burnard Consulting

This loop prints out the numbers from 1 through 10 on separate lines. How does it work? Output:

LAB4 Making Classes and Objects

3 - Lift with Monitors

Basketball Playbook Manual

Access Tutorial 3 Maintaining and Querying a Database. Microsoft Office 2013 Enhanced

Tutorial 3 Maintaining and Querying a Database

Moving from CS 61A Scheme to CS 61B Java

Building Robots with NXT and LEJOS. Introduc<on. What is the NXT Robot Michael Wooldridge liv.ac.uk)

STARTING WEBSTER...3 GETTING STARTED WITH WEBSTER TOOLS...5

Microsoft Word Quick Reference Guide. Union Institute & University

Transcription:

Sample Turtle Programs Sample 1: Drawing a square This program draws a square. Default values are used for the turtle s pen color, pen width, body color, etc. import galapagos.*; /** * This sample program shows the most basic way * of using a Turtle. */ public class TurtleSample1 public static void main( String args[] ) Turtle myturtle; SampleTurtle1.java int size, turn; myturtle = new Turtle( ); size = 100; //logical units turn = 90; //in degree //draw a square

Sample 2: Changing the Turtle s Properties This program also draws a square, but this time several of the turtle s properties are changed. import galapagos.*; import java.awt.*; //for using Color /** * This sample program shows several different of changing * the turtle's properties such as pen color, pen size, * body color, etc. */ public class TurtleSample2 SampleTurtle2.java public static void main( String args[] ) Turtle myturtle; int size, turn; myturtle = new Turtle( ); size = 100; //logical units turn = 90; //in degree

//set some properties myturtle.bodycolor( Color.black ); //body color is black myturtle.jumpto( 50, 0 ); //set the starting position to (50,0) //jumping to it //draw a square myturtle.pensize( 5 ); //draw the bottom side myturtle.pencolor( Color.green ); //in green 5 units wide myturtle.penup( ); //just move along the right side //without drawing by placing the //pen in the up position myturtle.pendown( ); //draw again by putting the myturtle.pensize( 3 ); //pen down; set pen size to 3 myturtle.pencolor( Color.blue ); //and color to blue myturtle.speed( 5 ); myturtle.pensize( 7 ); myturtle.pencolor( Color.red ); //draw the final side in one-fourth //the default speed (which is 20) with //pen size 7 and color red

Sample 3: Using TurtleDrawingWindow In the previous two sample programs, the turtles used a default drawing window they created internally. This approach is adequate for a simple drawing, but not for others. In this sample program, the programmer explicitly creates a TurtleDrawingWindow, a window which the turtle will draw. Notice that the constructor used for the Turtle in this class is different from the previous programs. You must use this second constructor if you want to make the Turtle draw on the TurtleDrawingWindow you create. Errata: The sample source code on page 190 exercise 4.16 is wrong because the way turtles are created in the sample code is invalid. import galapagos.*; import java.awt.*; //for using Color SampleTurtle3.java /** * This sample program shows how to use a TurtleDrawingWindow * explicitly. If TurtleDrawingWindow is not used, then a * Turtle will create one internally. If you want to set * the parameters such as moving the location of logical * origin point to places other than the center of window, * changing the window scale (zoom in or zoom out view), etc. * Also, you must use a TurtleDrawingWindow if you want * use multiple Turtle objects on a single window. * */ public class TurtleSample3 public static void main( String args[] ) Turtle myturtle; TurtleDrawingWindow playground; int size, turn; playground = new TurtleDrawingWindow( ); //**************** IMPORTANT ***************// //You must pass an int argument so the turtle //will not create a default drawing window. You must //create a turtle in this manner if you want to //add a turtle to the drawing window you create within //your program. Sample code for exercise 4.16 on page //190 is therefore wrong. Sorry. myturtle = new Turtle( Turtle.NO_DEFAULT_WINDOW );

//You must connect the turtle to a TurtleDrawingWindow //by 'adding' it to the window. playground.add( myturtle ); //******************************************// //Set some properties for the playground. //Experiment with different values. playground.setunit( 2.5 ); //one logical unit is equal to 3 pixels //default is 1, i.e. one unit equlas one pixel playground.setorigin( 75, 50 ); //logical point (100,100) is at the //center of the window playground.setgrid( true ); //Displays the grid line, which is a default //change the argument to false and see //what happens playground.setvisible( true ); //Don't forget to make the window visible. //You can make the //window visible first and then set properties //using setunit, setorigin, etc., but in this //case you may see the display in the original //property and suddenly change to the properties //you set. //Now draw a square size = 100; //logical units turn = 90; //in degree //draw a square

Sample Program 4: Using Multiple Turtles This program creates two turtles and let them draw a square simultaneously. import galapagos.*; import java.awt.*; //for using Color SampleTurtle4.java /** * This sample program shows how to use a TurtleDrawingWindow * explicitly with two turtles. * */ public class TurtleSample4 public static void main( String args[] ) Turtle betsy, emily; TurtleDrawingWindow playground; int size, turn; playground = new TurtleDrawingWindow( ); playground.setvisible( true );

//Create two turtles and add them to the playground betsy = new Turtle( Turtle.NO_DEFAULT_WINDOW ); emily = new Turtle( Turtle.NO_DEFAULT_WINDOW ); playground.add( betsy ); playground.add( emily ); //Don't show the turtle bodies betsy.hide( ); //if you want to show turtle bodies emily.hide( ); //then set their body color to distinguish //them. Use the bodycolor method. // betsy.bodycolor( Color.blue ); // emily.bodycolor( Color.yellow ); //Set pen colors betsy.pencolor( Color.blue ); emily.pencolor( Color.yellow ); //Now draw a square size = 100; //logical units turn = 90; //in degree //betsy draw counter clockwise square betsy.pause( ); betsy.move( size ); betsy.turn( turn ); betsy.move( size ); betsy.turn( turn ); betsy.move( size ); betsy.turn( turn ); betsy.move( size ); betsy.turn( turn ); //emily draws clockwise square of the same size emily.pause( ); emily.move( -size ); //If you make emily visible, you will emily.turn( -turn ); //notice that emily is moving backward //If you want emily to face forward emily.move( -size ); //then you must change emily's heading emily.turn( -turn ); //to 180 deg first and make both //size and turn positive, exactly like emily.move( -size ); //betsy's code emily.turn( -turn ); emily.move( -size ); emily.turn( -turn ); //now start both turtles together

betsy.start( ); emily.start( ); //NOTE: // Theoretically, the turtles move in their own thread, so if you don't // pause them, they would move immediately after receiving the first command. // This makes betsy start moving while emily is still waiting for the commands // to arrive. // In reality you will not see the delay even if you don't put the pause and // and start commands, because the sequence of commands in this program // is very short. The time to set up the drawing window will take much // longer time and executing the statements in this program. Sample Program 5: In this program, the programmer uses the javabook class InputBox along with the Turtle. Since TurtleDrawingWindow is a Frame object, it can serve as an owning window for the InputBox class (and other javabook dialogs).

import galapagos.*; import java.awt.*; //for using Color import javabook.*; SampleTurtle5.java /** * This sample program shows how to use a TurtleDrawingWindow * and the InputBox class from the javabook pacakge. */ public class TurtleSample5 public static void main( String args[] ) Turtle myturtle; TurtleDrawingWindow playground; InputBox inputbox; int size, turn; playground = new TurtleDrawingWindow( ); inputbox = new InputBox( playground ); myturtle = new Turtle( Turtle.NO_DEFAULT_WINDOW ); myturtle.bodycolor( Color.magenta ); playground.add( myturtle ); playground.setvisible( true ); //Get input size = inputbox.getinteger( "Enter the size of a square:"); turn = 90; //draw a square