Class Utils


  • public class Utils
    extends java.lang.Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.awt.image.BufferedImage draw​(GraphicE e, int w, int h)
      Draws a GraphicE into a BufferedImage
      static java.awt.Color getColorWithAlpha​(java.awt.Color color, int alpha)
      Generates a new Color based on the color but with the given alpha
      static java.awt.image.BufferedImage getImage​(Canvas canvas, boolean back)
      Retrieves a 1:1 image of the canvas
      static java.awt.Paint getTexture​(java.lang.String path)
      Generates a texture from a given file
      static void saveScreenshot​(Canvas canvas, java.lang.String path, boolean back)
      Saves a 1:1 screenshot of the canvas
      The image is saved using the png format, so if the file doesn't end with .png the extension will be appended
      static java.awt.image.BufferedImage trimImage​(java.awt.image.BufferedImage img, java.awt.Color omit)
      Trims a given image to it's minimum size without loosing any part of the drawing.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getColorWithAlpha

        public static java.awt.Color getColorWithAlpha​(java.awt.Color color,
                                                       int alpha)
        Generates a new Color based on the color but with the given alpha
        Parameters:
        color - the original color
        alpha - 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:
        java.lang.IllegalArgumentException - if color is null
      • saveScreenshot

        public static void saveScreenshot​(Canvas canvas,
                                          java.lang.String path,
                                          boolean back)
                                   throws java.io.IOException
        Saves a 1:1 screenshot of the canvas
        The image is saved using the png format, so if the file doesn't end with .png the extension will be appended
        Parameters:
        canvas - The Canvas to print
        path - The path of the file
        back - true if you want to paint the background and false otherwise (false is needed when painting with transparent components.
        Throws:
        java.io.IOException - If the image can't be written
        java.lang.IllegalArgumentException - If either the canvas or the path are null
      • getImage

        public static java.awt.image.BufferedImage getImage​(Canvas canvas,
                                                            boolean back)
        Retrieves a 1:1 image of the canvas
        Parameters:
        canvas - The Canvas to print
        back - true if you want to paint the background and false otherwise (false is needed when painting with transparent components.
        Returns:
        a BufferedImage the same size of the canvas
        Throws:
        java.lang.IllegalArgumentException - If the canvas is null
      • trimImage

        public static java.awt.image.BufferedImage trimImage​(java.awt.image.BufferedImage img,
                                                             java.awt.Color omit)
        Trims a given image to it's minimum size without loosing any part of the drawing.
        Parameters:
        img - The image to trim
        omit - The Color to omit when trimming (what to consider as not drawn). Usually is Color.WHITE or new Color(0, true)
        Returns:
        a BufferedImage result of trimming the original image
        Throws:
        java.lang.IllegalArgumentException - If img is null
      • getTexture

        public static java.awt.Paint getTexture​(java.lang.String path)
                                         throws java.io.IOException
        Generates a texture from a given file
        Parameters:
        path - the path of the image file
        Returns:
        A TexturePaint for this file
        Throws:
        java.io.IOException - If the image can't be read or if the file isn't a valid image
        java.lang.IllegalArgumentException - If the path is null
      • draw

        public static java.awt.image.BufferedImage draw​(GraphicE e,
                                                        int w,
                                                        int h)
        Draws a GraphicE into a BufferedImage
        Parameters:
        e - element to draw
        w - width of the image
        h - height of the image
        Returns:
        a BufferedImage that represents the GraphicE