fang
Class AnimationCanvas

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by fang.AnimationCanvas
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable

public class AnimationCanvas
extends JComponent

This class is a JPanel that contains Sprites.

The license below must remain in the software unaltered.

Author:
Jam Jenkins
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
protected static Color DEFAULT_BACKGROUND
          the color to use when clearing the background
protected static Dimension DEFAULT_SIZE
          default size of canvas
 
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.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
AnimationCanvas()
          constructs an empty canvas with the default size
AnimationCanvas(Dimension size)
          constructs an empty canvas with a give size and default color.
AnimationCanvas(Dimension size, Color backgroundColor)
          constructs an empty canvas with a give size and background color
 
Method Summary
 void addBottom(Sprite... sprite)
          adds sprite to the bottom of the canvas.
 void addSprite(double layer, Sprite... sprite)
          adds a Sprite to the canvas.
 void addSprite(Sprite... sprite)
          adds a Sprite to the canvas.
 boolean containsSprite(Sprite sprite)
          determines if the Sprite exists on the AnimationCanvas.
 void flattenLayers()
          gives each sprite a different layer where the layers start at 0 and go up by one for each consecutive sprite
 Sprite[] getAllSprites()
          gets a copy of the Sprite collection
 double getAspect()
          gets the aspect ratio of width to height
 int getHeight()
          returns height of the canvas on which drawing can occur.
 Sprite[] getLayer(double layer)
          gets the Sprites in the given layer
 double getLayer(Sprite sprite)
          gets the layer on which a sprite is drawn.
 double getMaxX()
          gets the maximum x value which can be displayed on the canvas.
 double getMaxY()
          gets the maximum y value which can be displayed on the canvas.
 Dimension getPreferredSize()
          gets the preferred size as the current size
 int getWidth()
          returns width of the canvas on which drawing can occur.
protected  void paintComponent(Graphics brush)
          paints all of the Sprites
 void paintImmediately()
          redraws the area which contains the Sprites
 void removeAllSprites()
          clears the canvas of all Sprites
 void removeCursor()
          removes the cursor from the game canvas
 void removeSprite(Sprite... sprite)
          removes the Sprite from the canvas.
 void restoreCursor()
           
 void setAspect(double aspect)
           
 void setCursor(URL url)
          sets the cursor for the game engine
 void updateSprites(double timeInterval)
          updates the cached array and removes all destoyed Sprites
 
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, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, 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

DEFAULT_BACKGROUND

protected static final Color DEFAULT_BACKGROUND
the color to use when clearing the background


DEFAULT_SIZE

protected static final Dimension DEFAULT_SIZE
default size of canvas

Constructor Detail

AnimationCanvas

public AnimationCanvas()
constructs an empty canvas with the default size


AnimationCanvas

public AnimationCanvas(Dimension size)
constructs an empty canvas with a give size and default color.

Parameters:
size - the width and height to make the AnimationCanvas. This width and height specify the target aspect ratio and preferred size. If the size of the AnimationCanvas expands or shrinks, it will maintain the original aspect ratio of width to height.

AnimationCanvas

public AnimationCanvas(Dimension size,
                       Color backgroundColor)
constructs an empty canvas with a give size and background color

Parameters:
size - the width and height to make the AnimationCanvas. This width and height specify the target aspect ratio and preferred size. If the size of the AnimationCanvas expands or shrinks, it will maintain the original aspect ratio of width to height.
backgroundColor - the color to set the background to.
Method Detail

addBottom

public void addBottom(Sprite... sprite)
adds sprite to the bottom of the canvas. This method can be useful for adding and/or changing the background.

Parameters:
sprite - the sprite to add at the bottom

addSprite

public void addSprite(double layer,
                      Sprite... sprite)
adds a Sprite to the canvas. The sprite is added at the given layer. The highest layer is on top.

Parameters:
layer - the order in which to draw the added sprite. The highest layer is on top
sprite - the Sprites to be added

addSprite

public void addSprite(Sprite... sprite)
adds a Sprite to the canvas. The last added Sprite appears on top.

Parameters:
sprite - the Sprite to be added

containsSprite

public boolean containsSprite(Sprite sprite)
determines if the Sprite exists on the AnimationCanvas. This method does not indicate whether the Sprite in question is visible or enabled, just whether it is currently on the AnimationCanvas.

Parameters:
sprite - the Sprite to check
Returns:
true if th sprite does exist in the collection, false otherwise

flattenLayers

public void flattenLayers()
gives each sprite a different layer where the layers start at 0 and go up by one for each consecutive sprite


getAllSprites

public Sprite[] getAllSprites()
gets a copy of the Sprite collection

Returns:
the copy of the Sprites collection in array form

getAspect

public double getAspect()
gets the aspect ratio of width to height

Returns:
the aspect ratio width/height

getHeight

public int getHeight()
returns height of the canvas on which drawing can occur. The canvas size is clipped to maintain the initial aspect ratio.

Overrides:
getHeight in class JComponent
Returns:
height in pixels

getLayer

public Sprite[] getLayer(double layer)
gets the Sprites in the given layer

Parameters:
layer - the position in which to add the sprite. The highest layer is on top.
Returns:
the sprites in the layer, which could possibly be a zero length array if there is no such layer

getLayer

public double getLayer(Sprite sprite)
gets the layer on which a sprite is drawn. The highest layer is on top.

Parameters:
sprite - the sprite which to find the layer of.
Returns:

getMaxX

public double getMaxX()
gets the maximum x value which can be displayed on the canvas. When the canvas is square, this returns 1. When the canvas is more long than it is wide, the return value will be the width/height, and when the width is less than the height, the return value is 1.

Returns:
the maximum displayable x value

getMaxY

public double getMaxY()
gets the maximum y value which can be displayed on the canvas. If the canvas is square, the return value is 1. If the canvas is taller than it is wide, the return value is height/width. If the canvas is wider than it is tall, the return value is 1.

Returns:
the maximum displayable y value

getPreferredSize

public Dimension getPreferredSize()
gets the preferred size as the current size

Overrides:
getPreferredSize in class JComponent
Returns:
the current size

getWidth

public int getWidth()
returns width of the canvas on which drawing can occur. The canvas size is clipped to maintain the initial aspect ratio.

Overrides:
getWidth in class JComponent
Returns:
width in pixels

paintImmediately

public void paintImmediately()
redraws the area which contains the Sprites


removeAllSprites

public void removeAllSprites()
clears the canvas of all Sprites


removeCursor

public void removeCursor()
removes the cursor from the game canvas


removeSprite

public void removeSprite(Sprite... sprite)
removes the Sprite from the canvas. Removing a non-existent sprite has no effect and does not cause an exception.

Parameters:
sprite - the Sprite to be removed

restoreCursor

public void restoreCursor()

setAspect

public void setAspect(double aspect)

setCursor

public void setCursor(URL url)
sets the cursor for the game engine

Parameters:
url - the image to display as the cursor

updateSprites

public void updateSprites(double timeInterval)
updates the cached array and removes all destoyed Sprites


paintComponent

protected void paintComponent(Graphics brush)
paints all of the Sprites

Overrides:
paintComponent in class JComponent
Parameters:
brush - the Graphics of the component