Using Turtles in Eclipse

Download Report

Transcript Using Turtles in Eclipse

Turtles
The beginning of media
computation.
Copyright © 1997-2013 Curt Hill
What is a turtle?
• It will show up as a icon in a window
• We direct the turtle to move forward
or backward as well as turn left or
right
– Among other things
• When it moves it may leave a line as
a trail
• It is a line drawing tool
Copyright © 1997-2013 Curt Hill
A Turtle in a World
Copyright © 1997-2013 Curt Hill
Two Objects
• There are two objects that are
needed for this
– A world
– A turtle
• The world provides the window and
background
– The sandbox for one or more turtles
• The turtle
– Our drawing object
Copyright © 1997-2013 Curt Hill
What is an object?
• In Java there are two kinds of
variables
– Primitives and objects
• A primitive represents one simple
value
– An int containing 4
– The character ‘A’
• An object bundles one or more
values into one
Copyright © 1997-2013 Curt Hill
Objects
• Objects have properties and
methods
• A property is a variable that may be
a variable or another object
• A method is a means to make the
object do something
– Obtain or change property values
– A constructor is a kind of method that
initializes the object
Copyright © 1997-2013 Curt Hill
Primitive Life Cycle
• We declare
• We assign values to them
• They are only known in the method
that they are used within
– Such as the main method
• Objects have a slightly different life
cycle
Copyright © 1997-2013 Curt Hill
Object Life Cycle
• Declare the handle
• Allocate (or instantiate) using new
or assignment
– Declaration makes primitives exist but
objects need another step
• Assign and use the values
Copyright © 1997-2013 Curt Hill
Terminology
• An object is a kind of variable
• A class is a type of object
• Example:
String s = “S”;
String is the class
s is the object
“S” is the value
Copyright © 1997-2013 Curt Hill
Declaration
• Objects are declared just like
primitives
Class_Name Variable_name ;
• Example:
Turtle george;
• george is now an object handle
• Cannot be used until allocated
• An import statement may be needed
to bring the class name within the
scope
Copyright © 1997-2013 Curt Hill
Two ways to initialize
• Assign from an existing object
Turtle george = sam;
where sam is an existing turtle
• Use the new keyword
Turtle bob=new Turtle(myWorld);
The Turtle(myWorld) part is a
constructor
myWorld is the world that the turtle will
be in
Copyright © 1997-2013 Curt Hill
Constructors
• Recall that a constructor is a special
method of a class
• It has the same name as the class
• It is often overloaded by different
sets of parameters
• One of the constructors is always
executed for a newly created object
• It initializes the new object
Copyright © 1997-2013 Curt Hill
Preparation
• Eclipse knows about the Java
Runtime Environment (JRE)
• This includes all Java standard
objects such as Scanner
• It does not know about Turtles
because they are not standard Java
items
• Eclipse will allow new items to be
added to a single project or to the
JRE
– It is easier to just modify the JRE
Copyright © 1997-2013 Curt Hill
JRE Modification
• Download TurtleGraphics.jar from class
web site
– Remember where you put it
• Select Window and then Preferences
• Choose preferences
• Open Java and Installed JREs in
preferences
• Edit the JRE
• Click Add External JARs
• Find the downloaded TurtleGraphics.jar
Copyright © 1997-2013 Curt Hill
Lab Computers
• Some of you may be running Eclipse
in a computer lab
• Sometimes these are periodically
cleaned
– Every night
– During a reboot
• In such cases you should put the
TurtleGraphics.jar file on your
space:
– Flash drive
– Your space on another drive
Copyright © 1997-2013 Curt Hill
Downloading
Copyright © 1997-2013 Curt Hill
Window Preferences
Copyright © 1997-2013 Curt Hill
Preferences
Copyright © 1997-2013 Curt Hill
Open Java in Preferences
Copyright © 1997-2013 Curt Hill
Choose Installed JREs
Copyright © 1997-2013 Curt Hill
Select and Edit
Copyright © 1997-2013 Curt Hill
After Edit Button
Copyright © 1997-2013 Curt Hill
Add External Jar
Copyright © 1997-2013 Curt Hill
Finishing up
• Once TurtleGraphics.jar has been
selected select the OK or Finish
button until back at the main Eclipse
window
• This modifies the JRE and we should
not need to do it a second time
• One more thing may be needed, the
forbidden access error needs to be
disabled
Copyright © 1997-2013 Curt Hill
Java Errors
Copyright © 1997-2013 Curt Hill
Import Statement
• We have told Eclipse that we need
TurtleGraphics to be part of the JRE
• There are a large number of things
in the JRE that require an import to
be used
• The import for this is:
import turtlegraphics.*;
• This follows the package statement
Copyright © 1997-2013 Curt Hill
Creating a World
• Each World will become a screen
• Turtles must be created within a
world
• Create the world:
World myWorld = new World();
– World is class name
– myWorld is variable name and could be
any name, but will be used with Turtle
constructor
– World() is the constructor
Copyright © 1997-2013 Curt Hill
Creating Turtles
• Once myWorld has been made we can
make turtles
• Create a turtle:
Turtle sam = new Turtle(myWorld);
– Turtle is a class name
– sam is the name of one turtle
– myWorld is the name of the previously created
world
• We may create as many as we like
Copyright © 1997-2013 Curt Hill
Turtle Methods
• Making a turtle to exist is no big deal
– We would like the turtle to do
something
– This takes methods
• For now just two
• forward(200);
– Moves forward 200 pixels
• turn(90)
– Turn right 90 degrees
– Left uses a negative
Copyright © 1997-2013 Curt Hill
Eclipse Again
• One of the very many advantages of
Eclipse over DrJava is the code
completion option
• When we type in the name of a
variable, that is a class, and then a
dot
– Eclipse suggests some methods that
could be used
• See the following screen:
Copyright © 1997-2013 Curt Hill
Code Completion
Copyright © 1997-2013 Curt Hill
Forward chosen
Copyright © 1997-2013 Curt Hill
The new code
Copyright © 1997-2013 Curt Hill
Fill in the value
Copyright © 1997-2013 Curt Hill
Getting Started
•
•
•
•
Create a new project
Create a new class
Add import
Add five lines of code within main
Copyright © 1997-2013 Curt Hill
Whole but Short Program
package turtle;
import turtlegraphics.*;
public class FirstTurtle {
public static void main(String[] args)
{
World w = new World();
Turtle t = new Turtle(w);
t.forward(100);
t.turn(90);
t.forward(100);
}
}
Copyright © 1997-2013 Curt Hill
Run It
• After the program is complete run it
• A image similar to the next screen
should occur
Copyright © 1997-2013 Curt Hill
The Run
Copyright © 1997-2013 Curt Hill
One last thought
• Do not name your project or class or
package the same thing as any class
you will use
• In particular do not use:
–
–
–
–
–
Turtle
World
Color
Picture
Among others
• If you do you may not be able to use
any of these objects
Copyright © 1997-2013 Curt Hill
Try It
• Now we have a glorified etch-asketch
– Soon we will have much more
• Create a new program
• Put in the commands to draw a
square
– 4 forwards
– 4 turns of 90
• I will and you can follow me in this
Copyright © 1997-2013 Curt Hill