Class Utils

java.lang.Object
com.dkt.graphics.utils.Utils

public class Utils extends Object
Author:
Federico Vera <[email protected]>
  • Method Details

    • getColorWithAlpha

      public static Color getColorWithAlpha(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:
      IllegalArgumentException - if color is null
    • saveScreenshot

      public static void saveScreenshot(Canvas canvas, String path, boolean back) throws 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:
      IOException - If the image can't be written
      IllegalArgumentException - If either the canvas or the path are null
    • getImage

      public static 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:
      IllegalArgumentException - If the canvas is null
    • trimImage

      public static BufferedImage trimImage(BufferedImage img, Color omit)
      Trims a given image to its 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:
      IllegalArgumentException - If img is null
    • getTexture

      public static Paint getTexture(String path) throws IOException
      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 image
      IllegalArgumentException - If the path is null
    • draw

      public static 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