com.threecoffee.anim
Class Sprite

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JLabel
                  extended by com.threecoffee.anim.Sprite
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.lang.Runnable, javax.accessibility.Accessible, javax.swing.SwingConstants
Direct Known Subclasses:
Actor

public class Sprite
extends javax.swing.JLabel
implements java.lang.Runnable, java.io.Serializable

Simple class to represent an image or a simple animation consisting of a group of images.

Author:
Divyansh Prakash
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.Component
java.awt.Component.BaselineResizeBehavior
 
Field Summary
 
Fields inherited from class javax.swing.JComponent
TOOL_TIP_TEXT_KEY, 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 javax.swing.SwingConstants
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
Sprite()
          Creates a new animated sprite with no image
Sprite(Animation anim)
          Creates a new Sprite from the given Animation
 
Method Summary
 void addCollider(Sprite s)
          Checks for collision with the given Sprite, and calls the collided(Sprite) method of BOTH THE SPRITES if collision is detected.
 void addImage(javax.swing.ImageIcon ico)
          Adds a new image to the sprite.
 void addTo(GameWindow gaw)
          Add this Sprite to the given GameWindow.
 void checkCollisions()
          Used by the system to check if this Sprite has collided with a collider Object.
 void collided(Sprite s)
          Called whenever this Sprite collides with another Sprite.
 Animation getAnimation()
          Returns the Animation Object that this Sprite displays
 java.util.Vector<Sprite> getColliders()
          Returns all the Sprites that this Sprite can collide with.
 javax.swing.ImageIcon getCurrentImage()
          Returns the ImageIcon being displayed by the Sprite.
 int getCurrentSpriteIndex()
          Returns the index of the image being displayed currently by this sprite
 long getDelay()
          Returns the time in milliseconds between two successive frames.
 GameWindow getGameWindow()
          Returns the GameWindow that this Sprite is attached to.
 Gravity getGravity()
          Returns the Gravity object acting on the Sprite.
 java.util.Vector<javax.swing.ImageIcon> getImages()
          Returns all the images that this sprite contains.
 java.lang.String getName()
          Returns the name of this Sprite.
 java.awt.Point getPreviousLocation()
          Returns the previous location of the sprite.
 boolean hasGravity()
          Returns whether the Sprite is affected by Gravity.
 boolean hasPoint(int x, int y)
          Returns true if the Sprite contains the given location on the screen.
 boolean hasPoint(java.awt.Point p)
          Returns true if the Sprite contains the given location on the screen.
 void init()
          Called by the system when the play() method is called, just before the Sprite starts playing.
 boolean inView()
          Returns true if the Sprite is being displayed on the screen.
 boolean isPaused()
          Returns true if the Sprite is paused.
 void moveSprite(int x, int y)
          Moves the sprite x pixels to the right, and y pixels down.
 void pause(boolean b)
          Use this method to pause/resume the Sprite.
 void play()
          Call this method to start displaying the Sprite on the screen.
 void run()
          Used by the system internally.
 void setCurrentImage(javax.swing.ImageIcon ico)
          Sets the image being displayed to the given image.
 void setCurrentSpriteIndex(int i)
          Sets the image being displayed to the image at the given index.
 void setDelay(long l)
          Sets the time in milliseconds between two consecutive frames.
 void setGravity(boolean b)
          Adds/Removes gravity to/from the Sprite.
 void setGravity(Gravity g1)
          Sets the given Gravity object as this Sprite's default Gravity.
 void setImages(java.util.Vector<javax.swing.ImageIcon> i)
          Sets the given images as the ones that this Sprite displays.
 void setName(java.lang.String n)
          Sets the name of this Sprite to the given string.
 void stop()
          Stops playing the current Sprite.
 void undo()
          Forces the Sprite to go back to it's previous location.
 void update()
          This method is called again and again unless the Sprite is paused or stopped.
 
Methods inherited from class javax.swing.JLabel
getAccessibleContext, getDisabledIcon, getDisplayedMnemonic, getDisplayedMnemonicIndex, getHorizontalAlignment, getHorizontalTextPosition, getIcon, getIconTextGap, getLabelFor, getText, getUI, getUIClassID, getVerticalAlignment, getVerticalTextPosition, imageUpdate, setDisabledIcon, setDisplayedMnemonic, setDisplayedMnemonic, setDisplayedMnemonicIndex, setHorizontalAlignment, setHorizontalTextPosition, setIcon, setIconTextGap, setLabelFor, setText, setUI, setVerticalAlignment, setVerticalTextPosition, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, 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, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintImmediately, paintImmediately, print, printAll, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, 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, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, 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, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, contains, createImage, createImage, createVolatileImage, createVolatileImage, dispatchEvent, enable, enableInputMethods, 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, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, 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, 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, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Sprite

