1 / 42

Introduction to Greenfoot

First look at Greenfoot and objects in Greenfoot James Brucker Revised July 2014. Introduction to Greenfoot. Software You Need. 2. Greenfoot graphical programming for Java http://se.cpe.ku.ac.th/download/greenfoot 1. Java Development Kit (JDK) compiles and runs Java programs

istas
Download Presentation

Introduction to Greenfoot

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. First look at Greenfoot and objects in Greenfoot James Brucker Revised July 2014 Introduction to Greenfoot

  2. Software You Need 2. Greenfoot graphical programming for Java http://se.cpe.ku.ac.th/download/greenfoot 1. Java Development Kit (JDK) compiles and runs Java programs includes Java Runtime + programmer tools http://java.oracle.com

  3. Install Greenfoot Download from... http://www.greenfoot.org http://se.cpe.ku.ac.th/download/greenfoot(at KU) Windows 1. double-click greenfootsetup-230.msi 2. Install in C:\greenfoot (recommended) Mac OS X Download .zip file. Unzip to install. Ubuntu Download package (.deb) and double click on it.

  4. Run Greenfoot The first time you may see a message like this: Select the Java you want and click "Launch". If no Java is found, then... click "Browse" and select your "java.exe" in the JDK "bin" directory. Example:c:\java\jdk1.7.0\bin\java.exe

  5. Lunar Lander Land on the moon without crashing. Press DOWN arrow key to fire thrust. Must land with velocity < 10 or you crash. You may need to compile the code before you can run it. Click Run to start.

  6. Add More Scenarios... Scenario can be a... Directory: c:\greenfoot\scenarios\lunarlander\ Archive File: c:\greenfoot\scenarios\BeatUp.gfar • Greenfoot will unpack it to directory.

  7. Greenfoot directories C:\Greenfoot \doc \lib \scenarios \ants \ku-crab \lunarlander \turtleGraphics \wombats greenfoot.exe README.txt Each scenario is a folder (directory) inside the scenarios folder. You can store a scenario anywhere on your computer, but each scenario must have its own folder (directory).

  8. Download Ants & turtleGraphics se.cpe.ku.ac.th/download/codecamp/scenarios Download ku-crab.zip turtleGraphics.zip

  9. Ants The Ants search for food and take it to their home. (Greenfoot can do simulation, not just games.) Click Run to start. http://www.greenfoot.org/scenarios/1016

  10. Biomedanoid Chicken by a student http://www.greenfoot.org/scenarios/338

  11. Turtle Graphics http://www.greenfoot.org/scenarios/3535 The turtles draw shapes or solve puzzles. Turtle has a pen. If the pen is down, it draws a path.

  12. What is a Program? A Program is a set of instructions for the computer. Ants Program in English Ants Program in Java move. if you find food, then... take some food go home move(); if ( findFood() ) { takeFood(); headHome(); } In Java, something( ); means todo something. takeFood( ) is a method to do something.

  13. A Program is a Collection of "things" In a program, objects represent "things". Rocket Turtle Ant Moon

  14. A Class defines a kind of thing (object) A class defines the properties and behavior of things. AntWorldclass defines an antWorld object. a Class AntHillclass defines anthills. Food class defines food objects. Ant class defines ant objects.

  15. Exploring "things" in TurtleGraphics Open TurtleGraphics What kinds of things are there? __________ __________ __________ __________ __________

  16. "things" have Behavior Behavior are the actions an object can do. Ants have behavior, Turtles have behavior. Ant Turtle act( ) checkFood( ) takeFood( food ) walk headHome( ) act( ) __________( ) __________( ) ________( __________ ) ________( __________ ) Name 4 Turtle behaviors. Two of them have parameter.

  17. Explore Turtle Behavior Create a Turtle object: Right-click on Turtle and choose newTurtle() Move the mouse pointer onto world and click to place. "new Turtle()" means to create aninstance (an object) of the Turtle class. A turtle object is a thing that has behavior. Click!

  18. Behavior: what can a Turtle do? Right-click on the Turtle instance. What behaviordoes a Turtle have?

  19. Turtle Behavior What are some Turtle behavior? Turtle act( ) _____(_______) _______( ) _______( ) _______(_______) "act( )" behavior is inheritedfrom the Actor class, so a Turtle can do it but Turtle doesn't provide its own code for act().

  20. Try using Behavior Right-click on the Turtle. Choose move(int distance). A dialog box asks you for the distance to move. Enter 50 What happens? OK, ok! I'm moving... see?

  21. Try more Behavior 2.Set the penDown 1. Set a pen color. 3. move 50 units. 4. Turn 90 degrees. 5. move 50 units again. What do you see?

  22. Each Object is Different Each object is unique, even when they belong to the same Class. 2. Use setAngle(double) to set Turtle#1 to 60 degrees. 1. Right-click to create 2 SpiralTurtle objects. Add to world. 3. Use setAngle(double) to set Turtle#2 to 90 degrees.

  23. Run it! Click Run. Does each Turtle behave differently? Turtle#1 turns 60 degrees. Turtle#2 turns 90 degrees.

  24. Turtles are Actors Turtle is a kind of Actor, called a subclass. This means: a Turtle can do whatever an Actor can do. I am an Actor. I should be in the movies. The arrow means: "Turtleis a subclass of Actor". This means "Turtle is a kind of Actor"

  25. Spiral Turtles are Turtles SpiralTurtle is a kind (subclass) ofTurtle. A SpiralTurtle can doanything a Turtle can do. SpiralTurtlespecializessome Turtle behavior. SpiralTurtles are much smarterthan Turtles.

  26. Spiral Turtles inherit Turtle Behavior A SpiralTurtle has all the behavior (methods) of a Turtle. Prove it! • Create 2 SpiralTurtle objects. • Use setColor (inherited from Turtle) to make one SpiralTurtleRED and the other one BLUE. Run it! Right-click on a SpiralTurtle to set its color.

  27. Program the World! We have interactivelycreated Turtles and set their behavior. We can write code in the TurtleWorld classtocreate turtles automatically. Greenfoot creates a TurtleWorldobject from the TurtleWorldclass.

  28. Edit the TurtleWorld class Double-click on TurtleWorld to edit the code. It opens an editor with the Java source code forTurtleWorld, like this:

  29. Create Turtles in TurtleWorld class Edit the code as shown below. Create a red turtle that turns 60 degrees. public TurtleWorld() { super(600, 600, 1); getBackground().setColor(Color.WHITE); getBackground().fill(); // a hexagonal spiral turtle SpiralTurtle turtle1 = new SpiralTurtle(); turtle1.setAngle(60); turtle1.setColor( "red" ); addObject( turtle1, 80, 90 ); } 80, 90 are the x,y values of whereto put the object in the world.

  30. Compile the Code and Run • Close the editor window. • Click Compile to recompile the scenario. • Run it. Is there a red turtle?

  31. What does this mean? SpiralTurtle turtle1 = new SpiralTurtle( ); creates a new object and a variable named turtle1 to refer to this object. turtle1.setAngle( 60 ); call the turtle's setAngle method. addObject( turtle1, 80, 90 ); addObject is a method belonging to World. It adds an Actor object to the world. 80, 90 are x,y coordinates of where to put the object.0,0 is the upper-left corner.

  32. Exercise • EditTurtleWorld and add anotherSpiralTurtle. • Makes the turtles as different as possible! • ...add as many turtles as you like, but make them unique.

  33. Review Object-oriented Concepts

  34. How Does an Object Know What to do? Where does behavior come from? How does a Turtle know what to do? How does an Ant know? I just do it. ?

  35. A Class Defines a Kind of thing Turtle Class Lander Class AntClass Turtle direction color x, y act( ) setColor(color) move(distance) turn(degrees) Ant homeHill = ? carryingFood act( ) haveFood() headHome() smellPheromone() Lander altitude = 200 speed = 0 act( ) checkCollision() fireThrust()

  36. A Class defines behavior of objects Turtle Class AntClass TurtleWorld Class act move turn penUp penDown setColor act checkFood haveFood headHome smellPheromone walk • act addObject setBackground

  37. Behavior is defined in methods SpiralTurtle's act() Method public void act( ) { size = size + step; move(size); turn(angle); }

  38. Objects knowstuff, too An object knows things (knowledge or state) An Actor needs to remember his location (x,y), direction... class Turtle extends Actor { String color = "red"; double direction; boolean penDown = true; double x, y; ... My color is red. Direction is 90o My pen is down

  39. What does a SpiralTurtle know? Right-click on a SpiralTurtle (object). Select Inspect.

  40. Attributes are what an Objects knows Attributes are variables defined inside the class. Each object gets its own set of attribute values. NOT Attributes: static are not attributes. Local variables (in a method) are not attributes. class Turtle extends Actor{ String color = "red"; double direction; boolean penDown = true; double x, y; ... // NOT an attribute static int MAX_SPEED = 10; public void sayHello( ) { // local variable int hour = getTime();

  41. A Class is written in Java class SpiralTurtle extends Turtle { double step = 4; // how much to grow double size = 0; // how much to move double angle = 90; // how much to turn public SpiralTurtle( ) { penDown(); } public void act( ) { size = size + step; move(size); turn(angle); } public void setAngle(double newAngle){ angle = newAngle; }

  42. Object-Oriented Programs A Program consists of objects that interact and do stuff. An objectis a "thing". An object knows stuff and performs behavior. Ant homeHill = ? carryingFood = ? act( ) haveFood() headHome() smellPheromone() walk() what an Ant knows (attributes) Ant behavior (methods)

More Related