com.hdcookbook.grin.animator
Class RepaintDrawEngine

java.lang.Object
  extended by com.hdcookbook.grin.animator.AnimationEngine
      extended by com.hdcookbook.grin.animator.ClockBasedEngine
          extended by com.hdcookbook.grin.animator.RepaintDrawEngine
All Implemented Interfaces:
java.lang.Runnable

public class RepaintDrawEngine
extends ClockBasedEngine

An animation engine that uses repaint draw. Repaint draw proceeds by calling Component.repaint(), and then waiting for the platform to call our Component.paint(Graphics) method. On most players, it's probably at least a little bit slower than direct draw, due to the thread context switching. Repaint draw can be significantly slower than direct draw for certain kinds of drawing, because a frame's damage rect is limited to one big rectangle (even when several small ones might do), and because of extra overhead erasing areas of the double buffer that are later drawn to in source mode.

Repaint draw has the advantage that it can take advantage of platform-supplied double buffering. That is, the engine will only needs to create a BufferedImage for double-buffering on platforms where HScene.isDoubleBuffered() returns false. On platforms where it returns true, repaint draw may be faster. Repaint draw may also make it easier to coexist with widgets (like HButton), particularly if they overlap with the area managed by the animation engine.


Field Summary
 
Fields inherited from class com.hdcookbook.grin.animator.AnimationEngine
modelTimeSkipped, renderContext, repaintBounds, targetsCanOverlap, transparent
 
Constructor Summary
RepaintDrawEngine()
          Create a new RepaintDrawEngine.
 
Method Summary
 void addRepaintArea(int x, int y, int width, int height)
          Sets this engine so that the given region of the image buffer is forced to be repainted at next frame.
protected  void callPaintTargets()
          Paint the current frame into the right graphics buffer.
protected  void clearArea(int x, int y, int width, int height)
          Called from showFrame() to cause an area to be cleared in the current frame.
protected  void finishedFrame()
          Called when the engine is finished drawing the current frame.
 java.awt.Component getComponent()
          Get the component that this animation engine renders into.
 int getHeight()
          Get the height of the area we display over
 int getWidth()
          Get the width of the area we display over
 void initContainer(java.awt.Container container, java.awt.Rectangle bounds)
          Initialize this engine with its parent container and the position and size of the engine within the container.
protected  boolean needsFullRedrawInAnimationLoop()
          Tell us whether or not this style of animation requires a full redraw of everything on the screen in each pass through the animation loop.
protected  void terminatingEraseScreen()
          Erase the screen because the AnimationManager is terminating.
 
Methods inherited from class com.hdcookbook.grin.animator.ClockBasedEngine
getFps, pause, runAnimationLoop, setFps, skipFrames, start
 
Methods inherited from class com.hdcookbook.grin.animator.AnimationEngine
advanceModel, checkDestroy, checkNewClients, destroy, destroyRequested, getAnimationClients, getDrawTargets, getEraseTargets, getModelTimeSkipped, getNumDrawTargets, getNumEraseTargets, initClients, initialize, paintFrame, paintNextFrameFully, paintTargets, repaintFrame, resetAnimationClients, run, setAllowOverlappingTargets, setDrawTargetCollapseThreshold, setThreadPriority, showFrame
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RepaintDrawEngine

public RepaintDrawEngine()
Create a new RepaintDrawEngine. It needs to be initialized with the various initXXX methods (including the inherited ones).

Method Detail

initContainer

public void initContainer(java.awt.Container container,
                          java.awt.Rectangle bounds)
Initialize this engine with its parent container and the position and size of the engine within the container. This should be called exactly once, before start() is called. A good time to call this would be in the animationInitialize() call within the context code passed to initialize().

The Component will be set to the specified bounds, and an internal double buffer may be created to these bounds. The container is expected to have a null layout; if it doesn't, this might change the widget's size to be different than the bounds passed in (and therefore the double buffer). The container must be visible when this is called.

Specified by:
initContainer in class AnimationEngine
See Also:
AnimationEngine.initialize(com.hdcookbook.grin.animator.AnimationContext), AnimationContext.animationInitialize()

getWidth

public int getWidth()
Get the width of the area we display over

Specified by:
getWidth in class AnimationEngine
Returns:
the width

getHeight

public int getHeight()
Get the height of the area we display over

Specified by:
getHeight in class AnimationEngine
Returns:
the height

getComponent

public java.awt.Component getComponent()
Get the component that this animation engine renders into. This is added by the engine to the container.

Specified by:
getComponent in class AnimationEngine
Returns:
the component, or null if initContainer hasn't been called yet.
See Also:
AnimationEngine.initialize(com.hdcookbook.grin.animator.AnimationContext), AnimationContext.animationInitialize()

addRepaintArea

public void addRepaintArea(int x,
                           int y,
                           int width,
                           int height)
Sets this engine so that the given region of the image buffer is forced to be repainted at next frame. This can be used if some section of the framebuffer was damaged somehow. If there are multiple calls to this method before the next frame drawing comes around, then the regions are combined together.


clearArea

protected void clearArea(int x,
                         int y,
                         int width,
                         int height)
Called from showFrame() to cause an area to be cleared in the current frame.

Specified by:
clearArea in class AnimationEngine
See Also:
AnimationEngine.showFrame()

needsFullRedrawInAnimationLoop

protected boolean needsFullRedrawInAnimationLoop()
Tell us whether or not this style of animation requires a full redraw of everything on the screen in each pass through the animation loop. This is called from the animation loop.

This is always false for repaint draw, because the AWT subsystem tells us the extent of our drawing, and it erases behind that area for us.

Specified by:
needsFullRedrawInAnimationLoop in class AnimationEngine

callPaintTargets

protected void callPaintTargets()
                         throws java.lang.InterruptedException
Paint the current frame into the right graphics buffer. The subclass implementation of this method should call paintTargets(Graphics2D).

Specified by:
callPaintTargets in class AnimationEngine
Throws:
java.lang.InterruptedException
See Also:
AnimationEngine.paintFrame(java.awt.Graphics2D)

finishedFrame

protected void finishedFrame()
Called when the engine is finished drawing the current frame. This should flush the results to the screen, if needed. The framework guarantees that each call to callPaintTargets() will be followed by a call to finishedFrame(), even if the thread is interrupted or there's a RuntimeException that terminates the thread.

Specified by:
finishedFrame in class AnimationEngine

terminatingEraseScreen

protected void terminatingEraseScreen()
Erase the screen because the AnimationManager is terminating. This is called in the animation thread as a result of a call to destroy(), just before the animation thread terminates.

Specified by:
terminatingEraseScreen in class AnimationEngine