public Sprite()
Creates a new animated sprite with no image


Sprite

public Sprite(Animation anim)
Creates a new Sprite from the given Animation

Method Detail

getAnimation

public Animation getAnimation()
Returns the Animation Object that this Sprite displays


setName

public void setName(java.lang.String n)
Sets the name of this Sprite to the given string.

Overrides:
setName in class java.awt.Component

getName

public java.lang.String getName()
Returns the name of this Sprite.

Overrides:
getName in class java.awt.Component

addImage

public void addImage(javax.swing.ImageIcon ico)
Adds a new image to the sprite. All images added will be played in a continuous loop.


getImages

public java.util.Vector<javax.swing.ImageIcon> getImages()
Returns all the images that this sprite contains.


setImages

public void setImages(java.util.Vector<javax.swing.ImageIcon> i)
Sets the given images as the ones that this Sprite displays.


addCollider

public void addCollider(Sprite s)
Checks for collision with the given Sprite, and calls the collided(Sprite) method of BOTH THE SPRITES if collision is detected.


getColliders

public java.util.Vector<Sprite> getColliders()
Returns all the Sprites that this Sprite can collide with.


setDelay

public void setDelay(long l)
Sets the time in milliseconds between two consecutive frames.


getDelay

public long getDelay()
Returns the time in milliseconds between two successive frames.


setCurrentImage

public void setCurrentImage(javax.swing.ImageIcon ico)
Sets the image being displayed to the given image. The other images adjust accordingly.


getCurrentImage

public javax.swing.ImageIcon getCurrentImage()
Returns the ImageIcon being displayed by the Sprite.


setCurrentSpriteIndex

public void setCurrentSpriteIndex(int i)
Sets the image being displayed to the image at the given index. The other images adjust accordingly.


getCurrentSpriteIndex

public int getCurrentSpriteIndex()
Returns the index of the image being displayed currently by this sprite


setGravity

public void setGravity(boolean b)
Adds/Removes gravity to/from the Sprite.


setGravity

public void setGravity(Gravity g1)
Sets the given Gravity object as this Sprite's default Gravity.


hasGravity

public boolean hasGravity()
Returns whether the Sprite is affected by Gravity.


getGravity

public Gravity getGravity()
Returns the Gravity object acting on the Sprite. Returns null if the Sprite is not affected by gravity.


inView

public boolean inView()
Returns true if the Sprite is being displayed on the screen.


getPreviousLocation

public java.awt.Point getPreviousLocation()
Returns the previous location of the sprite.


run

public void run()
Used by the system internally. See the play() method instead.

Specified by:
run in interface java.lang.Runnable

checkCollisions

public void checkCollisions()
Used by the system to check if this Sprite has collided with a collider Object.


hasPoint

public boolean hasPoint(int x,
                        int y)
Returns true if the Sprite contains the given location on the screen.


hasPoint

public boolean hasPoint(java.awt.Point p)
Returns true if the Sprite contains the given location on the screen.


undo

public void undo()
Forces the Sprite to go back to it's previous location.


play

public void play()
Call this method to start displaying the Sprite on the screen.


init

public void init()
Called by the system when the play() method is called, just before the Sprite starts playing.


update

public void update()
This method is called again and again unless the Sprite is paused or stopped. Override this method to animate your sprite.


pause

public void pause(boolean b)
Use this method to pause/resume the Sprite.


isPaused

public boolean isPaused()
Returns true if the Sprite is paused.


moveSprite

public void moveSprite(int x,
                       int y)
Moves the sprite x pixels to the right, and y pixels down.


collided

public void collided(Sprite s)
Called whenever this Sprite collides with another Sprite.


stop

public void stop()
Stops playing the current Sprite.


addTo

public void addTo(GameWindow gaw)
Add this Sprite to the given GameWindow.


getGameWindow

public GameWindow getGameWindow()
Returns the GameWindow that this Sprite is attached to.