|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object universe.world.World<Msg>
public abstract class World<Msg extends java.io.Serializable>
A Class representing a World and the related methods for drawing
the world and handling various events connected to a Universe
. The class is parameterized by the
type of messages (which must extend Serializable
) that are passed throughout the
Universe.
In order to implement a functioning World you must
extend this class, and implement an onDraw
method. Other handler methods
(tickRate
, onTick
, onMouse
, onKey
, onRelease
, stopWhen
, and lastScene
) are optional, and can
be overridden to add new functionality.
Most handler methods return a Package
,
which contains the updated World, and a possible message
to the connected Universe. In order to interact with the
Universe and other Worlds, implementations must
override the onReceive
method that handles messages from the Universe.
See also world.World
for similar documentation on individual methods.
Below is a simple example of aWorld
that adds a new point at each mouse click. The world contains aScene
and a newCircle
is placed for each"button-down"
event received.import image.*; import universe.Package; import universe.world.*; // Mouse World/Universe point/click Test public class MousePointsWorld extends World<None>{ public static void main(String[] s){ new MousePointsWorld(new EmptyScene(400,400)) .bigBang(); } // The inner Scene Scene scene; // Create a new World with the given Scene MousePointsWorld(Scene scene){ this.scene = scene; } // Draw by returning the inner Scene public Scene onDraw(){ return this.scene; } // On a mouse click add a circle to the inner Scene public Package<None> onMouse(int x, int y, String me){ if(!me.equals("button-down")) return new Package<None>(this); else return new Package<None>( new MousePointsWorld( this.scene.placeImage(new Circle(20, "solid","red") .overlay(new Circle(20, "outline", "blue")), x, y))); } }After a few mouse clicks, the window will look something like this:
Field Summary | |
---|---|
static double |
DEFAULT_TICK_RATE
Default Tick rate for the world: ~33 frames per second |
static java.lang.String |
KEY_ARROW_DOWN
Key arrow-down event String |
static java.lang.String |
KEY_ARROW_LEFT
Key arrow-left event String |
static java.lang.String |
KEY_ARROW_RIGHT
Key arrow-right event String |
static java.lang.String |
KEY_ARROW_UP
Key arrow-up event String |
static java.lang.String |
MOUSE_DOWN
Mouse down (button-down) event String |
static java.lang.String |
MOUSE_DRAG
Mouse down & move (drag) event String |
static java.lang.String |
MOUSE_ENTER
Mouse window enter (enter) event String |
static java.lang.String |
MOUSE_LEAVE
Mouse window leave (leave) event String |
static java.lang.String |
MOUSE_MOVE
Mouse motion (move) event String |
static java.lang.String |
MOUSE_UP
Mouse up (button-up) event String |
Constructor Summary | |
---|---|
World()
|
Method Summary | |
---|---|
World<Msg> |
bigBang()
Kick off the interaction/animation without connecting to the Universe. |
World<Msg> |
bigBang(java.lang.String server,
java.lang.String name)
Kick off the interaction/animation and connect to the given Universe server URL, under the given client name. |
Scene |
lastScene()
Returns the Scene that should be displayed when the interaction/animation completes ( stopWhen()
returns true). |
abstract Scene |
onDraw()
Return a visualization of this World as a Scene . |
Package<Msg> |
onKey(java.lang.String ke)
Produce a Package, containing a (possibly) new World and a possible Message, when a key is pressed. |
Package<Msg> |
onMouse(int x,
int y,
java.lang.String me)
Produce a Package, containing a (possibly) new World and a possible Message, when a mouse event is triggered. |
Package<Msg> |
onReceive(Msg msg)
Produce a Package, containing a (possibly) new World and a possible Message, when a message from the universe is received. |
Package<Msg> |
onRelease(java.lang.String ke)
Produce a Package, containing a (possibly) new World and a possible Message, when a key is released. |
Package<Msg> |
onTick()
Produce a Package, containing a (possibly) new World and a possible Message, based on the Tick of the clock. |
boolean |
stopWhen()
Determine if the World/interaction/animation should be stopped. |
double |
tickRate()
Return the tick rate for this World in seconds. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static double DEFAULT_TICK_RATE
public static java.lang.String MOUSE_DOWN
public static java.lang.String MOUSE_UP
public static java.lang.String MOUSE_ENTER
public static java.lang.String MOUSE_LEAVE
public static java.lang.String MOUSE_MOVE
public static java.lang.String MOUSE_DRAG
public static java.lang.String KEY_ARROW_UP
public static java.lang.String KEY_ARROW_DOWN
public static java.lang.String KEY_ARROW_LEFT
public static java.lang.String KEY_ARROW_RIGHT
Constructor Detail |
---|
public World()
Method Detail |
---|
public abstract Scene onDraw()
Scene
.
See EmptyScene
, Scene.placeImage(Image, int, int)
, and
Scene.addLine(int, int, int, int, String)
for documentation on
constructing Scenes
public double tickRate()
public Package<Msg> onTick()
public Package<Msg> onMouse(int x, int y, java.lang.String me)
Possible Mouse Events
"button-down" : | The user presses a mouse button in the World window |
"button-up" : | The user releases a mouse button in the World window |
"move" : | The user moves the mouse in the World window |
"drag" : | The user holds a mouse button and moves the mouse in the World window |
"enter" : | The user moves the mouse in-to the World window |
"leave" : | The user moves the mouse out-of the World window |
public Package<Msg> onKey(java.lang.String ke)
Special Keys
"up" : | The user presses the up-arrow key |
"down" : | The user presses the down-arrow key |
"left" : | The user presses the left-arrow key |
"right" : | The user presses the right-arrow key |
public Package<Msg> onRelease(java.lang.String ke)
Special Keys
"up" : | The user presses the up-arrow key |
"down" : | The user presses the down-arrow key |
"left" : | The user presses the left-arrow key |
"right" : | The user presses the right-arrow key |
public Package<Msg> onReceive(Msg msg)
public boolean stopWhen()
lastScene()
to be used to
draw the final Scene.
public Scene lastScene()
stopWhen()
returns true).
public World<Msg> bigBang(java.lang.String server, java.lang.String name)
public World<Msg> bigBang()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |