Class Gif

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

public class Gif extends Object
The purpose of this class is to create animated GIFs from a series of canvas snapshots
Since:
0.1.10
Author:
Federico Vera <[email protected]>
  • Constructor Summary

    Constructors
    Constructor
    Description
    Gif(Canvas canvas)
    Creates a new Gif object.
  • Method Summary

    Modifier and Type
    Method
    Description
    get(int idx)
    Retrieves one of the snapshots
    int
    Retrieves the delay between images in ms.
    The default value is 100ms.
    Retrieves the last exception in case such exception exists.
    remove(int idx)
    Removes on of the snapshots
    void
    setDelay(int delay)
    The delay between images in ms.
    int
    Retrieves the number of snapshots taken
    void
    This method should be called every time you want a snapshot of the canvas to be taken.
    void
    snapshot(int num)
    This method calls snapshot() a given number of times, which results in (besides an increase in size) the image staying on the gif num times delay seconds.
    boolean
    write(String path)
    Writes the image to a file, if this method fails it will return false and the exception will be saved.
    static void
    write(String path, int delay, BufferedImage... imgs)
    Creates a new gif from an array of BufferedImage.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • size

      public int size()
      Retrieves the number of snapshots taken
      Returns:
      number of snapshots
    • remove

      public BufferedImage remove(int idx)
      Removes on of the snapshots
      Parameters:
      idx - index of the snapshot
      Returns:
      the image that was removed or null if the index is out of bounds.
    • get

      public BufferedImage get(int idx)
      Retrieves one of the snapshots
      Parameters:
      idx - index of th image
      Returns:
      snapshot or null if the index is out of bounds
    • getDelay

      public int getDelay()
      Retrieves the delay between images in ms.
      The default value is 100ms.
      Returns:
      delay between images
      See Also:
    • setDelay

      public void setDelay(int delay)
      The delay between images in ms. Note that since the GIF specification sets the delay in hundredths of a second the number of ms is actually divided by ten.
      Parameters:
      delay - delay in ms
      See Also:
    • snapshot

      public void snapshot()
      This method should be called every time you want a snapshot of the canvas to be taken. You must take at least 1 in order for the save method to do something.
      See Also:
    • snapshot

      public void snapshot(int num)
      This method calls snapshot() a given number of times, which results in (besides an increase in size) the image staying on the gif num times delay seconds.
      Parameters:
      num - number of snapshots
      Throws:
      InvalidArgumentException - if num is less than 1
      See Also:
    • write

      public boolean write(String path) throws IllegalArgumentException
      Writes the image to a file, if this method fails it will return false and the exception will be saved.
      Parameters:
      path - String representing the path and name of the file
      Returns:
      true id the gif was correctly written and false if something goes wrong.
      Throws:
      IllegalArgumentException - if path is null
      See Also:
    • getLastException

      public Exception getLastException()
      Retrieves the last exception in case such exception exists.
      This method will always return null before write(String) is called for the first time.
      Returns:
      the exception or null if none exists.
      See Also:
    • write

      public static void write(String path, int delay, BufferedImage... imgs) throws IllegalArgumentException, IOException
      Creates a new gif from an array of BufferedImage.
      Parameters:
      path - The path in which to save the gif
      delay - The delay between frames in hundredths of a second
      imgs - Array of images
      Throws:
      IllegalArgumentException - if path is null
      IOException - if something goes wrong when saving the image