Package com.dkt.graphics.utils
Class Utils
java.lang.Object
com.dkt.graphics.utils.Utils
- Author:
- Federico Vera <[email protected]>
-
Method Summary
Modifier and TypeMethodDescriptionstatic BufferedImage
Draws aGraphicE
into aBufferedImage
static Color
getColorWithAlpha
(Color color, int alpha) Generates a newColor
based on the color but with the given alphastatic BufferedImage
Retrieves a 1:1 image of the canvasstatic Paint
getTexture
(String path) Generates a texture from a given filestatic void
saveScreenshot
(Canvas canvas, String path, boolean back) Saves a 1:1 screenshot of the canvas
The image is saved using thepng
format, so if the file doesn't end with.png
the extension will be appendedstatic BufferedImage
trimImage
(BufferedImage img, Color omit) Trims a given image to its minimum size without loosing any part of the drawing.
-
Method Details
-
getColorWithAlpha
Generates a newColor
based on the color but with the given alpha- Parameters:
color
- the original coloralpha
- the alpha value if the value is bigger than 255 it will be set to 255, and if the value is smaller than 0 then it will be set to 0- Returns:
- A new color with the same RGB value as the argument but with the specified alpha
- Throws:
IllegalArgumentException
- if color isnull
-
saveScreenshot
Saves a 1:1 screenshot of the canvas
The image is saved using thepng
format, so if the file doesn't end with.png
the extension will be appended- Parameters:
canvas
- TheCanvas
to printpath
- The path of the fileback
-true
if you want to paint the background andfalse
otherwise (false
is needed when painting with transparent components.- Throws:
IOException
- If the image can't be writtenIllegalArgumentException
- If either the canvas or the path arenull
-
getImage
Retrieves a 1:1 image of the canvas- Parameters:
canvas
- TheCanvas
to printback
-true
if you want to paint the background andfalse
otherwise (false
is needed when painting with transparent components.- Returns:
- a
BufferedImage
the same size of the canvas - Throws:
IllegalArgumentException
- If the canvas isnull
-
trimImage
Trims a given image to its minimum size without loosing any part of the drawing.- Parameters:
img
- The image to trimomit
- TheColor
to omit when trimming (what to consider as not drawn). Usually isColor.WHITE
ornew Color(0, true)
- Returns:
- a
BufferedImage
result of trimming the original image - Throws:
IllegalArgumentException
- Ifimg
isnull
-
getTexture
Generates a texture from a given file- Parameters:
path
- the path of the image file- Returns:
- A
TexturePaint
for this file - Throws:
IOException
- If the image can't be read or if the file isn't a valid imageIllegalArgumentException
- If the path isnull
-
draw
Draws aGraphicE
into aBufferedImage
- Parameters:
e
- element to draww
- width of the imageh
- height of the image- Returns:
- a
BufferedImage
that represents theGraphicE
-