org.formaria.swing.animation
Class AnimationSurface

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by org.formaria.swing.animation.AnimationSurface
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.awt.print.Printable, java.io.Serializable, TextHolder
Direct Known Subclasses:
CreditsText, FlowedTextComponent, MarqueeText, ProgressIndicator

public abstract class AnimationSurface
extends javax.swing.JComponent
implements java.awt.print.Printable, TextHolder

All demos extend this JAnimationSurface Abstract class. From this class demos must implement the drawObjects method. This class handles animated demos, the demo must implement the JAnimationContext interface.

Copyright (c) Formaria Ltd., 2008, This software is licensed under the GNU Public License (GPL), please see license.txt for more details. If you make commercial use of this software you must purchase a commercial license from Formaria.

$Revision: 1.27 $

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
protected  AnimationContext animationContext
           
protected  Animator animator
           
protected  java.lang.Object antiAlias
           
protected  float ascent
           
protected  boolean autoStart
           
protected  java.awt.image.BufferedImage bimg
           
protected  java.awt.AlphaComposite composite
           
protected  Project currentProject
          The owner project and the context in which this object operates.
protected  boolean finished
           
protected  int imageType
           
protected  int increment
           
protected  java.lang.String label
           
protected  int loopTime
           
protected  int oH
           
protected  int oldH
           
protected  int oldW
           
protected  int oW
           
protected  int oX
           
protected  int oY
           
protected  int position
           
protected  java.lang.Object rendering
           
protected  long sleepTime
           
protected  long startTime
           
protected  java.awt.Paint texture
           
protected  float timingFraction
           
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.print.Printable
NO_SUCH_PAGE, PAGE_EXISTS
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
protected AnimationSurface()
           
 
Method Summary
protected  void autoStart()
          Start an autostart animation if it hassn't started already
 void begin()
          Called when the Animator's animation begins.
 java.awt.image.BufferedImage createBufferedImage(int w, int h, int imgType)
          Create a buffer for the animation
 java.awt.Graphics2D createGraphics2D(int width, int height, java.awt.image.BufferedImage bi, java.awt.Graphics g)
          Create a graphics context for an image buffer and prepare that buffer for use.
abstract  void drawObjects(int w, int h, java.awt.Graphics2D g2)
          All classes that extend JAnimationSurface must implement this routine...
 void end()
          Called when the Animator's animation ends
 Animator getAnimator()
          Get the animation thread.
 boolean getAntiAlias()
          Get the value anti-aliasing value
 boolean getAutoStart()
          Get the autostart parameter
 boolean getComposite()
          Get the composite setup state
 int getImageType()
          Get the iamge type used for buffering the animation
 int getIncrement()
          Get the update increment
 int getLoopTime()
          Get the animation loop time
 boolean getRendering()
          Get the rendering hint
 long getSleepTime()
          Get the sleep time in milliseconds
 java.lang.String getText()
          Get the text of the control
 void init()
          Initialize the animation surface and prepare the starting point/setup
 boolean isAnimated(Animator at)
          Should the object be animated in this step?
 boolean isFinished(Animator at)
          Are there more steps to come?
 boolean isStarted()
          Has the animation begun
 void paintComponent(java.awt.Graphics g)
          Paint the component, buffering the image in the process if necessary
 int print(java.awt.Graphics g, java.awt.print.PageFormat pf, int pi)
          Print this component
static java.lang.String quoteReplacement(java.lang.String s)
          Returns a literal replacement String for the specified String.
 void render(Animator at)
          Do the rendering of the animation object
 void repeat()
          Called when the Animator repeats the animation cycle
