Class GPixMap

java.lang.Object
com.dkt.graphics.elements.GraphicE
com.dkt.graphics.extras.GPixMap
All Implemented Interfaces:
Cloneable

public final class GPixMap extends GraphicE
Author:
Federico Vera <[email protected]>
  • Field Summary

    Fields inherited from class com.dkt.graphics.elements.GraphicE

    STROKE
  • 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
    Copy constructor
    GPixMap(Color[][] data)
    Constructs a new GPixMap from a given Color matrix
  • Method Summary

    Modifier and Type
    Method
    Description
     
    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
    Tells if a GLine is contained in the bounds of this GPixMap
    Note: invisible GPixMaps don't intersect with anything
    boolean
    Tells if a GPoint is contained in the bounds of this GPixMap
    Note: invisible GPixMaps don't intersect with anything
    boolean
    Tells if a GRectangle is contained in the bounds of this GPixMap
    Note: invisible GPixMaps don't intersect with anything
    void
    Draws the component on the given graphics
    boolean
    Tells if the GPixMap will draw the inner lines or not
    boolean
     
    static void
    exportMap(GPixMap map, File file)
    Saves a text representation of this GPixMap to a file
    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
    Color[][]
    Retrieves a copy of the color data expressed as a Color matrix
    int[][]
    Retrieves a copy of the color data expressed as an int matrix
    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
    Retrieves the number of horizontal pixels in this GPixMap
    int
    Retrieves the number of vertical pixels in this GPixMap
    int
     
    static GPixMap
    Imports a GPixMap from a file
    static GPixMap
    Imports a GPixMap from a given InputStream
    boolean
    Tells this GPixMap intersects with a given GRectangle
    Note: invisible GPixMaps don't intersect with anything
    boolean
    Tells two GPixMaps intersect
    Note: invisible GPixMaps don't intersect with anything
    boolean
    Tells if the GPixMap has the same number of rows and columns
    boolean
    Tells if the GPixMap will be drawn
    Note: invisible GPixMaps don't intersect with anything
    Retrieves a new GPixMap which is the horizontally mirrored version of this one.
    Retrieves a new GPixMap which is the vertically mirrored version of this one.
    int
    Retrieves the pixel size
    Retrieves a new GPixMap which is the result of rotating this one counterclockwise 90°.
    Retrieves a new GPixMap which is the result of rotating this one clockwise 90°.
    void
    setColorAt(int i, int j, 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
    Creates a String representation of the GPixMap, which consists of space separated values representing the hex representation of the colors of each pixel.
    boolean
    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 from 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 com.dkt.graphics.elements.GraphicE

    getPaint, getStroke, setPaint, setStroke

    Methods inherited from class java.lang.Object

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

    • GPixMap

      public GPixMap(GPixMap e)
      Copy constructor
      Parameters:
      e - GPixMap to copy
      Throws:
      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(Color[][] data) throws IllegalArgumentException, InvalidArgumentException
      Constructs a new GPixMap from a given Color matrix
      Parameters:
      data - The data that will form the GPixMap
      Throws:
      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 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:
      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 Details

    • setPixelSize

      public void setPixelSize(int size) throws InvalidArgumentException
      Set's the pixel size of the GPixMap
      Parameters:
      size - new pixel size
      Throws:
      InvalidArgumentException - if the size <= 0
    • 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 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, Color col) throws IntervalException, 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
      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
    • getBounds

      public 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
      Returns:
      GRectangle object representing the bounds of this GPixMap
      See Also:
    • 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

      public boolean contains(GPoint p)
      Tells if a GPoint is contained in the bounds of this GPixMap
      Note: invisible GPixMaps don't intersect with anything
      Parameters:
      p - GPoint
      Returns:
      true if the point is contained and false otherwise
      See Also:
    • contains

      public boolean contains(GLine l)
      Tells if a GLine is contained in the bounds of this GPixMap
      Note: invisible GPixMaps don't intersect with anything
      Parameters:
      l - GLine
      Returns:
      true if the point is contained and false otherwise
      See Also:
    • intersects

      public boolean intersects(GRectangle r)
      Tells this GPixMap intersects with a given GRectangle
      Note: invisible GPixMaps don't intersect with anything
      Parameters:
      r - GRectangle
      Returns:
      true if the maps intersect and false otherwise
      See Also:
    • 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:
    • contains

      public boolean contains(GRectangle r)
      Tells if a GRectangle is contained in the bounds of this GPixMap
      Note: invisible GPixMaps don't intersect with anything
      Parameters:
      r - rectangle
      Returns:
      true if the point is contained and false otherwise
      See Also:
    • traslateUnits

      public 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)
      Parameters:
      h - number of horizontal pixels
      v - number of vertical pixels
      See Also:
    • 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

      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:
    • 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 counterclockwise 90°.
                                  +-----+
             +-------+            | *   |
             | ***** |            | *   |
             | *     |     ->     | *   |
             | *     |            | *   |
             +-------+            | *** |
                                  +-----+
      Returns:
      Counterclockwise 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 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 from 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:
      IllegalArgumentException - if map is null
      InvalidArgumentException - if the pixel sizes don't match
      See Also:
    • 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(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 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 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, File file) throws IOException, IllegalArgumentException
      Saves a text representation of this GPixMap to a file
      Parameters:
      map - GPixMap to export
      file - file in which to export
      Throws:
      IOException - if something goes wrong when writing the file
      IllegalArgumentException - if file or map are null
      See Also:
    • importMap

      public static GPixMap importMap(InputStream is) throws IOException, IllegalArgumentException
      Imports a GPixMap from a given InputStream
      Parameters:
      is - InputStream of the file
      Returns:
      a new GPixMap decoded from the InputStream
      Throws:
      IOException - if something goes wrong when reading the file
      IllegalArgumentException - if is is null
      See Also:
    • importMap

      public static GPixMap importMap(File file) throws FileNotFoundException, IOException, 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:
      FileNotFoundException - if the file doesn't exist
      IOException - if something goes wrong when reading the file
      IllegalArgumentException - if is is null
      See Also:
    • clone

      public GPixMap clone()
      Specified by:
      clone in class GraphicE
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class GraphicE
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class GraphicE