Class GSprite

  • All Implemented Interfaces:
    java.lang.Cloneable, java.lang.Iterable<GPixMap>

    public class GSprite
    extends GraphicE
    implements java.lang.Iterable<GPixMap>
    Creates a simple sprite, if you need help creating the pixmaps, you should probably try out PixmapCreator
    • Field Summary

    • Constructor Summary

      Constructors 
      Constructor Description
      GSprite()
      Creates an empty sprite
      GSprite​(GSprite e)
      Copy constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void append​(GPixMap map)
      Append a new GPixMap to this GSprite, this method will skip all GPixMap that are already in the GSprite
      GSprite clone()  
      void draw​(java.awt.Graphics2D g)
      Draws the component on the given graphics
      boolean drawGrid()
      Tells if the GSprite will draw the inner lines or not
      void first()
      Sets the first GPixMap as the current one.
      GRectangle getBounds()
      Retrieves the current bounds of this GSprite
      int getCursor()
      Retrieves the current cursor position
      int getPixelSize()
      Retrieves the pixel size of the GSprite
      boolean isCyclic()
      Tells if the GSprite should behave as a circular list.
      The default value is true.
      boolean isVisible()
      Tells if the GSprite is visible
      java.util.Iterator<GPixMap> iterator()  
      void last()
      Sets the last GPixMap as the current one.
      boolean next()
      Passes to the next GPixMap in the current sprite.
      This method depends on the number of skips set, that is: if skips is 3, then this method must be called 3 times in order to make an actual change, apart from it, it also depends on isCyclic().
      boolean prev()
      Passes to the previous GPixMap in the current sprite.
      This method depends on the number of skips set, that is: if skips is 3, then this method must be called 3 times in order to make an actual change, apart from it, it also depends on isCyclic().
      boolean remove​(GPixMap map)
      Removes a given map from the sprite.
      void setCyclic​(boolean cyclic)
      Tells the GSprite to act as if the list of elements is circular (cyclic), that means that when the last GPixMap is reached, then it will continue with the first one, and viceversa.
      The default value is true.
      void setDrawGrid​(boolean grid)
      Tells the GSprite to draw the inner/outer lines
      void setPixelSize​(int px)
      Sets a new pixel size for all of the GPixMap of this GSprite
      void setSkips​(int s)
      Sets the number of skips, this means how many times should the next() and prev() should be called in order to actually work
      The default value is 1
      void setVisible​(boolean visible)
      Tells the GSprite if it should draw itself
      int skips()
      Retrieves the number of skips that are necessary in order for the next() and prev() methods to make an actual change.
      void traslate​(int x, int y)
      Traslates this element on X and Y
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • GSprite

        public GSprite​(GSprite e)
        Copy constructor.
        Parameters:
        e - GSprite to copy
      • GSprite

        public GSprite()
        Creates an empty sprite
    • Method Detail

      • append

        public void append​(GPixMap map)
                    throws java.lang.IllegalArgumentException,
                           InvalidArgumentException
        Append a new GPixMap to this GSprite, this method will skip all GPixMap that are already in the GSprite
        Parameters:
        map - GPixMap to add
        Throws:
        java.lang.IllegalArgumentException - if map is null
        InvalidArgumentException - if the map doesn't have the same size.
      • remove

        public boolean remove​(GPixMap map)
                       throws java.lang.IllegalArgumentException
        Removes a given map from the sprite. This method is most likely to fail, since GPixMap.equals(java.lang.Object) relies on the pixel size and the grid, and those attributes are mostly certainly changed by the constructor.
        Parameters:
        map - Map to remove
        Returns:
        true if the element was found and removed and false otherwise
        Throws:
        java.lang.IllegalArgumentException - if map is null
      • getCursor

        public int getCursor()
        Retrieves the current cursor position
        Returns:
        cursor position
      • first

        public void first()
        Sets the first GPixMap as the current one.
      • prev

        public boolean prev()
        Passes to the previous GPixMap in the current sprite.
        This method depends on the number of skips set, that is: if skips is 3, then this method must be called 3 times in order to make an actual change, apart from it, it also depends on isCyclic().
        Returns:
        true the pixmap change and false otherwise.
        See Also:
        setSkips(int), skips(), isCyclic()
      • next

        public boolean next()
        Passes to the next GPixMap in the current sprite.
        This method depends on the number of skips set, that is: if skips is 3, then this method must be called 3 times in order to make an actual change, apart from it, it also depends on isCyclic().
        Returns:
        true the pixmap change and false otherwise.
        See Also:
        setSkips(int), skips(), isCyclic()
      • last

        public void last()
        Sets the last GPixMap as the current one.
      • isCyclic

        public boolean isCyclic()
        Tells if the GSprite should behave as a circular list.
        The default value is true.
        Returns:
        true if the GSprite is circular, and false otherwise.
        See Also:
        setCyclic(boolean)
      • setCyclic

        public void setCyclic​(boolean cyclic)
        Tells the GSprite to act as if the list of elements is circular (cyclic), that means that when the last GPixMap is reached, then it will continue with the first one, and viceversa.
        The default value is true.
        Parameters:
        cyclic - true if the GSprite should act as circular, and false otherwise.
        See Also:
        isCyclic()
      • setSkips

        public void setSkips​(int s)
                      throws InvalidArgumentException
        Sets the number of skips, this means how many times should the next() and prev() should be called in order to actually work
        The default value is 1
        Parameters:
        s - new number of skips
        Throws:
        InvalidArgumentException - if s is less than 1
      • skips

        public int skips()
        Retrieves the number of skips that are necessary in order for the next() and prev() methods to make an actual change.
        Returns:
        number of skips
      • getPixelSize

        public int getPixelSize()
        Retrieves the pixel size of the GSprite
        Returns:
        pixel size
      • setDrawGrid

        public void setDrawGrid​(boolean grid)
        Tells the GSprite to draw the inner/outer lines
        Parameters:
        grid - true to draw the lines and false otherwise
      • drawGrid

        public boolean drawGrid()
        Tells if the GSprite will draw the inner lines or not
        Returns:
        true if the GSprite is drawing the inner lines and false otherwise
      • getBounds

        public GRectangle getBounds()
        Retrieves the current bounds of this GSprite
        Returns:
        bounds of this GSprite
      • setVisible

        public void setVisible​(boolean visible)
        Tells the GSprite if it should draw itself
        Parameters:
        visible - true if the GSprite should be drawn and false otherwise
      • isVisible

        public boolean isVisible()
        Tells if the GSprite is visible
        Returns:
        true if the GSprite is visible and false otherwise
      • draw

        public void draw​(java.awt.Graphics2D g)
        Description copied from class: GraphicE
        Draws the component on the given graphics
        Specified by:
        draw in class GraphicE
        Parameters:
        g - where to draw
      • traslate

        public void traslate​(int x,
                             int y)
        Description copied from class: GraphicE
        Traslates this element on X and Y
        Specified by:
        traslate in class GraphicE
        Parameters:
        x - size in px of the horizontal translation
        y - size in px of the vertical translation
      • iterator

        public java.util.Iterator<GPixMap> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<GPixMap>