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 intDEFAULT_MAX_FPSDefault maximum frames per second.
-
Constructor Summary
Constructors Constructor Description CanvasAWTAdapter(java.awt.Component target, boolean strictThreadControl)Thejava.awt.Componentthat 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 voiddestroy()The component using this adapter should calldestroy()when the adapter is no longer needed.java.awt.ImageproduceImage(int width, int height)Images are useful for doing double buffering.java.awt.ImageproduceImage(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.voidrequestUpdate()This method triggers a call toupdate(Graphics)on a correspondingAbstractCanvas.voidsetUpdatable(VisiblyUpdatable updatable)Should be regarded as part of the construction of an instance.java.awt.Rectangleupdate(java.awt.Graphics g)This should be called directly fromupdate(Graphics)of thejava.awt.Componentusing 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.Componentthat 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 theComponentis expected to behave as documented by the Java specification, also in regards to its current state. In a sense thisComponentcan be considered to be a "peer".However, the
Componentis 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
publicaccess 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 eitherFrameAWTStuborAppletAWTStub, both of which hide the implementation/usage details ofCanvasAWTAdapterandBoardAWTAdapter.- 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 thisupdatabledoes not get passed in as a constructor parameter is because the constructor of theupdatableobject, in practice, oftentimes takes thisCanvasAWTStubas 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:
destroyin interfaceDestroyable
-
produceImage
public java.awt.Image produceImage(int width, int height)Description copied from interface:CanvasAWTStubImages are useful for doing double buffering. The returned image can be rendered and it can be drawn into by way ofgetGraphics().- Specified by:
produceImagein interfaceCanvasAWTStub
-
produceImage
public java.awt.Image produceImage(java.awt.image.ImageProducer prod)
Description copied from interface:CanvasAWTStubgetGraphics()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:
produceImagein interfaceCanvasAWTStub
-
requestUpdate
public void requestUpdate()
Description copied from interface:CanvasAWTStubThis 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:
requestUpdatein interfaceCanvasAWTStub
-
update
public java.awt.Rectangle update(java.awt.Graphics g)
This should be called directly fromupdate(Graphics)of thejava.awt.Componentusing this adapter. Calls to this method are always expected to be in the same thread: the JVM's event thread.
-
-