Package com.dkt.graphics.extras
Class GPixMap
java.lang.Object
com.dkt.graphics.elements.GraphicE
com.dkt.graphics.extras.GPixMap
- All Implemented Interfaces:
Cloneable
- Author:
- Federico Vera <[email protected]>
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionclone()
colorAt
(int i, int j) Retrieves theColor
on a given position of theGPixMap
Note: The value ofj
is affected by the direction of theY axis
boolean
contains
(int x, int y) Tells if a given set of coordinates is contained in the bounds of thisGPixMap
Note: invisibleGPixMap
s don't intersect with anythingboolean
Tells if aGLine
is contained in the bounds of thisGPixMap
Note: invisibleGPixMap
s don't intersect with anythingboolean
Tells if aGPoint
is contained in the bounds of thisGPixMap
Note: invisibleGPixMap
s don't intersect with anythingboolean
Tells if aGRectangle
is contained in the bounds of thisGPixMap
Note: invisibleGPixMap
s don't intersect with anythingvoid
draw
(Graphics2D g) Draws the component on the given graphicsboolean
Tells if theGPixMap
will draw the inner lines or notboolean
static void
Saves a text representation of thisGPixMap
to a fileRetrieves the bounds of thisGPixMap
at this particular moment, if you plan on using this to check for collisions, it's better to use thecontains(...)
methods of this classColor[][]
Retrieves a copy of the color data expressed as aColor
matrixint[][]
getData()
Retrieves a copy of the color data expressed as anint
matrixThis will retrieve the position of the lower left corner of theGPixMap
.
Note: if the canvas is inverting theY axis
then it will represent the upper left cornerint
getXSize()
Retrieves the number of horizontal pixels in thisGPixMap
int
getYSize()
Retrieves the number of vertical pixels in thisGPixMap
int
hashCode()
static GPixMap
Imports aGPixMap
from a filestatic GPixMap
importMap
(InputStream is) Imports aGPixMap
from a givenInputStream
boolean
Tells thisGPixMap
intersects with a givenGRectangle
Note: invisibleGPixMap
s don't intersect with anythingboolean
intersects
(GPixMap map) Tells twoGPixMap
s intersect
Note: invisibleGPixMap
s don't intersect with anythingboolean
isSquare()
Tells if theGPixMap
has the same number of rows and columnsboolean
Tells if theGPixMap
will be drawn
Note: invisibleGPixMap
s don't intersect with anythingRetrieves a newGPixMap
which is the horizontally mirrored version of this one.Retrieves a newGPixMap
which is the vertically mirrored version of this one.int
Retrieves the pixel sizeRetrieves a newGPixMap
which is the result of rotating this one counterclockwise 90°.rotateCW()
Retrieves a newGPixMap
which is the result of rotating this one clockwise 90°.void
setColorAt
(int i, int j, Color col) Set's theColor
at the specified coordinates
Note: The value ofj
is affected by the direction of theY axis
void
setDrawLines
(boolean draw) Tells theGPixMap
to draw the inner/outer linesvoid
setPixelSize
(int size) Set's the pixel size of theGPixMap
void
setValueAt
(int i, int j, int argb, boolean hasAlpha) Set's theColor
at the specified coordinates which will be created asnew Color(argb, hasAlpha)
Note: The value ofj
is affected by the direction of theY axis
void
setVisible
(boolean v) Tells theGPixMap
to draw itself in the canvas Note: invisibleGPixMap
s don't intersect with anythingCreates aString
representation of theGPixMap
, which consists of space separated values representing the hex representation of the colors of each pixel.boolean
Tells if twoGPixMap
s touch each other, this differs from theintersects(GPixMap)
because this checks if the images are actually touching each other ignoring the transparent pixels.
This method will only work onGPixMap
s that have the same pixel size.
Note: This method will most likely fail miserably when combined withGTransform
s.
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 atraslateUnits(int, int)
method.void
traslate
(int x, int y) Traslates this element on X and Yvoid
traslateUnits
(int h, int v) As opposed toGraphicE.traslate(int, int)
this method will traslate the PixMap a certain amount ofpixels
(consideringpixel
the size of each square drawn in the screen)int
valueAt
(int i, int j) Retrieves theColor
on a given position of theGPixMap
Note: The value ofj
is affected by the direction of theY axis
-
Constructor Details
-
GPixMap
Copy constructor- Parameters:
e
-GPixMap
to copy- Throws:
IllegalArgumentException
- ife
isnull
-
GPixMap
Creates a newGPixMap
of a given size- Parameters:
x
- horizontal size in pixelsy
- vertical size in pixels- Throws:
InvalidArgumentException
- if eitherx
ory
are smaller or equal to zero
-
GPixMap
Constructs a newGPixMap
from a givenColor
matrix- Parameters:
data
- The data that will form theGPixMap
- Throws:
IllegalArgumentException
- ifdata
isnull
or if any of the rows arenull
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 newGPixMap
from a givenint
matrix- Parameters:
data
- The data that will form theGPixMap
all this data will be converted toColor
objects, so they should have the formARGB
hasAlpha
- Tells if the ints represent a color with alpha- Throws:
IllegalArgumentException
- ifdata
isnull
or if any of the rows arenull
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
Set's the pixel size of theGPixMap
- Parameters:
size
- new pixel size- Throws:
InvalidArgumentException
- if thesize <= 0
-
getXSize
public int getXSize()Retrieves the number of horizontal pixels in thisGPixMap
- Returns:
- horizontal pixels
-
getYSize
public int getYSize()Retrieves the number of vertical pixels in thisGPixMap
- Returns:
- vertical pixels
-
colorAt
Retrieves theColor
on a given position of theGPixMap
Note: The value ofj
is affected by the direction of theY axis
- Parameters:
i
- vertical coordinatej
- horizontal coordinate- Returns:
- Color
- Throws:
IntervalException
- if eitheri
orj
are out of range
-
setColorAt
Set's theColor
at the specified coordinates
Note: The value ofj
is affected by the direction of theY axis
- Parameters:
i
- vertical coordinatej
- horizontal coordinatecol
- the color to set- Throws:
IntervalException
- if eitheri
orj
are out of rangeIllegalArgumentException
- ifcol
isnull
-
valueAt
Retrieves theColor
on a given position of theGPixMap
Note: The value ofj
is affected by the direction of theY axis
- Parameters:
i
- vertical coordinatej
- horizontal coordinate- Returns:
- numerical value of the color
- Throws:
IntervalException
- if eitheri
orj
are out of range
-
setValueAt
Set's theColor
at the specified coordinates which will be created asnew Color(argb, hasAlpha)
Note: The value ofj
is affected by the direction of theY axis
- Parameters:
i
- vertical coordinatej
- horizontal coordinateargb
- color datahasAlpha
-true
if the color has alpha andfalse
otherwise- Throws:
IntervalException
- if eitheri
orj
are out of range
-
getPosition
This will retrieve the position of the lower left corner of theGPixMap
.
Note: if the canvas is inverting theY axis
then it will represent the upper left corner- Returns:
GPoint
representing the lower left corner
-
setDrawLines
public void setDrawLines(boolean draw) Tells theGPixMap
to draw the inner/outer lines- Parameters:
draw
-true
to draw the lines andfalse
otherwise
-
drawLines
public boolean drawLines()Tells if theGPixMap
will draw the inner lines or not- Returns:
true
if theGPixMap
is drawing the inner lines andfalse
otherwise
-
pixelSize
public int pixelSize()Retrieves the pixel size- Returns:
- pixel size
-
getBounds
Retrieves the bounds of thisGPixMap
at this particular moment, if you plan on using this to check for collisions, it's better to use thecontains(...)
methods of this class- Returns:
GRectangle
object representing the bounds of thisGPixMap
- See Also:
-
contains
public boolean contains(int x, int y) Tells if a given set of coordinates is contained in the bounds of thisGPixMap
Note: invisibleGPixMap
s don't intersect with anything- Parameters:
x
-x
coordinate of the pointy
-y
coordinate of the point- Returns:
true
if the point is contained andfalse
otherwise- See Also:
-
contains
Tells if aGPoint
is contained in the bounds of thisGPixMap
Note: invisibleGPixMap
s don't intersect with anything- Parameters:
p
-GPoint
- Returns:
true
if the point is contained andfalse
otherwise- See Also:
-
contains
Tells if aGLine
is contained in the bounds of thisGPixMap
Note: invisibleGPixMap
s don't intersect with anything- Parameters:
l
-GLine
- Returns:
true
if the point is contained andfalse
otherwise- See Also:
-
intersects
Tells thisGPixMap
intersects with a givenGRectangle
Note: invisibleGPixMap
s don't intersect with anything- Parameters:
r
-GRectangle
- Returns:
true
if the maps intersect andfalse
otherwise- See Also:
-
intersects
Tells twoGPixMap
s intersect
Note: invisibleGPixMap
s don't intersect with anything- Parameters:
map
- otherGPixMap
- Returns:
true
if the maps intersect andfalse
otherwise- See Also:
-
contains
Tells if aGRectangle
is contained in the bounds of thisGPixMap
Note: invisibleGPixMap
s don't intersect with anything- Parameters:
r
- rectangle- Returns:
true
if the point is contained andfalse
otherwise- See Also:
-
traslateUnits
public void traslateUnits(int h, int v) As opposed toGraphicE.traslate(int, int)
this method will traslate the PixMap a certain amount ofpixels
(consideringpixel
the size of each square drawn in the screen)- Parameters:
h
- number of horizontal pixelsv
- number of vertical pixels- See Also:
-
isVisible
public boolean isVisible()Tells if theGPixMap
will be drawn
Note: invisibleGPixMap
s don't intersect with anything- Returns:
true
if it's visible andfalse
otherwise- See Also:
-
setVisible
public void setVisible(boolean v) Tells theGPixMap
to draw itself in the canvas Note: invisibleGPixMap
s don't intersect with anything- Parameters:
v
-true
if theGPixMap
will be drawn andfalse
otherwise- See Also:
-
isSquare
public boolean isSquare()Tells if theGPixMap
has the same number of rows and columns- Returns:
true
if theGPixMap
is square andfalse
otherwise
-
rotateCW
Retrieves a newGPixMap
which is the result of rotating this one clockwise 90°.+-----+ +-------+ | *** | | ***** | | * | | * | -> | * | | * | | * | +-------+ | * | +-----+
- Returns:
- Clockwise turn of this
GPixMap
-
rotateCCW
Retrieves a newGPixMap
which is the result of rotating this one counterclockwise 90°.+-----+ +-------+ | * | | ***** | | * | | * | -> | * | | * | | * | +-------+ | *** | +-----+
- Returns:
- Counterclockwise turn of this
GPixMap
-
mirrorHorizontal
Retrieves a newGPixMap
which is the horizontally mirrored version of this one.+------+ +------+ | **** | | **** | | * | -> | * | | **** | | **** | +------+ +------+
- Returns:
- Horizontal mirror of this
GPixMap
-
mirrorVertical
Retrieves a newGPixMap
which is the vertically mirrored version of this one.+------+ +------+ | * * | | **** | | * * | -> | * * | | **** | | * * | +------+ +------+
- Returns:
- Vertical mirror of this
GPixMap
-
touches
Tells if twoGPixMap
s touch each other, this differs from theintersects(GPixMap)
because this checks if the images are actually touching each other ignoring the transparent pixels.
This method will only work onGPixMap
s that have the same pixel size.
Note: This method will most likely fail miserably when combined withGTransform
s.
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 atraslateUnits(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
- theGPixMap
to check against- Returns:
true
if the twoGPixMap
s touch andfalse
otherwise- Throws:
IllegalArgumentException
- ifmap
isnull
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 -
draw
Description copied from class:GraphicE
Draws the component on the given graphics -
getColorData
Retrieves a copy of the color data expressed as aColor
matrix- Returns:
- color data
-
getData
public int[][] getData()Retrieves a copy of the color data expressed as anint
matrix- Returns:
- color data
-
toDataString
Creates aString
representation of theGPixMap
, 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 theGPixMap
-
exportMap
Saves a text representation of thisGPixMap
to a file- Parameters:
map
-GPixMap
to exportfile
- file in which to export- Throws:
IOException
- if something goes wrong when writing the fileIllegalArgumentException
- iffile
ormap
arenull
- See Also:
-
importMap
Imports aGPixMap
from a givenInputStream
- Parameters:
is
-InputStream
of the file- Returns:
- a new
GPixMap
decoded from theInputStream
- Throws:
IOException
- if something goes wrong when reading the fileIllegalArgumentException
- ifis
isnull
- See Also:
-
importMap
public static GPixMap importMap(File file) throws FileNotFoundException, IOException, IllegalArgumentException Imports aGPixMap
from a file- Parameters:
file
- file from which to read theGPixMap
- Returns:
- a new
GPixMap
decoded from theFile
- Throws:
FileNotFoundException
- if the file doesn't existIOException
- if something goes wrong when reading the fileIllegalArgumentException
- ifis
isnull
- See Also:
-
clone
-
hashCode
public int hashCode() -
equals
-