|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object becker.robots.City
public class City
A city contains intersections joined by streets and avenues.
Intersection
s may contain Thing
s such as walls,
streetlights, flashers, and robots.
Constructor Summary | |
---|---|
City()
Construct a new city using the defaults stored in the becker.robots.ini initialization file. |
|
City(int numVisibleStreets,
int numVisibleAvenues)
Construct a new City which displays streets 0 through numVisibleStreets-1 and avenues 0 through
numVisibleAvenues-1 . |
|
City(int firstVisibleStreet,
int firstVisibleAvenue,
int numVisibleStreets,
int numVisibleAvenues)
Construct a new City which displays streets firstVisibleStreet through
numVisibleStreets-1 and avenues firstVisibleAvenue through
numVisibleAvenues-1 . |
|
City(int firstVisibleStreet,
int firstVisibleAvenue,
int numVisibleStreets,
int numVisibleAvenues,
int intersectionSize)
Construct a new City. |
|
City(Scanner in)
Construct a new city by reading information to construct it from a file. |
|
City(String fileName)
Construct a new city by reading information to construct it from a file. |
Method Summary | |
---|---|
protected void |
customizeIntersection(Intersection intersection)
Customize an intersection, perhaps by adding Things to it. |
IIterate<Light> |
examineLights()
Examine all the Light objects in this city, one at a time. |
IIterate<Robot> |
examineRobots()
Examine all the other Robot s, one at a time, that are on the same
intersection
as this robot. |
IIterate<Thing> |
examineThings()
Examine all the Thing s in this City, one at a time. |
IIterate<Thing> |
examineThings(IPredicate aPredicate)
Examine all the Thing s, one at a time, that are in this city
and match the provided predicate. |
protected Intersection |
getIntersection(int street,
int avenue)
Obtain a reference to a specified intersection within this city. |
IPredicate |
getThingCountPredicate()
Get the predicate used to show counts on the city's intersections. |
boolean |
isShowingThingCounts()
Is the city showing the number of Things and Robots on each intersection? |
protected void |
keyTyped(char key)
This method is called when the city's display has the focus and a key is typed. |
protected Intersection |
makeIntersection(int street,
int avenue)
Make an intersection which will appear at the specified avenue and street. |
void |
save(String indent,
PrintWriter out)
Save a representation of this city to a file for later use. |
void |
setFrameTitle(String title)
Sets the title of the implicitly created frame, if there is one. |
void |
setSize(int width,
int height)
Set the size of the implicitly created frame, if there is one. |
void |
setThingCountPredicate(IPredicate pred)
Set the predicate for what kinds of things to count when showing the number of things on each intersection. |
static void |
showFrame(boolean show)
Should the city be shown in a frame? The default is to show it. |
void |
showThingCounts(boolean show)
Show the number of Things and Robots on each intersection, counted according to the predicate set with setThingCountPredicate(becker.robots.IPredicate) . |
String |
toString()
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public City()
public City(int numVisibleStreets, int numVisibleAvenues)
numVisibleStreets-1
and avenues 0 through
numVisibleAvenues-1
.
numVisibleStreets
- The number of streets to display.numVisibleAvenues
- The number of avenues to display.public City(int firstVisibleStreet, int firstVisibleAvenue, int numVisibleStreets, int numVisibleAvenues)
firstVisibleStreet
through
numVisibleStreets-1
and avenues firstVisibleAvenue
through
numVisibleAvenues-1
.
firstVisibleStreet
- The street of the upper left intersection.firstVisibleAvenue
- The avenue of the upper left intersection.numVisibleStreets
- The number of streets to displaynumVisibleAvenues
- The number of avenues to displaypublic City(int firstVisibleStreet, int firstVisibleAvenue, int numVisibleStreets, int numVisibleAvenues, int intersectionSize)
firstVisibleStreet
and firstVisibleAvenue
in the upper left corner of the City. numVisibleStreet
streets and numVisibleAvenues
avenues
are displayed below and to the right. Use showFrame(boolean)
to avoid
creating the implicit frame.
firstVisibleStreet
- The street of the upper left intersection.firstVisibleAvenue
- The avenue of the upper left intersection.numVisibleStreets
- The number of streets to displaynumVisibleAvenues
- The number of avenues to displayintersectionSize
- The number of pixels each intersection takes uppublic City(String fileName)
City(Scanner)
.
If fileName does not exist, display a file chooser to allow the user to choose a file.
fileName
- the name of the file containing the information to
build this city.public City(Scanner in)
becker.robots.Robot
,
becker.robots.Wall
or becker.robots.Thing
.
Classes that extend Thing
(which includes extensions of
Robot
) may also be included provided the class is
in the program's classpath. Under normal usage, it will be without
any special action.
The class name is followed by the information that would normally
be passed to its constructor, except for the City
.
For example,
# A city with one robot, one thing and one wall. # Window title Clear snow # first street, first avenue, num streets, num avenues 0 0 5 9 # intersection size 38 becker.robots.Robot 1 2 Direction.EAST becker.robots.Thing 2 2 becker.robots.Wall 2 2 Direction.EAST
in
- the open file containing the information to
build this city.Method Detail |
---|
public static void showFrame(boolean show)
RobotUIComponents
.
This method, if used, must be called before the City object is constructed.
show
- True to show the city inside a frame; false otherwise.public void showThingCounts(boolean show)
setThingCountPredicate(becker.robots.IPredicate)
. The default
predicate counts things that can be moved by a robot.
show
- true if the number of things should be shown; false otherwisepublic boolean isShowingThingCounts()
public void setThingCountPredicate(IPredicate pred)
pred
- A predicate that returns true if the Thing/Robot should be counted,
false otherwise.public IPredicate getThingCountPredicate()
public void setFrameTitle(String title)
title
- The frame's titlepublic void setSize(int width, int height)
width
- the width of the frame, in pixelsheight
- the height of the frame, in pixelsprotected Intersection getIntersection(int street, int avenue)
street
- the street of the desired intersectionavenue
- the avenue of the desired intersection
protected Intersection makeIntersection(int street, int avenue)
Caution: Do not add Things such as Walls or Flashers to
the intersection before returning it; doing so results in infinite
recursion. Instead, override
customizeIntersection
.
avenue
- the avenue where the intersection will appear within the city.street
- the street where the intersection will appear within the city.
protected void customizeIntersection(Intersection intersection)
protected void customizeIntersection(Intersection intersection) { if (intersection.getStreet() == 0) { new Wall(this, intersection.getAvenue(), 0, Direction.NORTH); } }
intersection
- the intersection to be customized.protected void keyTyped(char key)
key
- The character typed at the keyboard.public final IIterate<Robot> examineRobots()
Robot
s, one at a time, that are on the same
intersection
as this robot. examineRobots
returns an iterator, which may be used as follows:
Robot
can be obtained with
Robot r = this.examineRobots().next();If there are no robots on the intersection, an exception is thrown.
for(Robot r : this.examineRobots()) { // do something with r }
Iteratorrobots = this.examineRobots(); while (robots.hasNext()) { Robot r = robots.next(); // do something with r }
examineThings()
,
examineThings(IPredicate)
,
examineLights()
public final IIterate<Light> examineLights()
Light
objects in this city, one at a time. examineLights
returns an iterator, which may be used as follows:
Light
can be obtained with
Light l = this.examineLights().next();If there are no lights on the intersection, an exception is thrown.
for(Light l : this.examineLights()) { // do something with l }
Iteratorlights = this.examineLights(); while (lights.hasNext()) { Light l = lights.next(); // do something with l }
examineThings()
,
examineThings(IPredicate)
,
examineRobots()
public final IIterate<Thing> examineThings()
Thing
s in this City, one at a time. examineThings
returns an iterator, which may be used as follows:
Thing
can be obtained with
Thing t = this.examineThings().next();If there are no things on the intersection, an exception is thrown.
for(Thing t : this.examineThings()) { // do something with t }
Iteratorthings = this.examineThings(); while (things.hasNext()) { Thing t = things.next(); // do something with t }
examineThings(IPredicate)
,
examineLights()
,
examineRobots()
public final IIterate<Thing> examineThings(IPredicate aPredicate)
Thing
s, one at a time, that are in this city
and match the provided predicate. examineThings
returns an iterator, which may be used as follows:
Thing
can be obtained with
Thing t = this.examineThings(aPredicate).next();If there are no things matching the predicate on the intersection, an exception is thrown.
for(Thing t : this.examineThings(aPredicate)) { // do something with t }
Iteratorthings = this.examineThings(aPredicate); while (things.hasNext()) { Thing t = things.next(); // do something with t }
aPredicate
- A predicate used to test whether a thing should be
included in the iteration. Commonly used predicates are defined
in IPredicate
.
examineThings()
,
examineLights()
,
examineRobots()
public void save(String indent, PrintWriter out)
indent
- A string of spaces prepended to each line of output.out
- an open output stream.public String toString()
toString
in class Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |