javalib.funworld
Class World

java.lang.Object
  extended by javalib.funworld.World
All Implemented Interfaces:
Drawable
Direct Known Subclasses:
Animation

public abstract class World
extends Object
implements Drawable

World for programming interactive games - with graphics, key events, mouse events and a timer.

Since:
November 15 2007, March 17 2008, October 19 2009, February 4 2012
Author:
Viera K. Proulx

Field Summary
 WorldEnd lastWorld
          the last world - if needed
protected  MyTimer mytime
          the timer for this world
protected  boolean stopTimer
          timer events not processed when the mouse event is processed
 WorldCanvas theCanvas
          the canvas that displays the current world
 
Constructor Summary
World()
          The default constructor.
 
Method Summary
 boolean bigBang()
          Start the world by creating a canvas whose size is that of the initial image, with no timer.
 boolean bigBang(double speed)
          Start the world by creating a canvas whose size is that of the initial image, and starting the timer at a given speed.
 boolean bigBang(int w, int h)
          Start the world by creating a canvas of the given size, creating and adding the key and mouse adapters, without running the the timer.
 boolean bigBang(int w, int h, double speed)
          Start the world by creating a canvas of the given size, creating and adding the key and mouse adapters, and starting the timer at the given speed.
protected  boolean drawWorld(String s)
          Invoke the user defined makeImage method, if this World has been initialized via bigBang and did not stop or end, otherwise invoke the user defined lastImage method,
 World endOfWorld(String s)
          End the world interactions - leave the canvas open, show the image of the last world with the given message
 int getCurrentHeight()
          Get the current height of the animation window (which may have been adjusted by the user).
 int getCurrentWidth()
          Get the current width of the animation window (which may have been adjusted by the user).
 double getElapsedTime()
          Getter for elapsed time.
 int getHeight()
          Get the initially-specified height.
 int getTickCount()
          Getter for tickCount.
 int getWidth()
          Get the initially-specified width.
 WorldImage lastImage(String s)
          User defined method to draw the World.
abstract  WorldImage makeImage()
          User defined method to draw the World.
 World onKeyEvent(String s)
          User defined method to be invoked by the key adapter on selected key events.
 World onMouseClicked(Posn mouse)
          User defined method to be invoked by the mouse adapter when a mouse is clicked.
 World onMouseDragged(Posn mouse)
          User defined method to be invoked by the mouse adapter when a mouse is dragged.
 World onMouseEntered(Posn mouse)
          User defined method to be invoked by the mouse adapter when a mouse is entered.
 World onMouseExited(Posn mouse)
          User defined method to be invoked by the mouse adapter when a mouse is exited.
 World onMouseMoved(Posn mouse)
          User defined method to be invoked by the mouse adapter when a mouse is moved.
 World onMousePressed(Posn mouse)
          User defined method to be invoked by the mouse adapter when a mouse is pressed.
 World onMouseReleased(Posn mouse)
          User defined method to be invoked by the mouse adapter when a mouse is released.
 World onTick()
          User defined method to be invoked by the timer on each tick.
protected  World processKeyEvent(String ke)
          The method invoked by the key adapter on selected key events.
protected  World processMouseClicked(Posn mouse)
          The method invoked by the mouse adapter on mouse clicked event.
protected  World processMouseDragged(Posn mouse)
          The method invoked by the mouse adapter on mouse dragged event.
protected  World processMouseEntered(Posn mouse)
          The method invoked by the mouse adapter on mouse entered event.
protected  World processMouseExited(Posn mouse)
          The method invoked by the mouse adapter on mouse exited event.
protected  World processMouseMoved(Posn mouse)
          The method invoked by the mouse adapter on mouse moved event.
protected  World processMousePressed(Posn mouse)
          The method invoked by the mouse adapter on mouse pressed event.
protected  World processMouseReleased(Posn mouse)
          The method invoked by the mouse adapter on mouse released event.
protected  World processTick()
          The method invoked by the timer on each tick.
protected  void stopWorld()
          Stop the world, close all listeners and the timer, draw the last Scene.
 World testOnTick()
          The onTick method is invoked only if the world exists.
 WorldEnd worldEnds()
          This method is invoked at each tick.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lastWorld

public WorldEnd lastWorld
the last world - if needed


mytime

protected transient MyTimer mytime
the timer for this world


stopTimer

protected transient boolean stopTimer
timer events not processed when the mouse event is processed


theCanvas

public WorldCanvas theCanvas
the canvas that displays the current world

Constructor Detail

World

public World()
The default constructor. To start the world one must invoke the bigBang method.

Method Detail

bigBang

public boolean bigBang()
Start the world by creating a canvas whose size is that of the initial image, with no timer.

Returns:
true
Since:
Feb. 10, 2013

bigBang

public boolean bigBang(double speed)
Start the world by creating a canvas whose size is that of the initial image, and starting the timer at a given speed.

Parameters:
speed - the interval between clock ticks, in seconds
Returns:
true
Since:
Feb. 10, 2013

bigBang

public boolean bigBang(int w,
                       int h)
Start the world by creating a canvas of the given size, creating and adding the key and mouse adapters, without running the the timer.

Parameters:
w - the width of the Canvas
h - the height of the Canvas
Returns:
true

bigBang

public boolean bigBang(int w,
                       int h,
                       double speed)
Start the world by creating a canvas of the given size, creating and adding the key and mouse adapters, and starting the timer at the given speed.

Parameters:
w - the width of the Canvas
h - the height of the Canvas
speed - the speed at which the clock runs
Returns:
true

drawWorld

protected boolean drawWorld(String s)
Invoke the user defined makeImage method, if this World has been initialized via bigBang and did not stop or end, otherwise invoke the user defined lastImage method,

Parameters:
s - a String to be passed on to lastImage
Returns:
true

endOfWorld

public World endOfWorld(String s)
End the world interactions - leave the canvas open, show the image of the last world with the given message

Parameters:
s - the message to display
Returns:
this world

getCurrentHeight

public int getCurrentHeight()
Get the current height of the animation window (which may have been adjusted by the user).

Returns:
an int
Since:
Mar. 7, 2013

getCurrentWidth

public int getCurrentWidth()
Get the current width of the animation window (which may have been adjusted by the user).

Returns:
an int
Since:
Mar. 7, 2013

getElapsedTime

public double getElapsedTime()
Getter for elapsed time.

Returns:
the number of seconds so far.
Since:
Mar. 7, 2013

getHeight

public int getHeight()
Get the initially-specified height.

Returns:
the height initially specified in the bigBang call, or if not specified, the height of the first image.
Since:
Mar. 7, 2013

getTickCount

public int getTickCount()
Getter for tickCount.

Returns:
the number of ticks so far.
Since:
Mar. 7, 2013

getWidth

public int getWidth()
Get the initially-specified width.

Returns:
the width initially specified in the bigBang call, or if not specified, the width of the first image.
Since:
Mar. 7, 2013

lastImage

public WorldImage lastImage(String s)

User defined method to draw the World.

Override this method in the game world class

Parameters:
s - a String that endOfWorld can use to communicate to lastImage
Returns:
the image that represents the last world to be drawn

makeImage

public abstract WorldImage makeImage()

User defined method to draw the World.

Override this method in the game world class

Specified by:
makeImage in interface Drawable
Returns:
the image that represents this world at this moment

onKeyEvent

public World onKeyEvent(String s)

User defined method to be invoked by the key adapter on selected key events. Produces a new World.

Override this method in the game world class

Parameters:
a - String indicating the key that was pressed. This will be either a single character (for letters, numbers, punctuation) or one of the following: "backspace", "tab", "newline", "escape", "page up", "page down", "end", "home", "left", "up", "right", "down", "delete", or "f1" through "f12"
Returns:
World that needs to have the canvas and the event handlers initialized

onMouseClicked

public World onMouseClicked(Posn mouse)

User defined method to be invoked by the mouse adapter when a mouse is clicked. Update the World.

Override this method in the game world class

Parameters:
mouse - the location of the mouse when clicked
Returns:
World after the mouse event

onMouseDragged

public World onMouseDragged(Posn mouse)

User defined method to be invoked by the mouse adapter when a mouse is dragged. Update the World.

Override this method in the game world class

Parameters:
mouse - the location of the mouse when moved
Returns:
World after the mouse event

onMouseEntered

public World onMouseEntered(Posn mouse)

User defined method to be invoked by the mouse adapter when a mouse is entered. Update the World.

Override this method in the game world class

Parameters:
mouse - the location of the mouse when entered
Returns:
World after the mouse event

onMouseExited

public World onMouseExited(Posn mouse)

User defined method to be invoked by the mouse adapter when a mouse is exited. Update the World.

Override this method in the game world class

Parameters:
mouse - the location of the mouse when exited
Returns:
World after the mouse event

onMouseMoved

public World onMouseMoved(Posn mouse)

User defined method to be invoked by the mouse adapter when a mouse is moved. Update the World.

Override this method in the game world class

Parameters:
mouse - the location of the mouse when moved
Returns:
World after the mouse event

onMousePressed

public World onMousePressed(Posn mouse)

User defined method to be invoked by the mouse adapter when a mouse is pressed. Update the World.

Override this method in the game world class

Parameters:
mouse - the location of the mouse when pressed
Returns:
World after the mouse event

onMouseReleased

public World onMouseReleased(Posn mouse)

User defined method to be invoked by the mouse adapter when a mouse is released. Update the World.

Override this method in the game world class

Parameters:
mouse - the location of the mouse when released
Returns:
World after the mouse event

onTick

public World onTick()

User defined method to be invoked by the timer on each tick. Produces a new World.

Override this method in the game world class

Returns:
World that needs to have the canvas and the event handlers initialized

processKeyEvent

protected World processKeyEvent(String ke)
The method invoked by the key adapter on selected key events. Delegates to the user to define a new state of the world, then resets the canvas and event handlers for the new world to those currently used.

Returns:
World after the key event

processMouseClicked

protected World processMouseClicked(Posn mouse)
The method invoked by the mouse adapter on mouse clicked event. Delegates to the user to define a new state of the world.

Parameters:
mouse - the location of the mouse when clicked
Returns:
World after the mouse event

processMouseDragged

protected World processMouseDragged(Posn mouse)
The method invoked by the mouse adapter on mouse dragged event. Delegates to the user to define a new state of the world.

Parameters:
mouse - the location of the mouse when dragged
Returns:
World after the mouse event

processMouseEntered

protected World processMouseEntered(Posn mouse)
The method invoked by the mouse adapter on mouse entered event. Delegates to the user to define a new state of the world.

Parameters:
mouse - the location of the mouse when entered
Returns:
World after the mouse event

processMouseExited

protected World processMouseExited(Posn mouse)
The method invoked by the mouse adapter on mouse exited event. Delegates to the user to define a new state of the world.

Parameters:
mouse - the location of the mouse when exited
Returns:
World after the mouse event

processMouseMoved

protected World processMouseMoved(Posn mouse)
The method invoked by the mouse adapter on mouse moved event. Delegates to the user to define a new state of the world.

Parameters:
mouse - the location of the mouse when moved
Returns:
World after the mouse event

processMousePressed

protected World processMousePressed(Posn mouse)
The method invoked by the mouse adapter on mouse pressed event. Delegates to the user to define a new state of the world.

Parameters:
mouse - the location of the mouse when pressed
Returns:
World after the mouse event

processMouseReleased

protected World processMouseReleased(Posn mouse)
The method invoked by the mouse adapter on mouse released event. Delegates to the user to define a new state of the world.

Parameters:
mouse - the location of the mouse when released
Returns:
World after the mouse event

processTick

protected World processTick()
The method invoked by the timer on each tick. Delegates to the user to define a new state of the world, then resets the canvas and event handlers for the new world to those currently used.

Returns:
World after the tick event

stopWorld

protected void stopWorld()
Stop the world, close all listeners and the timer, draw the last Scene.


testOnTick

public World testOnTick()
The onTick method is invoked only if the world exists. To test the method onTick we provide this method that will invoke the onTick method for the testing purposes.


worldEnds

public WorldEnd worldEnds()

This method is invoked at each tick. It checks if the world should end now.

The saved image will be shown when the world ends, otherwise it is ignored.

Returns:
pair (true, last image) or (false, any image)