Class GPixMap

  • All Implemented Interfaces:
    java.lang.Cloneable

    public final class GPixMap
    extends GraphicE
    • Field Summary

    • Constructor Summary

      Constructors 
      Constructor Description
      GPixMap​(int[][] data, boolean hasAlpha)
      Constructs a new GPixMap from a given int matrix
      GPixMap​(int x, int y)
      Creates a new GPixMap of a given size
      GPixMap​(GPixMap e)
      Copy constructor
      GPixMap​(java.awt.Color[][] data)
      Constructs a new GPixMap from a given Color matrix
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      GPixMap clone()  
      java.awt.Color colorAt​(int i, int j)
      Retrieves the Color on a given position of the GPixMap
      Note: The value of j is affected by the direction of the Y axis
      boolean contains​(int x, int y)
      Tells if a given set of coordinates is contained in the bounds of this GPixMap
      Note: invisible GPixMaps don't intersect with anything
      boolean contains​(GLine l)
      Tells if a GLine is contained in the bounds of this GPixMap
      Note: invisible GPixMaps don't intersect with anything
      boolean contains​(GPoint p)
      Tells if a GPoint is contained in the bounds of this GPixMap
      Note: invisible GPixMaps don't intersect with anything
      boolean contains​(GRectangle r)
      Tells if a GRectangle is contained in the bounds of this GPixMap
      Note: invisible GPixMaps don't intersect with anything
      void draw​(java.awt.Graphics2D g)
      Draws the component on the given graphics
      boolean drawLines()
      Tells if the GPixMap will draw the inner lines or not
      boolean equals​(java.lang.Object obj)  
      static void exportMap​(GPixMap map, java.io.File file)
      Saves a text representation of this GPixMap to a file
      GRectangle getBounds()
      Retrieves the bounds of this GPixMap at this particular moment, if you plan on using this to check for collisions, it's better to use the contains(...) methods of this class
      java.awt.Color[][] getColorData()
      Retrieves a copy of the color data expressed as a Color matrix
      int[][] getData()
      Retrieves a copy of the color data expressed as an int matrix
      GPoint getPosition()
      This will retrieve the position of the lower left corner of the GPixMap.
      Note: if the canvas is inverting the Y axis then it will represent the upper left corner
      int getXSize()
      Retrieves the number of horizontal pixels in this GPixMap
      int getYSize()
      Retrieves the number of vertical pixels in this GPixMap
      int hashCode()  
      static GPixMap importMap​(java.io.File file)
      Imports a GPixMap from a file
      static GPixMap importMap​(java.io.InputStream is)
      Imports a GPixMap from a given InputStream
      boolean intersects​(GRectangle r)
      Tells this GPixMap intersects with a given GRectangle
      Note: invisible GPixMaps don't intersect with anything
      boolean intersects​(GPixMap map)
      Tells two GPixMaps intersect
      Note: invisible GPixMaps don't intersect with anything
      boolean isSquare()
      Tells if the GPixMap has the same number of rows and columns
      boolean isVisible()
      Tells if the GPixMap will be drawn
      Note: invisible GPixMaps don't intersect with anything
      GPixMap mirrorHorizontal()
      Retrieves a new GPixMap which is the horizontally mirrored version of this one.
      GPixMap mirrorVertical()
      Retrieves a new GPixMap which is the vertically mirrored version of this one.
      int pixelSize()
      Retrieves the pixel size
      GPixMap rotateCCW()
      Retrieves a new GPixMap which is the result of rotating this one counter clockwise 90°.
      GPixMap rotateCW()
      Retrieves a new GPixMap which is the result of rotating this one clockwise 90°.
      void setColorAt​(int i, int j, java.awt.Color col)
      Set's the Color at the specified coordinates
      Note: The value of j is affected by the direction of the Y axis
      void setDrawLines​(boolean draw)
      Tells the GPixMap to draw the inner/outer lines
      void setPixelSize​(int size)
      Set's the pixel size of the GPixMap
      void setValueAt​(int i, int j, int argb, boolean hasAlpha)
      Set's the Color at the specified coordinates which will be created as new Color(argb, hasAlpha)
      Note: The value of j is affected by the direction of the Y axis
      void setVisible​(boolean v)
      Tells the GPixMap to draw itself in the canvas Note: invisible GPixMaps don't intersect with anything
      java.lang.String toDataString()
      Creates a String representation of the GPixMap, which consists of space separated values representing the hex representation of the colors of each pixel.
      boolean touches​(GPixMap map)
      Tells if two GPixMaps touch each other, this differs from the intersects(GPixMap) because this checks if the images are actually touching each other ignoring the transparent pixels.
      This method will only work on GPixMaps that have the same pixel size.
      Note: This method will most likely fail miserably when combined with GTransforms.
      Note 2: There are some issues when the traslation step is different than the pixel size, but that should be a non issue, since it's a "very rare" scenario, and that's why we have a traslateUnits(int, int) method.
      void traslate​(int x, int y)
      Traslates this element on X and Y
      void traslateUnits​(int h, int v)
      As opposed to GraphicE.traslate(int, int) this method will traslate the PixMap a certain amount of pixels (considering pixel the size of each square drawn in the screen)
      int valueAt​(int i, int j)
      Retrieves the Color on a given position of the GPixMap
      Note: The value of j is affected by the direction of the Y axis
      • Methods inherited from class java.lang.Object

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

      • GPixMap

        public GPixMap​(GPixMap e)
        Copy constructor
        Parameters:
        e - GPixMap to copy
        Throws:
        java.lang.IllegalArgumentException - if e is null
      • GPixMap

        public GPixMap​(int x,
                       int y)
                throws InvalidArgumentException
        Creates a new GPixMap of a given size
        Parameters:
        x - horizontal size in pixels
        y - vertical size in pixels
        Throws:
        InvalidArgumentException - if either x or y are smaller or equal to zero
      • GPixMap

        public GPixMap​(java.awt.Color[][] data)
                throws java.lang.IllegalArgumentException,
                       InvalidArgumentException
        Constructs a new GPixMap from a given Color matrix
        Parameters:
        data - The data that will form the GPixMap
        Throws:
        java.lang.IllegalArgumentException - if data is null or if any of the rows are null
        InvalidArgumentException - if the matrix isn't rectangular (given that matrix implementations in Java are based on Iliffe vectors, it's possible for the rows to have different sizes
      • GPixMap

        public GPixMap​(int[][] data,
                       boolean hasAlpha)
                throws java.lang.IllegalArgumentException,
                       InvalidArgumentException
        Constructs a new GPixMap from a given int matrix
        Parameters:
        data - The data that will form the GPixMap all this data will be converted to Color objects so they should have the form ARGB
        hasAlpha - Tells if the ints represent a color with alpha
        Throws:
        java.lang.IllegalArgumentException - if data is null or if any of the rows are null
        InvalidArgumentException - if the matrix isn't rectangular (given that matrix implementations in Java are based on Iliffe vectors, it's possible for the rows to have different sizes
    • Method Detail

      • getXSize

        public int getXSize()
        Retrieves the number of horizontal pixels in this GPixMap
        Returns:
        horizontal pixels
      • getYSize

        public int getYSize()
        Retrieves the number of vertical pixels in this GPixMap
        Returns:
        vertical pixels
      • colorAt

        public java.awt.Color colorAt​(int i,
                                      int j)
                               throws IntervalException
        Retrieves the Color on a given position of the GPixMap
        Note: The value of j is affected by the direction of the Y axis
        Parameters:
        i - vertical coordinate
        j - horizontal coordinate
        Returns:
        Color
        Throws:
        IntervalException - if either i or j are out of range
      • setColorAt

        public void setColorAt​(int i,
                               int j,
                               java.awt.Color col)
                        throws IntervalException,
                               java.lang.IllegalArgumentException
        Set's the Color at the specified coordinates
        Note: The value of j is affected by the direction of the Y axis
        Parameters:
        i - vertical coordinate
        j - horizontal coordinate
        col - the color to set
        Throws:
        IntervalException - if either i or j are out of range
        java.lang.IllegalArgumentException - if col is null
      • valueAt

        public int valueAt​(int i,
                           int j)
                    throws IntervalException
        Retrieves the Color on a given position of the GPixMap
        Note: The value of j is affected by the direction of the Y axis
        Parameters:
        i - vertical coordinate
        j - horizontal coordinate
        Returns:
        numerical value of the color
        Throws:
        IntervalException - if either i or j are out of range
      • setValueAt

        public void setValueAt​(int i,
                               int j,
                               int argb,
                               boolean hasAlpha)
                        throws IntervalException
        Set's the Color at the specified coordinates which will be created as new Color(argb, hasAlpha)
        Note: The value of j is affected by the direction of the Y axis
        Parameters:
        i - vertical coordinate
        j - horizontal coordinate
        argb - color data
        hasAlpha - true if the color has alpha and false otherwise
        Throws:
        IntervalException - if either i or j are out of range
      • getPosition

        public GPoint getPosition()
        This will retrieve the position of the lower left corner of the GPixMap.
        Note: if the canvas is inverting the Y axis then it will represent the upper left corner
        Returns:
        GPoint representing the lower left corner
      • setDrawLines

        public void setDrawLines​(boolean draw)
        Tells the GPixMap to draw the inner/outer lines
        Parameters:
        draw - true to draw the lines and false otherwise
      • drawLines

        public boolean drawLines()
        Tells if the GPixMap will draw the inner lines or not
        Returns:
        true if the GPixMap is drawing the inner lines and false otherwise
      • pixelSize

        public int pixelSize()
        Retrieves the pixel size
        Returns:
        pixel size
      • contains

        public boolean contains​(int x,
                                int y)
        Tells if a given set of coordinates is contained in the bounds of this GPixMap
        Note: invisible GPixMaps don't intersect with anything
        Parameters:
        x - x coordinate of the point
        y - y coordinate of the point
        Returns:
        true if the point is contained and false otherwise
        See Also:
        contains(GLine), contains(GPoint), contains(GRectangle), setVisible(boolean), isVisible()
      • intersects

        public boolean intersects​(GPixMap map)
        Tells two GPixMaps intersect
        Note: invisible GPixMaps don't intersect with anything
        Parameters:
        map - other GPixMap
        Returns:
        true if the maps intersect and false otherwise
        See Also:
        setVisible(boolean), isVisible(), touches(GPixMap)
      • isVisible

        public boolean isVisible()
        Tells if the GPixMap will be drawn
        Note: invisible GPixMaps don't intersect with anything
        Returns:
        true if it's visible and false otherwise
        See Also:
        setVisible(boolean)
      • setVisible

        public void setVisible​(boolean v)
        Tells the GPixMap to draw itself in the canvas Note: invisible GPixMaps don't intersect with anything
        Parameters:
        v - true if the GPixMap will be drawn and false otherwise
        See Also:
        isVisible()
      • isSquare

        public boolean isSquare()
        Tells if the GPixMap has the same number of rows and columns
        Returns:
        true if the GPixMap is square and false otherwise
      • rotateCW

        public GPixMap rotateCW()
        Retrieves a new GPixMap which is the result of rotating this one clockwise 90°.
                                    +-----+
               +-------+            | *** |
               | ***** |            |   * |
               | *     |     ->     |   * |
               | *     |            |   * |
               +-------+            |   * |
                                    +-----+
        Returns:
        Clockwise turn of this GPixMap
      • rotateCCW

        public GPixMap rotateCCW()
        Retrieves a new GPixMap which is the result of rotating this one counter clockwise 90°.
                                    +-----+
               +-------+            | *   |
               | ***** |            | *   |
               | *     |     ->     | *   |
               | *     |            | *   |
               +-------+            | *** |
                                    +-----+
        Returns:
        Counter clockwise turn of this GPixMap
      • mirrorHorizontal

        public GPixMap mirrorHorizontal()
        Retrieves a new GPixMap which is the horizontally mirrored version of this one.
               +------+            +------+
               | **** |            | **** |
               | *    |     ->     |    * |
               | **** |            | **** |
               +------+            +------+
        Returns:
        Horizontal mirror of this GPixMap
      • mirrorVertical

        public GPixMap mirrorVertical()
        Retrieves a new GPixMap which is the vertically mirrored version of this one.
               +------+            +------+
               | *  * |            | **** |
               | *  * |     ->     | *  * |
               | **** |            | *  * |
               +------+            +------+
        Returns:
        Vertical mirror of this GPixMap
      • touches

        public boolean touches​(GPixMap map)
                        throws java.lang.IllegalArgumentException,
                               InvalidArgumentException
        Tells if two GPixMaps touch each other, this differs from the intersects(GPixMap) because this checks if the images are actually touching each other ignoring the transparent pixels.
        This method will only work on GPixMaps that have the same pixel size.
        Note: This method will most likely fail miserably when combined with GTransforms.
        Note 2: There are some issues when the traslation step is different than the pixel size, but that should be a non issue, since it's a "very rare" scenario, and that's why we have a traslateUnits(int, int) method. Oops... it appears to be a more common scenario than predicted... my bad. If someone actually wants to take care of this, that would be great.
        Parameters:
        map - the GPixMap to check against
        Returns:
        true if the two GPixMaps touch and false otherwise
        Throws:
        java.lang.IllegalArgumentException - if map is null
        InvalidArgumentException - if the pixel sizes don't match
        See Also:
        pixelSize()
      • 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
      • 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
      • getColorData

        public java.awt.Color[][] getColorData()
        Retrieves a copy of the color data expressed as a Color matrix
        Returns:
        color data
      • getData

        public int[][] getData()
        Retrieves a copy of the color data expressed as an int matrix
        Returns:
        color data
      • toDataString

        public java.lang.String toDataString()
        Creates a String representation of the GPixMap, which consists of space separated values representing the hex representation of the colors of each pixel. This method does not include either pixel size nor visibility of the grid.
        Returns:
        String representing the data of the GPixMap
      • exportMap

        public static void exportMap​(GPixMap map,
                                     java.io.File file)
                              throws java.io.IOException,
                                     java.lang.IllegalArgumentException
        Saves a text representation of this GPixMap to a file
        Parameters:
        map - GPixMap to export
        file - file in which to export
        Throws:
        java.io.IOException - if something goes wrong when writing the file
        java.lang.IllegalArgumentException - if file or map are null
        See Also:
        importMap(java.io.File), importMap(java.io.InputStream)
      • importMap

        public static GPixMap importMap​(java.io.InputStream is)
                                 throws java.io.IOException,
                                        java.lang.IllegalArgumentException
        Imports a GPixMap from a given InputStream
        Parameters:
        is - InputStream of the file
        Returns:
        a new GPixMap decoded from the InputStream
        Throws:
        java.io.IOException - if something goes wrong when reading the file
        java.lang.IllegalArgumentException - if is is null
        See Also:
        exportMap(com.dkt.graphics.extras.GPixMap, java.io.File), importMap(java.io.File)
      • importMap

        public static GPixMap importMap​(java.io.File file)
                                 throws java.io.FileNotFoundException,
                                        java.io.IOException,
                                        java.lang.IllegalArgumentException
        Imports a GPixMap from a file
        Parameters:
        file - file from which to read the GPixMap
        Returns:
        a new GPixMap decoded from the File
        Throws:
        java.io.FileNotFoundException - if the file doesn't exist
        java.io.IOException - if something goes wrong when reading the file
        java.lang.IllegalArgumentException - if is is null
        See Also:
        exportMap(com.dkt.graphics.extras.GPixMap, java.io.File), importMap(java.io.InputStream)
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class GraphicE