(SIPC) Helper Classes Lesson

Helper Classes

In the previous modules, you created a lot of code to create actions to perform specific behaviors. You should be able to transfer that knowledge to new situations.   Recognizing behaviors that are similar so you can reuse code is part of being a good programmer.  Being able to analyze code from other programs and recognize the parts that you can use to complete a task is an important problem solving skill that programmers learn.  

To help you in reusing code, Greenfoot has a created a set of Helper classes to make repetitive tasks easier to program. We created the SharedResources class in our FlyingBird program. These helper classes serve the same purpose. They allow you to write code once and reuse it in different classes. The Helper classes are created for specific purposes.

To access the Helper classes, from the Greenfoot environment menu, click Edit and Import Classes. You will see a list of all the Helper classes available for import. Click on the Helper class you want to use and click the Import button. It will place the helper class into your program.

The image below shows how to access the Helper classes from the Greenfoot environment menu and a list of the classes and their purpose. In the next lesson we will see how to use some of these Helper classes in creating a program.

Screenshot of Edit screen with 
"Import Class" selected

Import Class:
Animal
Counter
GifImage
Label
Map
ScoreBoard
Simple Timer
SmoothMover
Weather

The Helper Classes are classes that can be used as a superclass for other actors. These classes are created for specific purposes. Look at the specific purpose for each helper class and the methods that are in it. As you look at the purpose for each helper class and the methods in it, think about a scenario where it could be used.

Helper Classes
Helper Class Description Methods
Animal Used to move and rotate objects, as well as look for objects and remove them.
  • turn (int angle)
  • move ()
  • canSee(Class class)
  • eat(Class class)
  • atWorldEdge()
Counter This class is used with the Scoreboard to add or subtract and update the score.
  • add(int score)
  • subtract(int score)
  • getValue()
  • updateImage()
GifImage This class can be used to read animated gif image files and extract the individual images of the animation
  • getImages()
  • getCurrentImage()
  • isRunning()
  • pause()
  • resume()
Label A label class that allows yo to display a textual value on screen. The Label is an actor, so you will need to create it, then add it to the world in Greenfoot. If you keep a reference to the Label, then you can change the text it displays.
  • stillborn(java.awt.Color fillColor)
  • setLineColor(java.awt.Color lineColor)
  • setValue(int value)
  • setValue(java.lang. String value)
Map A helper class that fetches a Google map for a specific location as an Image.
  • getImage()
  • setType()
  • setZoom()
  • zoomIn()
  • zoomOut()
ScoreBoard An actor class used to create the image for displaying a scoreboard or could be revised to display lives, using Greenfoots UserInfo class.
  • makeImage(String title, String prefix, int score)
SimpleTimer A simple timer class that allows you to keep track of how much time has passed between events. You use this class by creating a timer as a member field in your actor (or whatever)
  • mark()
  • millisElapsed()
SmoothMover A variation of an actor that maintains a precise location (using doubles for the co-ordinates instead of ints). This allows small precise movements (e.g. movements of 1 pixel or less) that do not lose precision.
  • getExactX()
  • getExactY()
  • move(double distance)
  • move(int distance)
  • setLocation(double x, double y)
  • setLocation(int x, int y)
Weather A helper class that fetch weather status for a specific location. It uses "The Weather Underground Core" API. This class is licensed under the terms of the GNU General Public License version 3.
  • getCity()
  • getCounty()
  • getPressure()
  • getTemperature()
  • getWindDirection()
  • getWindSpeed()
Mover The class Mover provides some basic movement methods for other actors that should be able to move left and right, jump up and fall down.
  • void moveRight()
  • void moveLeft()
  • boolean onGround()
  • boolean canMoveSideways()
  • boolean canMoveLeft()
  • boolean canMoveRight()
  • boolean canJump()
  • void jump(int speed)
  • void doGravity()
  • boolean atBottom()
  • void setGravity(int g)
  • void setVSpeed(intv)
  • boolean touches(Class clss)
  • void remove(Class clss)
  • void fall()
  • void remove(Class class)

 

In the next lesson, We'll look at some programs that use these classes and how they are helpful in building different programs.  

[CC BY 4.0] UNLESS OTHERWISE NOTED | IMAGES: LICENSED AND USED ACCORDING TO TERMS OF SUBSCRIPTION