static java.lang.String replace(java.lang.String source, java.lang.CharSequence target, java.lang.CharSequence replacement)
          Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
 void reset()
           
 void setAnimator(Animator thread)
          Set the animation thread.
 void setAntiAlias(boolean aa)
          Turn the value anti-aliasing on or off.
 void setAutoStart(boolean as)
          Flag the animation to automatically start on first display
 void setComposite(boolean cp)
          Set the composite instance
 void setImageType(int imgType)
          Set the way in which the image is presented.
 void setIncrement(int incr)
          Sets the update increment.
 void setLoopTime(int lt)
          Set the animation loop time in milliseconds
 void setRendering(boolean rd)
          Set the rendering hint for quality or speed
 void setSleepTime(long st)
          Set the sleep time for the animation thread
 void setText(java.lang.String newText)
          Set the label/text of the component.
 void setTexture(java.lang.Object obj)
          Set a texture for paints
 void start()
          Begin the animation
 void stop()
          End the animation
 void timingEvent(float fraction)
          This method will receive all of the timing events from an Animator during an animation.
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

antiAlias

protected java.lang.Object antiAlias

rendering

protected java.lang.Object rendering

texture

protected java.awt.Paint texture

composite

protected java.awt.AlphaComposite composite

bimg

protected java.awt.image.BufferedImage bimg

imageType

protected int imageType

animator

protected Animator animator

sleepTime

protected long sleepTime

startTime

protected long startTime

animationContext

protected AnimationContext animationContext

oldW

protected int oldW

oldH

protected int oldH

oX

protected int oX

oY

protected int oY

oW

protected int oW

oH

protected int oH

label

protected java.lang.String label

finished

protected boolean finished

autoStart

protected boolean autoStart

ascent

protected float ascent

position

protected int position

increment

protected int increment

loopTime

protected int loopTime

timingFraction

protected float timingFraction

currentProject

protected Project currentProject
The owner project and the context in which this object operates.

Constructor Detail

AnimationSurface

protected AnimationSurface()
Method Detail

init

public void init()
Initialize the animation surface and prepare the starting point/setup


reset

public void reset()

timingEvent

public void timingEvent(float fraction)
This method will receive all of the timing events from an Animator during an animation. The fraction is the percent elapsed (0 to 1) of the current animation cycle.

Parameters:
fraction - the fraction of completion between the start and end of the current cycle. Note that on reversing cycles (Animator.Direction#BACKWARD) the fraction decreases from 1.0 to 0 on backwards-running cycles. Note also that animations with a duration of INFINITE will call timingEvent with an undefined value for fraction, since there is no fraction that makes sense if the animation has no defined length.
See Also:
Animator.Direction

begin

public void begin()
Called when the Animator's animation begins. This provides a chance for targets to perform any setup required at animation start time.


end

public void end()
Called when the Animator's animation ends


repeat

public void repeat()
Called when the Animator repeats the animation cycle


isFinished

public boolean isFinished(Animator at)
Are there more steps to come?

Parameters:
at - the animation thread
Returns:
true if the animation has completed

isAnimated

public boolean isAnimated(Animator at)
Should the object be animated in this step?

Parameters:
at - the animation thread
Returns:
true if the animation has started and is still animating

isStarted

public boolean isStarted()
Has the animation begun

Parameters:
at - the animation thread
Returns:
true if the animation has started

render

public void render(Animator at)
Do the rendering of the animation object

Parameters:
at - the animation thread

setIncrement

public void setIncrement(int incr)
Sets the update increment.

Parameters:
incr - the increment for animation steps in milliseconds

getIncrement

public int getIncrement()
Get the update increment

Returns:
the animation step increment in milliseconds

setText

public void setText(java.lang.String newText)
Set the label/text of the component. Usage depends on the concrete component type

Specified by:
setText in interface TextHolder
Parameters:
newText - the new text

getText

public java.lang.String getText()
Get the text of the control

Specified by:
getText in interface TextHolder
Returns:
the text

replace

public static java.lang.String replace(java.lang.String source,
                                       java.lang.CharSequence target,
                                       java.lang.CharSequence replacement)
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab". Pulled from JDK 1.5 for compatibility with 1.4

Parameters:
source - the source text/string
target - The sequence of char values to be replaced
replacement - The replacement sequence of char values
Returns:
The resulting string

quoteReplacement

public static java.lang.String quoteReplacement(java.lang.String s)
Returns a literal replacement String for the specified String. This method produces a String that will work use as a literal replacement s in the appendReplacement method of the Matcher class. The String produced will match the sequence of characters in s treated as a literal sequence. Slashes ('\') and dollar signs ('$') will be given no special meaning.

Parameters:
s - The string to be literalized
Returns:
A literal string replacement
Since:
1.5

setImageType

public void setImageType(int imgType)
Set the way in which the image is presented.

Parameters:
imgType - = 0 to create a buffered image of TYPE_INT_ARGB,
1 = this class does not set up a buffered image or,
>2 = BufferedImage.<> + 2 where type is the BufferedImage type constant

getImageType

public int getImageType()
Get the iamge type used for buffering the animation

Parameters:
the - image type flag

setAntiAlias

public void setAntiAlias(boolean aa)
Turn the value anti-aliasing on or off. By default the anti-aliasing is on

Parameters:
aa - true to turn the anti-aliasing on

getAntiAlias

public boolean getAntiAlias()
Get the value anti-aliasing value

Parameters:
aa - true if anti-aliasing is on

setRendering

public void setRendering(boolean rd)
Set the rendering hint for quality or speed

Parameters:
rd - true for quality, false for speed

getRendering

public boolean getRendering()
Get the rendering hint

Returns:
true if rendering is to favour quality over speed

setTexture

public void setTexture(java.lang.Object obj)
Set a texture for paints

Parameters:
obj - the Paint object, for example a GradientPaint

setComposite

public void setComposite(boolean cp)
Set the composite instance

Parameters:
cp - the composite e.g. AlphaComposite

getComposite

public boolean getComposite()
Get the composite setup state

Returns:
true if a compisite is used

setLoopTime

public void setLoopTime(int lt)
Set the animation loop time in milliseconds

Parameters:
lt - the animation time

getLoopTime

public int getLoopTime()
Get the animation loop time

Returns:
the loop time

createBufferedImage

public java.awt.image.BufferedImage createBufferedImage(int w,
                                                        int h,
                                                        int imgType)
Create a buffer for the animation

Parameters:
w - the image width
h - the image height
imgType - the image type e.g. BufferedImage.TYPE_INT_ARGB
Returns:
the new image

createGraphics2D

public java.awt.Graphics2D createGraphics2D(int width,
                                            int height,
                                            java.awt.image.BufferedImage bi,
                                            java.awt.Graphics g)
Create a graphics context for an image buffer and prepare that buffer for use.

Parameters:
width - the width
height - the height
bi - the buffered image
g - the graphics context
Returns:
the Java2D graphics context for the buffer

drawObjects

public abstract void drawObjects(int w,
                                 int h,
                                 java.awt.Graphics2D g2)
All classes that extend JAnimationSurface must implement this routine...

Parameters:
w - the width
h - the height
g2 - the graphics context

paintComponent

public void paintComponent(java.awt.Graphics g)
Paint the component, buffering the image in the process if necessary

Overrides:
paintComponent in class javax.swing.JComponent
Parameters:
g - the graphics context

print

public int print(java.awt.Graphics g,
                 java.awt.print.PageFormat pf,
                 int pi)
          throws java.awt.print.PrinterException
Print this component

Specified by:
print in interface java.awt.print.Printable
Parameters:
g - the graphics context
pf - the page format
pi - the page index
Returns:
Printable.PAGE_EXISTS on success or Printable.NO_SUCH_PAGE
Throws:
java.awt.print.PrinterException - Problems!

setAnimator

public void setAnimator(Animator thread)
Set the animation thread.

Parameters:
thread - the animation thread

getAnimator

public Animator getAnimator()
Get the animation thread.

Returns:
the animation thread

start

public void start()
Begin the animation


stop

public void stop()
End the animation


setAutoStart

public void setAutoStart(boolean as)
Flag the animation to automatically start on first display

Parameters:
as - true to automatically start, false to wait for a start signal

getAutoStart

public boolean getAutoStart()
Get the autostart parameter

Returns:
the autostart value

autoStart

protected void autoStart()
Start an autostart animation if it hassn't started already


setSleepTime

public void setSleepTime(long st)
Set the sleep time for the animation thread

Parameters:
st - defaults to 100 milliseconds

getSleepTime

public long getSleepTime()
Get the sleep time in milliseconds

Returns:
the sleep time