Class AbstractCanvas
- java.lang.Object
-
- com.nerius.game.AbstractCanvas
-
- All Implemented Interfaces:
Destroyable
,RunnableDestroyable
,VisiblyUpdatable
,java.lang.Runnable
public abstract class AbstractCanvas extends java.lang.Object implements VisiblyUpdatable, RunnableDestroyable
Useful for de-coupling a game's logic from things such as applets and windows. This framework (this class andCanvasAWTStub
) are here to define a contract, hopefully making things simpler for games and animations.
-
-
Field Summary
Fields Modifier and Type Field Description protected java.awt.Dimension
m_size
protected CanvasAWTStub
m_stub
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractCanvas(CanvasAWTStub stub, java.awt.Dimension size)
The parametersize
is the size of the viewport that this board will be allowed to render into; it gets saved as member variablem_size
.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract void
destroy()
destroy()
is called externally, usually not by subclasses.java.awt.Dimension
getSize()
Required for implementation ofVisiblyUpdatable
.abstract void
run()
run()
is usually called externally, not by subclasses.abstract java.awt.Rectangle
update(java.awt.Graphics g)
This will automatically be called [usually by an asynchronous thread] wheneverm_stub
'srequestUpdate()
method is called.
-
-
-
Field Detail
-
m_stub
protected final CanvasAWTStub m_stub
-
m_size
protected final java.awt.Dimension m_size
-
-
Constructor Detail
-
AbstractCanvas
protected AbstractCanvas(CanvasAWTStub stub, java.awt.Dimension size)
The parametersize
is the size of the viewport that this board will be allowed to render into; it gets saved as member variablem_size
. Whenupdate(Graphics)
is called, this canvas should render into theGraphics
from the origin tom_size
.IMPORTANT NOTE: Don't call
stub.requestUpdate()
from this constructor!!!
-
-
Method Detail
-
run
public abstract void run()
run()
is usually called externally, not by subclasses. Calls tom_stub.requestUpdate()
shall be made only from the thread invokingrun()
.run()
isn't necessarily expected to return untildestroy()
is invoked (obviously,destroy()
will be called from a different thread). Furthermore,run()
shall only be called once for a given instance.An
AbstractCanvas
may create threads and run them. It is a good practice to create all such threads inrun()
, and to not return fromrun()
until all created threads have finished.- Specified by:
run
in interfacejava.lang.Runnable
-
destroy
public abstract void destroy()
destroy()
is called externally, usually not by subclasses.destroy()
itself should not block; it should return quickly.destroy()
should signal to the thread that calledrun()
to exit from therun()
method.Both
run()
anddestroy()
shall each be called at most once for any given instance. Defining the order in whichrun()
anddestroy()
are called is impossible because these two methods are called from different threads. Ifdestroy()
is called "before"run()
, then therun()
method, when and if it is invoked, should terminate immediately.- Specified by:
destroy
in interfaceDestroyable
-
update
public abstract java.awt.Rectangle update(java.awt.Graphics g)
This will automatically be called [usually by an asynchronous thread] wheneverm_stub
'srequestUpdate()
method is called. TherequestUpdate()
method will block until thisupdate()
code completes. This method is not meant to be directly called by this class (except for maybe debugging by updating to an offscreen image); it is only meant to be implemented. This canvas should callm_stub.requestUpdate()
to render itself.Returns a
Rectangle
representing the area which was updated; a null return value indicates that nothing was updated.Each
update()
will be called with a newGraphics
object.Having just an
update()
and nopaint()
fits in nicely in this abstraction layer; many canvases will have multiple image buffers to draw; these buffers can be drawn individually by this method, and the invoker of this method can keep a single buffer to be blitzed onto the screen. It is intended that such buffering not take place within the context of this class implementation.Subclasses should do as little computation as possible in this method. The recommended approach is to figure out exactly what is to be drawn before
m_stub.requestUpdate()
is called. This method should ideally return quickly.Please note that during destruction time, a call to
m_stub.requestUpdate()
may not necessarily trigger a call toupdate()
; it may just return immediately. However, implementations ofCanvasAWTStub
are required to not return prematurely during an invocation ofrequestUpdate()
if such a call triggersupdate()
, even in the end of life situation.- Specified by:
update
in interfaceVisiblyUpdatable
-
getSize
public final java.awt.Dimension getSize()
Required for implementation ofVisiblyUpdatable
. Normally subclasses would use theprotected
data memberm_size
.- Specified by:
getSize
in interfaceVisiblyUpdatable
-
-