Package com.nerius.game.util
Class CanvasAWTAdapter
- java.lang.Object
-
- com.nerius.game.util.CanvasAWTAdapter
-
- All Implemented Interfaces:
CanvasAWTStub
,Destroyable
- Direct Known Subclasses:
BoardAWTAdapter
public class CanvasAWTAdapter extends java.lang.Object implements CanvasAWTStub, Destroyable
The most basic implementation ofcom.nerius.game.CanvasAWTStub
.
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_MAX_FPS
Default maximum frames per second.
-
Constructor Summary
Constructors Constructor Description CanvasAWTAdapter(java.awt.Component target, boolean strictThreadControl)
Thejava.awt.Component
that is passed to the constructors is interacted with by the code in this class with only three calls to it: (1)Component.createImage(int,int)
, (2)Component.createImage(java.awt.image.ImageProducer)
, and (3)Component.repaint()
.CanvasAWTAdapter(java.awt.Component target, boolean strictThreadControl, int maxFramesPerSecond)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
destroy()
The component using this adapter should calldestroy()
when the adapter is no longer needed.java.awt.Image
produceImage(int width, int height)
Images are useful for doing double buffering.java.awt.Image
produceImage(java.awt.image.ImageProducer prod)
getGraphics()
on the returned image will fail; in other words the returned image is good for rendering and not so good for drawing onto.void
requestUpdate()
This method triggers a call toupdate(Graphics)
on a correspondingAbstractCanvas
.void
setUpdatable(VisiblyUpdatable updatable)
Should be regarded as part of the construction of an instance.java.awt.Rectangle
update(java.awt.Graphics g)
This should be called directly fromupdate(Graphics)
of thejava.awt.Component
using this adapter.
-
-
-
Field Detail
-
DEFAULT_MAX_FPS
public static final int DEFAULT_MAX_FPS
Default maximum frames per second. This affects the time gap between successive frames. Successive frames won't be allowed to be too close together in time proximity, for the sake of not exhausting CPU resources. A value of zero or negative is interpreted to mean no limit. Currently this value is set to two hundred, which means that a five millisecond gap is neccessary between successive frames of animation, at least. This gap is imposed by the logic in this class.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
CanvasAWTAdapter
public CanvasAWTAdapter(java.awt.Component target, boolean strictThreadControl)
Thejava.awt.Component
that is passed to the constructors is interacted with by the code in this class with only three calls to it: (1)Component.createImage(int,int)
, (2)Component.createImage(java.awt.image.ImageProducer)
, and (3)Component.repaint()
. To that extent theComponent
is expected to behave as documented by the Java specification, also in regards to its current state. In a sense thisComponent
can be considered to be a "peer".However, the
Component
is expected to keep its end of the bargain, which is the requirement to callCanvasAWTAdapter.update(Graphics)
wheneverComponent.update(Graphics)
is invoked by the system.The
public
access to this class and its constructors is left intact, even though in practice a programmer will rarely find the need to access this class directly, unless it is desired to embed anAbstractCanvas
(or one of its derivatives) inside of a larger application while using the native Java widget hierarchy. In most cases a programmer will choose to use eitherFrameAWTStub
orAppletAWTStub
, both of which hide the implementation/usage details ofCanvasAWTAdapter
andBoardAWTAdapter
.- See Also:
FrameAWTStub
,BoardAWTAdapter
-
CanvasAWTAdapter
public CanvasAWTAdapter(java.awt.Component target, boolean strictThreadControl, int maxFramesPerSecond)
- Parameters:
maxFramesPerSecond
- maximum frames per second; a value of zero or a negative value is interpreted to mean no FPS limit.- See Also:
CanvasAWTAdapter(Component,boolean)
-
-
Method Detail
-
setUpdatable
public void setUpdatable(VisiblyUpdatable updatable)
Should be regarded as part of the construction of an instance. Call this only once, and right after the constructor, before any other methods are called. The reason why thisupdatable
does not get passed in as a constructor parameter is because the constructor of theupdatable
object, in practice, oftentimes takes thisCanvasAWTStub
as one of its constructor parameters.
-
destroy
public void destroy()
The component using this adapter should calldestroy()
when the adapter is no longer needed. This will free up any threads that are waiting inrequestUpdate()
.- Specified by:
destroy
in interfaceDestroyable
-
produceImage
public java.awt.Image produceImage(int width, int height)
Description copied from interface:CanvasAWTStub
Images are useful for doing double buffering. The returned image can be rendered and it can be drawn into by way ofgetGraphics()
.- Specified by:
produceImage
in interfaceCanvasAWTStub
-
produceImage
public java.awt.Image produceImage(java.awt.image.ImageProducer prod)
Description copied from interface:CanvasAWTStub
getGraphics()
on the returned image will fail; in other words the returned image is good for rendering and not so good for drawing onto.- Specified by:
produceImage
in interfaceCanvasAWTStub
-
requestUpdate
public void requestUpdate()
Description copied from interface:CanvasAWTStub
This method triggers a call toupdate(Graphics)
on a correspondingAbstractCanvas
. This method blocks until theupdate()
method finishes. By agreement, this method shall only be called by a correspondingAbstractCanvas
, and only by the thread invokingAbstractCanvas.run()
.- Specified by:
requestUpdate
in interfaceCanvasAWTStub
-
update
public java.awt.Rectangle update(java.awt.Graphics g)
This should be called directly fromupdate(Graphics)
of thejava.awt.Component
using this adapter. Calls to this method are always expected to be in the same thread: the JVM's event thread.
-
-