tipgame
Class AnimationCanvas

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

public class AnimationCanvas
extends javax.swing.JComponent

This class is essentially a JPanel that contains Sprites

Author:
Jam Jenkins
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.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
private static java.awt.Color DEFAULT_BACKGROUND
          the color to use when clearing the background
private static java.awt.Dimension DEFAULT_SIZE
          default size of canvas
private  int height
          height of canvas
private  java.util.LinkedHashSet<Sprite> sprites
          the ordered collection of Sprites.
private  Sprite[] waterMark
          used for the transparent text at the bottom right
private  int width
          width of the 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
AnimationCanvas(java.awt.Dimension size)
          constructs an empty canvas
 
Method Summary
 void addBottom(Sprite sprite)
          adds sprite to the bottom of the canvas
 void addSprite(Sprite sprite)
          adds a Sprite to the canvas.
private  void clearBackground(java.awt.Graphics2D brush)
          paints a rectangle in the default background color
 boolean containsSprite(Sprite sprite)
          determines if the Sprite exists on the display
 Sprite[] getAllSprites()
          gets a copy of the Sprite collection
 int getHeight()
          returns height of the canvas
 java.awt.Dimension getMaximumSize()
          returns maximum size
 java.awt.Dimension getMinimumSize()
          returns minimum size
 java.awt.Dimension getPreferredSize()
          returns the preferred size
 java.awt.Dimension getSize()
          returns the size of the canvas
 int getWidth()
          returns width of the canvas
private  void makeWaterMark()
          makes the semi-transparent string sprites appearing at the lower right of the canvas.
protected  void paintComponent(java.awt.Graphics brush)
          paints all of the Sprites in the center of the component
 void paintImmediately()
          redraws the area which contains the Sprites
private  void paintSprites(java.awt.Graphics2D brush)
          paints all of the Sprites
 void removeAllSprites()
          clears the canvas of all Sprites
 void removeSprite(Sprite sprite)
          removes the Sprite from the canvas.
 void setSize(java.awt.Dimension size)
          sets size to size
 void setSize(int width, int height)
          sets the display size.
 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, 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, 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, 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

sprites

private java.util.LinkedHashSet<Sprite> sprites
the ordered collection of Sprites. The last added Sprite will appear on top.


waterMark

private Sprite[] waterMark
used for the transparent text at the bottom right


DEFAULT_BACKGROUND

private static final java.awt.Color DEFAULT_BACKGROUND
the color to use when clearing the background


DEFAULT_SIZE

private static final java.awt.Dimension DEFAULT_SIZE
default size of canvas


width

private int width
width of the canvas


height

private int height
height of canvas

Constructor Detail

AnimationCanvas

public AnimationCanvas()
constructs an empty canvas


AnimationCanvas

public AnimationCanvas(java.awt.Dimension size)
constructs an empty canvas

Method Detail

makeWaterMark

private void makeWaterMark()
makes the semi-transparent string sprites appearing at the lower right of the canvas. This watermark must not be removed or altered.


setSize

public void setSize(int width,
                    int height)
sets the display size. The display size does not grow and shrink dynamically

Overrides:
setSize in class java.awt.Component
Parameters:
size - the size of the display

setSize

public void setSize(java.awt.Dimension size)
sets size to size

Overrides:
setSize in class java.awt.Component
Parameters:
size -

paintImmediately

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


getWidth

public int getWidth()
returns width of the canvas

Overrides:
getWidth in class javax.swing.JComponent
Returns:
width

getHeight

public int getHeight()
returns height of the canvas

Overrides:
getHeight in class javax.swing.JComponent
Returns:
height

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

removeSprite

public void removeSprite(Sprite sprite)
removes the Sprite from the canvas.

Parameters:
sprite - the Sprite to be removed

removeAllSprites

public void removeAllSprites()
clears the canvas of all Sprites


containsSprite

public boolean containsSprite(Sprite sprite)
determines if the Sprite exists on the display

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

getAllSprites

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

Returns:
the copy of the Sprites collection in an array form

updateSprites

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


paintComponent

protected void paintComponent(java.awt.Graphics brush)
paints all of the Sprites in the center of the component

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

clearBackground

private void clearBackground(java.awt.Graphics2D brush)
paints a rectangle in the default background color

Parameters:
brush - the Graphics of the component

paintSprites

private void paintSprites(java.awt.Graphics2D brush)
paints all of the Sprites

Parameters:
brush - the Graphics of the component

addBottom

public void addBottom(Sprite sprite)
adds sprite to the bottom of the canvas

Parameters:
sprite -

getSize

public java.awt.Dimension getSize()
returns the size of the canvas

Overrides:
getSize in class java.awt.Component
Returns:
size

getPreferredSize

public java.awt.Dimension getPreferredSize()
returns the preferred size

Overrides:
getPreferredSize in class javax.swing.JComponent
Returns:
size

getMinimumSize

public java.awt.Dimension getMinimumSize()
returns minimum size

Overrides:
getMinimumSize in class javax.swing.JComponent
Returns:
size

getMaximumSize

public java.awt.Dimension getMaximumSize()
returns maximum size

Overrides:
getMaximumSize in class javax.swing.JComponent
Returns:
size