Class Gif


  • public class Gif
    extends java.lang.Object
    The purpose of this class is to create animated GIFs from a series of canvas snapshots
    Since:
    0.1.10
    • Constructor Summary

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

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.awt.image.BufferedImage get​(int idx)
      Retrieves one of the snapshots
      int getDelay()
      Retrieves the delay between images in ms.
      The default value is 100ms.
      java.lang.Exception getLastException()
      Retrieves the last exception in case such exception exists.
      java.awt.image.BufferedImage remove​(int idx)
      Removes on of the snapshots
      void setDelay​(int delay)
      The delay between images in ms.
      int size()
      Retrieves the number of snapshots taken
      void snapshot()
      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​(java.lang.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​(java.lang.String path, int delay, java.awt.image.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 Detail

      • Gif

        public Gif​(Canvas canvas)
            throws java.lang.IllegalArgumentException
        Creates a new Gif object.
        Parameters:
        canvas - the canvas from which to create the images
        Throws:
        java.lang.IllegalArgumentException - if canvas is null
        See Also:
        write(String, int, BufferedImage...)
    • Method Detail

      • size

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

        public java.awt.image.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 java.awt.image.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(int)
      • 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:
        getDelay()
      • 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(int)
      • 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:
        snapshot()
      • write

        public boolean write​(java.lang.String path)
                      throws java.lang.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:
        java.lang.IllegalArgumentException - if path is null
        See Also:
        getLastException(), setDelay(int)
      • getLastException

        public java.lang.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(String)
      • write

        public static void write​(java.lang.String path,
                                 int delay,
                                 java.awt.image.BufferedImage... imgs)
                          throws java.lang.IllegalArgumentException,
                                 java.io.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:
        java.lang.IllegalArgumentException - if path is null
        java.io.IOException - if something goes wrong when saving the image